0

I have a problem with inputs in the code. In the program I want to create a menu which allows us to put shapes in it. In the first option when I write square, circle or rectangle the program should ask me side, radius or height and width. However when I write the kind of the shape the program goes back to menu as nothing happened. Could you help?

  import java.util.*; 

  public class test
  {
     public static void main(String[] args) 
     {
         Scanner scan = new Scanner(System.in);
         Scanner scan2 = new Scanner(System.in);
         String kindofshape;
         int choice, radius, width, height, side;

         do
         {
            System.out.println("(1) Add a shape to the list");
            System.out.println("(2) Get the total area of the shapes");
            System.out.println("(3) Show information of the shapes");
            System.out.println("(4) Quit");
            System.out.println("Choice : ");
            choice = scan.nextInt();

            if (choice == 1)
            {
                System.out.println("enter the type of the shape");
                kindofshape = scan2.nextLine();
                if(kindofshape == "circle")
                {
                    System.out.println("enter the radius");
                    radius = scan.nextInt();              
                }
                if(kindofshape == "rectangle")
                {
                    System.out.println("enter the width");
                    width = scan.nextInt();
                    System.out.println("enter the height");
                    height = scan.nextInt();   
                }
                if(kindofshape == "square")
                {
                    System.out.println("enter the width");
                    side = scan.nextInt(); 
                }
            }

        }while(choice != 4);

      }
  }
yole
  • 92,896
  • 20
  • 260
  • 197
harold_finch
  • 80
  • 3
  • 11
  • 1
    `== "circle"` [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Mar 22 '15 at 15:22

1 Answers1

2

This is Java. You should compare string with .equals() but not with ==. If you compare strings with == you comparing instances but not string values. And please, use switch against if-else constructions.

RussianVodka
  • 450
  • 1
  • 6
  • 18