5 Replies - 9740 Views - Last Post: 15 December 2010 - 03:23 PM Rate Topic: -----

#1 sunboy_08282  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 01-November 10

switch with do - while loop

Posted 13 December 2010 - 11:00 PM

I tried to create a menu file, and I used a switch method instead of if-else. I put it in do while loop, but when I run that, it is like running an infinite loop. I can't understand where is the problem.
tx

 public static void main(String[] args) {

        //pass in the cmd args as parameters for the Picture class
        aPicture = new Picture(Integer.parseInt(args[0]), Integer.parseInt(args[0]));

        Scanner kbEntry = new Scanner(System.in);
        //  Menu();
        String input = kbEntry.next();
        char menuOptions = input.charAt(0);

        do {
            System.out.println("'S' - To add a Square ");
            System.out.println("'L' - To add a Line");
            System.out.println("'U' - To remove last added item");
            System.out.println("'D' - To load a drawing from a file ");
            System.out.println("'V' - To save a drawing to a file ");
            System.out.println("'N' - New Picture ");
            System.out.println("'Q' - Quit:\n");

            switch (menuOptions) {
                case 'S':
                    // Picture aPicture;
                    System.out.println("Enter the value of x: ");
                    int x = kbEntry.nextInt();
                    System.out.println("Enter the value of y: ");
                    int y = kbEntry.nextInt();
                    System.out.println("Enter the value of z: ");
                    int z = kbEntry.nextInt();
                    //  Picture.class.getMethod(addSquare(x,y,z),keyboardEntry.nextInt());
                    aPicture.addSquare(x, y, z);
                    break;

                case 'L':
                    System.out.println("Enter the value of x1: ");
                    int x1 = kbEntry.nextInt();
                    System.out.println("Enter the value of y1: ");
                    int y1 = kbEntry.nextInt();
                    System.out.println("Enter the value of x2: ");
                    int x2 = kbEntry.nextInt();
                    System.out.println("Enter the value of y2: ");
                    int y2 = kbEntry.nextInt();
                    aPicture.addLine(x1, y1, x2, y2);
                    break;
                case 'U':
                    aPicture.undo();
                    break;
                case 'D':
                    System.out.println("Enter the Filename: ");
                    try {
                        aPicture.load(kbEntry.next());
                    } catch (Exception e) {
                        System.out.println("This file does not exist !!!");
                    }
                    break;
                case 'V':
                    System.out.println("Enter the Filename: ");
                    aPicture.save(kbEntry.next());
                    break;
                case 'N':
                    aPicture = new Picture(Integer.parseInt(args[0]), Integer.parseInt(args[0]));
                    break;
                case 'Q':
                    break;
                    
                    default: System.out.println("You Entered wrong menuOption");
            }
        } while (menuOptions != 'Q');
}



Is This A Good Question/Topic? 0
  • +

Replies To: switch with do - while loop

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: switch with do - while loop

Posted 13 December 2010 - 11:54 PM

Your

   String input = kbEntry.next();  
   char menuOptions = input.charAt(0);  


