Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 While loop repeats even if conditions are met
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
username123




PostPosted: Mon Nov 16, 2015 5:52 pm   Post subject: While loop repeats even if conditions are met

What the title says. Here's my code:
Java:

System.out.println("Enter your payment method (supported: Visa, Mastercard, Paypal, cash");
        String payment = input.next();
        do
        {
          System.out.println("Invalid payment method.");
          System.out.println("Enter your payment method (supported: Visa, Mastercard, Paypal, cash");
          payment = input.next();
        } while(!payment.equals("visa")||!payment.equals("Visa")||!payment.equals("mastercard")||!payment.equals("Mastercard")||!payment.equals("paypal")||!payment.equals("Paypal")||!payment.equals("cash")||!payment.equals("Cash"));
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Mon Nov 16, 2015 7:31 pm   Post subject: RE:While loop repeats even if conditions are met

Use payment.equalsIgnoreCase("visa") instead of doing 2^(lengthOfString) comparisions to check all possibilities.

do {} while(); loops will always run once. It will not check the exit condition until the end of the first iteration.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: