Results 1 to 5 of 5
  1. #1
    popeyito18's Avatar
    popeyito18 is offline Member
    Join Date
    Jun 2011
    Location
    USA
    Posts
    17
    Rep Power
    0

    Default Using while loop in Java?

    Hi,
    I am new to the Java language and I'm still clueless on some stuff. I guess I'll be around this website for some time.

    I'm writing my very first program in Java.
    I did a while loop with some if statements within. My loop has four different options. Every option does something different, and option 4 is designed to end the program.
    The problem is that I don't know how to make the loop repeat itself until I decide to end it (by choosing option #4)?
    Since I put 4 different while(s) loops, and so the programs shuts itself after I input four different options. (independently of what options I punch in)

    This is my code.

    import java.util.Scanner; //Needed for Scanner class


    public class proyectoquefunciona
    {
    public static void main(String[] args)
    {
    String cname, caddress, cemail;
    int option1, option2, option3, option4;

    Scanner keyboard = new Scanner(System.in);
    System.out.println("Welcome to THE STORE");
    System.out.println("Please Select One of Following");
    System.out.println(" 1. Enter customer information \n 2. Price Lookup \n 3. Display Total Bill \n 4. Quit");
    option1 = keyboard.nextInt();
    {
    while (option1 != 0)
    {

    if (option1 ==1)
    { System.out.println("Good job you're in part (1)");}

    if (option1 ==2)
    {System.out.println("Good job you're in part 1.2");}

    if (option1 ==3)
    {System.out.println("Good job you're in part 1.3"); }

    if (option1==4)
    {System.exit(0);}
    break;
    }}



    System.out.println("Please Select One of Following");
    System.out.println(" 1. Enter customer information \n 2. Price Lookup \n 3. Display Total Bill \n 4. Quit");
    option2 = keyboard.nextInt();


    {
    while (option2 !=1 || option2 != 2 || option2 !=3 || option2 !=4)
    { if (option2 ==1)
    {
    System.out.println("Good job you're in part (2)");}
    if (option2 ==2)
    {System.out.println("Good job you're in part 2.2"); }
    if (option2 ==3)
    {System.out.println("Good job you're in part "); }
    if (option2==4)
    {System.exit(0);}
    break; }}


    System.out.println("Please Select One of Following");
    System.out.println(" 1. Enter customer information \n 2. Price Lookup \n 3. Display Total Bill \n 4. Quit");
    option3 = keyboard.nextInt();

    {
    while (option3 !=1 || option3 != 2 || option3 !=3 || option3 !=4)
    { if (option3 ==1)
    {
    System.out.println("Good job you're in part (33)");}
    if (option3 ==2)
    {System.out.println("Good job you're in part 33.2"); }
    if (option3 ==3)
    {System.out.println("Good job you're in part 33.3"); }
    if (option3==4)
    {System.exit(0);}
    break; }}

    System.out.println("Please Select One of Following");
    System.out.println(" 1. Enter customer information \n 2. Price Lookup \n 3. Display Total Bill \n 4. Quit");
    option4 = keyboard.nextInt();

    {
    while (option4 !=1 || option4 != 2 || option4 !=3 || option4 !=4)
    { if (option2 ==1)
    {
    System.out.println("Good job you're in part (44)");}
    if (option4 ==2)
    {System.out.println("Good job you're in part 44.2"); }
    if (option4 ==3)
    {System.out.println("Good job you're in part 44.3"); }
    if (option4==4)
    {System.exit(0);}
    break; }}

    }
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    I need to figure out what you're trying to do here: Do you want to prompt for options 1-4 four times (then repeat this cycle), or do you want to prompt for options 1-4 indefinitely until option 4 is chosen? There is a difference, most notably that with the first choice you will receive four different options variables; with the latter you will instead have only one option variable that will be handled each time.

    If you want the first, wrap a while loop around all four options that executes forever; when the program exits the loop will obviously stop. Like so:
    Java Code:
    while true {
        while loop 1 {
            pick an option1
        }
        while loop 2 {
            pick an option2
        }
        while loop 3 {
            pick an option3
        }
        while loop 4 {
            pick an option4
        }
    }
    If you want the second, ax three of your existing while loops and put a while loop around the remaining one. Like so:
    Java Code:
    while true {
        while loop 1 {
            pick an option
        }
    }

  3. #3
    popeyito18's Avatar
    popeyito18 is offline Member
    Join Date
    Jun 2011
    Location
    USA
    Posts
    17
    Rep Power
    0

    Default

    I want to be asked for options 1-4 indefinitely until option 4 is chosen.
    I will use the following code - thanks to you- but like I said, I'm very new to Java.

    while true {
    while loop 1 {
    pick an option
    }
    }
    ----------------------------------
    Where do I put the statements and the other loops for option 2,3 or 4?
    My project is based on the first chapters of the textbook and it doesn't explain the "while true" thing.
    thanks in advance

  4. #4
    popeyito18's Avatar
    popeyito18 is offline Member
    Join Date
    Jun 2011
    Location
    USA
    Posts
    17
    Rep Power
    0

    Default

    Thanks for the help Zack.
    It helped me solve the problem.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    A better approach would be to use simple switches inside a do ... while loop
    Java Code:
    do{
      get user input
      switch(user input){
        case 1:
          do stuff for option 1
          break;
        case 2:
          do stuff for option 2
          break;
        case 3:
          do stuff for option 3
          break;
      }
      if(option 4)
        break;
    } while(true);
    This will get input, do something based on input if items are 1-3, otherwise it will test if option 4 was chosen, if so it breaks out of the loop.

Similar Threads

  1. for loop in java
    By newjava in forum New To Java
    Replies: 7
    Last Post: 12-16-2009, 10:39 AM
  2. Java: While loop help
    By alfredoooddd in forum New To Java
    Replies: 3
    Last Post: 11-05-2008, 03:48 AM
  3. Help with loop in java
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:36 AM
  4. Help, loop with java
    By cachi in forum New To Java
    Replies: 5
    Last Post: 08-01-2007, 06:03 AM
  5. Enhanced For loop In Java
    By goldhouse in forum Advanced Java
    Replies: 1
    Last Post: 05-06-2007, 04:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •