0

i had try enter other thing that not related to what i want and it will prompt the else if statement ask me enter again ..but why when i enter the correct thing it still ask me to choose again ??? why ??

here is part of my code :

 public static void choose()
{

    Scanner read=new Scanner(System.in);
    String shape = "";


    do{

    System.out.println("which shape you would like to choose");
    shape=read.nextLine();     
    if(shape.equals("rectangle"))
    {
        System.out.println("enter width");
         width=Double.parseDouble(read.nextLine());
        System.out.println("enter length");
        length=Double.parseDouble(read.nextLine());
        System.out.println("enter color");
       String color = read.nextLine();


    }
    else if (shape.equals("box"))
    {
        System.out.println("enter width");
         width=Double.parseDouble(read.nextLine());
        System.out.println("enter length");
        length=Double.parseDouble(read.nextLine());
        System.out.println("enter height");
        height=Double.parseDouble(read.nextLine());
        System.out.println("enter color");
        String color = read.nextLine();


    }
    else 
    {
        System.out.println("please enter only rectangle and box");

    }

    }while((shape !="rectangle" && shape !="box"));

here my run :

which shape you would like to choose
abc
please enter only rectangle and box
which shape you would like to choose
box
enter width
9
enter length
8
enter height
8
 enter color
  blue
 which shape you would like to choose
user130875
  • 93
  • 1
  • 10

3 Answers3

1

Change

shape !="rectangle" && shape !="box"

to

!shape.equals("rectangle") && !shape.equals("box")

for the same reason for which you are using it inside your if condition.

Naman
  • 27,789
  • 26
  • 218
  • 353
1

You must use equals method in your loop condition not the operator !=. So the correct version is:

} while(!"rectangle".equals(shape) && !"box".equals(shape));
Mehdi Javan
  • 1,081
  • 6
  • 25
0

Your test in the while statement is not correct as said by others.

But you can remove it by adding break; at the end of each blocks (except the one that ask to input again) :

do{

System.out.println("which shape you would like to choose");
shape=read.nextLine();     
if(shape.equals("rectangle"))
{
    System.out.println("enter width");
     width=Double.parseDouble(read.nextLine());
    System.out.println("enter length");
    length=Double.parseDouble(read.nextLine());
    System.out.println("enter color");
   String color = read.nextLine();

    break; // Exit loop here
}
else if (shape.equals("box"))
{
    System.out.println("enter width");
    width=Double.parseDouble(read.nextLine());
    System.out.println("enter length");
    length=Double.parseDouble(read.nextLine());
    System.out.println("enter height");
    height=Double.parseDouble(read.nextLine());
    System.out.println("enter color");
    String color = read.nextLine();

    break; // Exit loop here
}
else 
{
    System.out.println("please enter only rectangle and box");

}

}while(true);

If you have few cases and/or the test is time consuming, this is a viable alternative since you are testing each value only once.

Xvolks
  • 2,065
  • 1
  • 21
  • 32