will have to be just after your do { statement
otherwise you will loop for ever on the first menuOptions selected

and obviously after all your System.out.println() that display the menu
Was This Post Helpful? 0
  • +
  • -

#3 sunboy_08282  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 01-November 10

Re: switch with do - while loop

Posted 15 December 2010 - 04:00 AM

View Postpbl, on 14 December 2010 - 05:54 AM, said:

Your

   String input = kbEntry.next();  
   char menuOptions = input.charAt(0);  


will have to be just after your do { statement
otherwise you will loop for ever on the first menuOptions selected

and obviously after all your System.out.println() that display the menu


tx, the solution was so easy and simple, I should be more careful, I change the part, but now I have another problem, the program doesn't run. it says there is a problem with while condition, and it can not be applied to variable 'menuOptions'.

    public static void main(String[] args) {

        //pass in the cmd args as parameters for the Picture class
        aPicture = new Picture(Integer.parseInt(args[0]), Integer.parseInt(args[0]));

        Scanner kbEntry = new Scanner(System.in);
        //  Menu();


        do {
            System.out.println("'S' - To add a Square ");
            System.out.println("'L' - To add a Line");
            System.out.println("'U' - To remove last added item");
            System.out.println("'D' - To load a drawing from a file ");
            System.out.println("'V' - To save a drawing to a file ");
            System.out.println("'N' - New Picture ");
            System.out.println("'Q' - Quit:\n");

            String input = kbEntry.next();
            char menuOptions = input.charAt(0);
            switch (menuOptions) {
                case 'S':
                    // Picture aPicture;
                    System.out.println("Enter the value of x: ");
                    int x = kbEntry.nextInt();
                    System.out.println("Enter the value of y: ");
                    int y = kbEntry.nextInt();
                    System.out.println("Enter the value of z: ");
                    int z = kbEntry.nextInt();
                    //  Picture.class.getMethod(addSquare(x,y,z),keyboardEntry.nextInt());
                    aPicture.addSquare(x, y, z);
                    break;

                case 'L':
                    System.out.println("Enter the value of x1: ");
                    int x1 = kbEntry.nextInt();
                    System.out.println("Enter the value of y1: ");
                    int y1 = kbEntry.nextInt();
                    System.out.println("Enter the value of x2: ");
                    int x2 = kbEntry.nextInt();
                    System.out.println("Enter the value of y2: ");
                    int y2 = kbEntry.nextInt();
                    aPicture.addLine(x1, y1, x2, y2);
                    break;
                case 'U':
                    aPicture.undo();
                    break;
                case 'D':
                    System.out.println("Enter the Filename: ");
                    try {
                        aPicture.load(kbEntry.next());
                    } catch (Exception e) {
                        System.out.println("This file does not exist !!!");
                    }
                    break;
                case 'V':
                    System.out.println("Enter the Filename: ");
                    aPicture.save(kbEntry.next());
                    break;
                case 'N':
                    System.out.println("Enter new window width");
                    int w = kbEntry.nextInt();
                    System.out.println("Enter new window height");
                    int h = kbEntry.nextInt();
                    aPicture = new Picture(w, h);
                    break;
                case 'Q':
                    break;

                default:
                    System.out.println("You Entered wrong menuOption");
            }
        } while (menuOptions != 'Q');
}


Was This Post Helpful? 0
  • +
  • -

#4 mufasa  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 44
  • Joined: 12-February 10

Re: switch with do - while loop

Posted 15 December 2010 - 09:58 AM

it's having an error because menuOptions is defined inside the do statement's body and so it's scope is limited to this area. define the char menuOptions before the do-while loop (i.e. put "char menuOptions;" right before the do-while loop and remove the char definition from before menuOptions in the statement "char menuOptions = input.charAt(0);"
Was This Post Helpful? 0
  • +
  • -

#5 sunboy_08282  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 01-November 10

Re: switch with do - while loop

Posted 15 December 2010 - 12:05 PM

View Postmufasa, on 15 December 2010 - 03:58 PM, said:

it's having an error because menuOptions is defined inside the do statement's body and so it's scope is limited to this area. define the char menuOptions before the do-while loop (i.e. put "char menuOptions;" right before the do-while loop and remove the char definition from before menuOptions in the statement "char menuOptions = input.charAt(0);"


Tx a lot guys, I apply that, but the problem that it has, and I can't find out the reason is the while condition, the while condition defines that the do loop repeated till the menuOptions is equal to 'Q' which means that if the user types Q , the program should quite, but it doesn't work, when I enter Q the program just think and think and nothing happens.

    public static void main(String[] args) {

        //pass in the cmd args as parameters for the Picture class
        aPicture = new Picture(Integer.parseInt(args[0]), Integer.parseInt(args[0]));

        Scanner kbEntry = new Scanner(System.in);
        //  Menu();

        char menuOptions = 'A';
        do {
            System.out.println("'S' - To add a Square ");
            System.out.println("'L' - To add a Line");
            System.out.println("'U' - To remove last added item");
            System.out.println("'D' - To load a drawing from a file ");
            System.out.println("'V' - To save a drawing to a file ");
            System.out.println("'N' - New Picture ");
            System.out.println("'Q' - Quit:\n");

            String input = kbEntry.next();
            menuOptions = input.charAt(0);
            switch (menuOptions) {
                case 'S':
                    // Picture aPicture;
                    System.out.println("Enter the value of x: ");
                    int x = kbEntry.nextInt();
                    System.out.println("Enter the value of y: ");
                    int y = kbEntry.nextInt();
                    System.out.println("Enter the value of z: ");
                    int z = kbEntry.nextInt();
                    //  Picture.class.getMethod(addSquare(x,y,z),keyboardEntry.nextInt());
                    aPicture.addSquare(x, y, z);
                    break;

                case 'L':
                    System.out.println("Enter the value of x1: ");
                    int x1 = kbEntry.nextInt();
                    System.out.println("Enter the value of y1: ");
                    int y1 = kbEntry.nextInt();
                    System.out.println("Enter the value of x2: ");
                    int x2 = kbEntry.nextInt();
                    System.out.println("Enter the value of y2: ");
                    int y2 = kbEntry.nextInt();
                    aPicture.addLine(x1, y1, x2, y2);
                    break;
                case 'U':
                    aPicture.undo();
                    break;
                case 'D':
                    System.out.println("Enter the Filename: ");
                    try {
                        aPicture.load(kbEntry.next());
                    } catch (Exception e) {
                        System.out.println("This file does not exist !!!");
                    }
                    break;
                case 'V':
                    System.out.println("Enter the Filename: ");
                    aPicture.save(kbEntry.next());
                    break;
                case 'N':
                    System.out.println("Enter new window width");
                    int w = kbEntry.nextInt();
                    System.out.println("Enter new window height");
                    int h = kbEntry.nextInt();
                    aPicture = new Picture(w, h);
                    break;
                case 'Q':
                    break;

                default:
                    System.out.println("You Entered wrong menuOption");
            }
        } while (menuOptions != 'Q');
}


This post has been edited by sunboy_08282: 15 December 2010 - 12:12 PM

Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2860
  • View blog
  • Posts: 10,964
  • Joined: 15-July 08

Re: switch with do - while loop

Posted 15 December 2010 - 03:23 PM

It worked for me! Note that you have to use a capital 'Q' and not 'q' in order for it to work:

'S' - To add a Square 
'L' - To add a Line
'U' - To remove last added item
'D' - To load a drawing from a file 
'V' - To save a drawing to a file 
'N' - New Picture 
'Q' - Quit:

q
You Entered wrong menuOption
'S' - To add a Square 
'L' - To add a Line
'U' - To remove last added item
'D' - To load a drawing from a file 
'V' - To save a drawing to a file 
'N' - New Picture 
'Q' - Quit:

Q
[Program Ended]


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1