开发者

Basic JAVA console based menu problem

I am trying to make a basic java console based menu system. I have managed to get the first tier working fine:

public static int menu(){

    //Displays the main menu and handles passing off to next menu. 

      Scanner scanner = new Scanner (System.in);
      int selection=0;
      String CDid;
      int i=0;
      int j=0;

      while(i==0) {   //changed while(1) to a value that didn't complain in netbeans
          System.out.println("Please choose an option from the following:"); 
          System.out.println("[1] Search by CD ID"); 
          System.out.println("[2] View all CD's in store"); 
          System.out.println("[3] Quit"); 
          System.out.println("Choice: "); 
          selection=scanner.nextInt();     

     switch (selection){

         case 1:System.out.println("Please enter the CD ID:");
              i=1;
              break;       

         case 2:System.out.println("List all CD's");
              i=1;
              break;

         case 3:System.out.println("Quiting...");
              System.exit(5);

         default:System.out.println("Your choice was not valid!");

          };

      }  
      return selection;
  }

I am now trying to make it so that when you choose option [1] it takes the next string you input and runs the showCD() method. I have tried the method I used before but as it's a string I am hitting some errors.

  public static int menu(){
        //Displays the main menu and handles passing off to next menu. 

          Scanner scanner = new Scanner (System.in);
          int selection=0;
          String CDid;
          int i=0;
          int j=0;

          while(i==0) {   //changed while(1) to a value that didn't complain in netbeans
              System.out.println("Please choose an option from the following:"); 
              System.out.println("[1] Search by CD ID"); 
              System.out.println("[2] View all CD's in store"); 
              System.out.println("[3] Quit"); 
              System.out.println("Choice: "); 
              selection=scanner.nextInt();     

         switch (selection){

             case 1:System.out.println("Please enter the CD ID:");
                    CDid=scanner.toString(); 
     开发者_如何学运维                 while(j==0) { 
                         switch (CDid){
                         case 1:showCD(CDid);
                         j=1;
                         break;

                         default:System.out.println("Your choice was not valid!");

                         }
                      }

                  i=1;
                  break;       

             case 2:System.out.println("List all CD's");
                  i=1;
                  break;

             case 3:System.out.println("Quiting...");
                  System.exit(5);

             default:System.out.println("Your choice was not valid!");

              };

          }  
          return selection;
      }


Sounds like homework (you should tag it as such). You're trying to get the next String of user input but your code is calling scanner.toString(). This will merely print out the String representation of the Scanner object. You may want to use scanner.nextLine() instead. Take a look at this for reference.

EDIT: Also, just reading further through your code, you're then trying to switch on CDid which is a String. You can't switch on a String in Java (yet if you're <= 1.6). You would need an if-else block here instead.


change: CDid=scanner.toString();

to: CDid=scanner.next();

I am not so sure that you can make a switch with a String... if you can't parse CDid to Integer with Integer.parseInt() method

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