Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: loop or what

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile loop or what

    Hi all witing a program and need help where you see<<<<<<<<< I need the program to return to the main menu or when it has done its job in that area i.e enter taxi number enter name then it needs to return to main menu but cant think how woul get a FOR or While loop to do that as you can see the main menu has 4 instances of sub menu and as they are completed it will need to return to main menu. at moment if i enter data in ie taxi number and name program ends which with the code below is right but it needs to loop back from each sub menu

    Thanks in advance for help
    //This program will provide a menu system for inputting
    //data regards 3 taxi's
    import java.util.Scanner;//importing the scanner utility 
     
     
    class c22taxis
    {
        public static void main (String[]args)
        {
            //initialising the scanner
            Scanner input = new Scanner(System.in);
     
            //creating/initialising the variables
            String taxi1, taxi2,taxi3;
            String [] name = new String[3];
            String [] Status = new String [3];
            int choice = 0;
            int tNo = 0;
            //array variable for changing driver names
            //i.e. driver 1 would be name[0]
     
     
            //printing of the system menu on screen to user
            System.out.println("Welcome to C22 Taxis automated System");
            System.out.println("");// blank line
            System.out.println("Please select from a option below to continue.");
            System.out.println("");// blank line
            System.out.println("    1. Enter/Change Driver Name.");
            System.out.println("");// blank line
            System.out.println("    2. Enter/Change veichle booking status.");
            System.out.println("");// blank line
            System.out.println("    3. Enter/Change veichle service status.");
            System.out.println("");// blank line
            System.out.println("    4. List all veichle status");
            System.out.println("");// blank line
            System.out.println("    5. Exit");//exit command for program
            System.out.println("");
            System.out.print("Please select a option:  ");
            choice = input.nextInt();
     
                  if (choice == 1)// path of input from user
                  {
                  System.out.println('\f');
                  // sub menu for driver change details
                  System.out.println("       Driver name sub Menu");
                  System.out.println("");
                  System.out.println("");
                  System.out.print("Please enter taxi number: ");
                  tNo = input.nextInt();//instruction for user to input number
                    //if/else relating to choice made in sub menu will then prompt user for tsxi no. and then name 
                    //name then goes in relevant array position.
     
                    if (tNo == 1)
                    {
                    System.out.print("Please enter drivers name: ");
                    name[0] = input.next();
                }
                    else if (tNo ==2)
                    {
                    System.out.print("Please enter drivers name: ");
                    name[1] = input.next();
                }
                    else if (tNo ==3)
                    {
                    System.out.print("Please enter drivers name: ");
                    name[2] = input.next();
                }   //else instruction at end giving invalid input for anything other
                    else
                    System.out.println("Invalid entry please restart");<<<<<<<<<
     
                }
                 else if (choice == 2)
                 System.out.println('\f');
                 //sub menu start for taxi status
                 System.out.println("       Taxi status sub Menu");
                  System.out.println("");
                  System.out.println("");
                  System.out.print("Please enter taxi number: ");
                  tNo = input.nextInt();//instruction for user to input number
                    //if/else relating to choice made in sub menu will then prompt user for taxi status 
                    //status will then go in relevant array position.
     
                    if (tNo == 1)
                    {
                    System.out.print("Please enter taxi1 status(if no status enter NONE): ");
                    Status[0] = input.next();
                }
                    else if (tNo ==2)
                    {
                    System.out.print("Please enter taxi1 status(if no status enter NONE): ");
                    Status[1] = input.next();
                }
                    else if (tNo ==3)
                    {
                    System.out.print("Please enter taxi1 status(if no status enter NONE): ");
                    Status[2] = input.next();
                }   //else instruction at end giving invalid input for anything other
                    else
                    System.out.println("Invalid entry please restart");<<<<<<<<<<<   
     
     
     
        }
    }
    Last edited by helloworld922; November 16th, 2009 at 03:05 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,294
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: loop or what

    Separate the code in the main method into a separate function and call that function from the main method. This structure allows you to call that method whenever you wish to return the main menu. In doing this, you may wish to instantiate the class c22taxis which will allow you to use class variables for things like the Scanner so you don't have to create new ones.
    Last edited by copeg; November 16th, 2009 at 02:09 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: loop or what

    see thats were you lost me can you give me a example

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,294
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: loop or what

    Something like this

    class c22taxis
    {
        Scanner input = new Scanner(System.in);
        ///Place other reusable variables here. 
     
        public c22taxis(){}
     
        public void getUserInput()
        {
            //main menu code and processing here
            //Call this function again should user input be invalid and/or you wish to go back to main menu 
             if ( /**Invalid input */ )
            {
                 getUserInput();//<<<<<<<<<
             }
     
        }
     
        public static void main (String[]args)
        {
            c22taxis c2 = new c22taxis();
            c2.getUserInput();
        }
    }

  5. #5
    Junior Member
    Join Date
    Oct 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: loop or what

    so do i need i method containing the main menu or do i need a method for each menu.

    sorry to ask but this is a assignment and we not covered methods but been told we know all we need to complete assignment but we get extra marks for added functionality

    learnt arrays, loops(for and while and dowhile), else,if , double, string, int.

    is this possible in a loop or while loop as well. you know its just distinction sounds much better than pass

  6. #6
    Junior Member
    Join Date
    Oct 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: loop or what

    ok changed the program around using methods like you suggested but for some reason it wont loop back round to main menu can anyone see why all it keeps doing is running the sub menu and if i place loop further up it just runs but no window opens

    //This program will provide a menu system for inputting
    //data regards 3 taxi's
    import java.util.Scanner;//importing the scanner utility 
     
    class c22taxis
    {
       public static void menu() //This method will be called by the main method later
       {
            //printing of the system menu on screen to user
            System.out.println("Welcome to C22 Taxis automated System");
            System.out.println("");// blank line
            System.out.println("Please select from a option below to continue.");
            System.out.println("");// blank line
            System.out.println("    1. Enter/Change Driver Name.");
            System.out.println("");// blank line
            System.out.println("    2. Enter/Change veichle booking status.");
            System.out.println("");// blank line
            System.out.println("    3. Enter/Change veichle service status.");
            System.out.println("");// blank line
            System.out.println("    4. List all veichle status");
            System.out.println("");// blank line
            System.out.println("    5. Exit");//exit command for program
            System.out.println("");
            System.out.print("Please select a option:  ");
        }    
        public static void main (String[]args)
        {
            //initialising the scanner
            Scanner input = new Scanner(System.in);
     
            //creating/initialising the variables
            String taxi1, taxi2,taxi3;
            String [] name = new String[3];//name array useable upto as many names as required
            String [] dest = new String [3];//array for destination again as many taxis as required
            int choice = 0, preference;
            int tNo = 0;
     
     
            menu();//calling the method /main menu
     
            preference = input.nextInt();
     
               while (preference !=5)
               {
                  if (preference == 1)// path of input from user
                  {
                  System.out.println('\f');
                  // sub menu for driver change details
                  System.out.println("       Driver name sub Menu");
                  System.out.println("");
                  System.out.println("");
                  System.out.print("Please enter taxi number: ");
                  tNo = input.nextInt();//instruction for user to input number
                    //if/else relating to choice made in sub menu will then prompt user for tsxi no. and then name 
                    //name then goes in relevant array position.
     
                    switch (tNo)
                    {
                    case 1:
                        System.out.print("Please enter drivers name: ");
                        name[0] = input.next();
                        break;
                    case 2:
                        System.out.print("Please enter drivers name: ");
                        name[1] = input.next();
                        break;
     
                    case 3: 
                        System.out.print("Please enter drivers name: ");
                        name[2] = input.next();
                        break;
     
                        //the default
                    default:
                        System.out.print("invalid entry return to menu.");
                        menu();
                        break;
     
                }
            }
    Last edited by helloworld922; November 19th, 2009 at 07:37 PM. Reason: Please use [code] tags!

Similar Threads

  1. Need help with loop
    By SwEeTAcTioN in forum Loops & Control Statements
    Replies: 8
    Last Post: October 25th, 2009, 05:59 PM
  2. literal Loop
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 10th, 2009, 10:03 PM
  3. can any one do this in a loop? (for loop)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: October 4th, 2009, 08:31 PM
  4. For loop tricks
    By helloworld922 in forum Java Programming Tutorials
    Replies: 0
    Last Post: October 4th, 2009, 01:09 AM
  5. [SOLVED] Looping of particular instruction ith times
    By lotus in forum Loops & Control Statements
    Replies: 2
    Last Post: July 12th, 2009, 11:47 PM