1

This is the code.

do {
    System.out.println("Enter Product Code: ");
    medicine = machine1.nextInt();

    if (medicine==1) {
        machine1.buy1();
        machine1.addproduct(); 
    } 
    if (medicine==2); {
        machine1.buy2();
        machine1.addproduct();
    } 
} while((String answer=='y') || (String answer=='Y'));

And it gives this error:

enter code    drugstore1.java:96: error: ')' expected
            }while((String answer=='y'))||((String answer=='Y'));
                                 ^    drugstore1.java:96: error: ';' expected
            }while((String answer=='y'))||((String answer=='Y'));
                                       ^  drugstore1.java:96: error: ';' expected
            }while((String answer=='y'))||((String answer=='Y'));
                                                          ^

It poof'ed after fixing other errors. I think I'm using it right.. But what is wrong?

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
John
  • 47
  • 6

5 Answers5

5

I suggest you to write

while(answer.equalsIgnoreCase("y"));

intead of

while((String answer=='y'))||((String answer=='Y'));

You cannot define new variables inside logical conditions.

1. String answer [X] -> answer [V]
2. answer=='y' [X] -> answer.equals("y") [V]
3. answer.equals("y") || answer.equals("Y") [V] -> answer.equalsIgnoreCase("y") [V]

4. if(medicine==2); [X] -> if (medicine == 2) [V]
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
4

Try to use double quote and equalsignorecase method in all you while condition like this:

  while(answer.equalsignorecase("y"));

Before try to use nextLine() when you want to insert string "y" with creating variable:

  String answer= machine1.nextLine();

Like this:

  System.out.println("Enter Product Code: ");
    medicine = machine1.nextInt();
  System.out.println("Enter y if you want leave: ");
    String answer= machine1.nextLine();
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
  • @John see the additionale answer – Abdelhak Mar 11 '16 at 15:43
  • Yes, that is what I'm using... I've minimized it to 12 errors and now it's saying error: ";" expected whichis pointing to the parenthesis next to my void. – John Mar 11 '16 at 15:51
  • @John if you want try to use this:while((String answer=='y')||(String answer=='Y')); you should remove the second parentesis – Abdelhak Mar 11 '16 at 17:29
2

Here are some guidelines:

  • Do not declare objects inside while() statements. Did you mean while(answer=="y")?

  • Do not compare Strings using the == operator. Strings are compared with .equals(). The == only checks if the two operands are at the same physical memory location.

2

You're trying to declare variables inside your condition:

String answer=='y'

How would a variable declaration be usable as a comparison?

Where should answer be coming from? It doesn't exist anywhere in your code, so it's not clear at all what you're trying to accomplish there.

But once you do have an answer variable in your code which potentially contains some 'y' or 'Y' value, you can compare it like this:

while(answer.equalsIgnoreCase("y"))

Additionally, you have a bug here:

if(medicine==2);

That semi-colon is going to silently terminate the if block, and the following block of code will always execute regardless of the condition. Remove that semi-colon.

David
  • 208,112
  • 36
  • 198
  • 279
2

it should be

while("y".equalsIgnoreCase(answer)){// will avoid null pointer exception if answer is null

 if(medicine==1) 
            {
                machine1.buy1();
                machine1.addproduct(); 
            } 
        else if(medicine==2) // better for performance as both if conditions are mutually exclusive
            {
                machine1.buy2();
                machine1.addproduct();
            } 

}
M Sach
  • 33,416
  • 76
  • 221
  • 314