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 3 of 3

Thread: runs once only

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

    Default runs once only

    hi I am building a program but when it does a job and returns to menu it wont accept any input like the scanner was not initialised can anyone see why not it will do it once but as soon as it returns to menu it stops second set of eyes would be much appreciated

    //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();


    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.");
    break;
    }
    menu();
    }
    else if (preference == 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 estination(if no destination enter NONE): ");
    dest[0] = input.next();
    }
    else if (tNo ==2)
    {
    System.out.print("Please enter taxi2 destination(if no destination enter NONE): ");
    dest[1] = input.next();
    }
    else if (tNo ==3)
    {
    System.out.print("Please enter taxi3 destination(if no destination enter NONE): ");
    dest[2] = input.next();
    } //else instruction at end giving invalid input for anything other
    else
    System.out.println("Invalid entry please restart");

    }

    else
    System.out.println("end program");
    menu();
    }
    }


  2. #2
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: runs once only

    well idk what was wrong , but when i was messing around with the code i some how fixed it . i also changed how u have the select taxi number . cause when i first ran though it w/o really reading it , i put a name were it needed the number and just blew up XP , so its now a little different

    package test;
     
    //This program will provide a menu system for inputting
    //data regards 3 taxi's
    import java.util.Scanner;//importing the scanner utility
     
    public class c22taxis {
    	static int preference;
    	static Scanner input = new Scanner(System.in);
    	static String[] name = new String[3];
    	static String[] dest = new String[3];
    	static int tNo = 0;
     
    	public static void main(String[] args) {
    		menu();// calling the method /main menu
    	}
     
    	public static void menu(){
     
    		// 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: ");
    		mainAsk();
    	}
    	public static void mainAsk(){
    		preference = input.nextInt();
     
    		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: ");
     
    			String tNo2 = input.next();    // Changed so that if some1 puts in a non-int 
    			if (tNo2.contains("1") || tNo2.contains("2") || tNo2.contains("3")){
    				tNo = Integer.parseInt(tNo2);
    			} else {
    				tNo = 0;
    			}
     
    			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.");
    				break;
    			}
    			menu();
    		} else if (preference == 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: ");
     
    			String tNo2 = input.next();    // Changed so that if some1 puts in a non-int 
    			if (tNo2.contains("1") || tNo2.contains("2") || tNo2.contains("3")){
    				tNo = Integer.parseInt(tNo2);
    			} else {
    				tNo = 0;
    			}
     
    			if (tNo == 1) {
    				System.out
    						.print("Please enter taxi1 destination(if no destination enter NONE): ");
    				dest[0] = input.next();
    			} else if (tNo == 2) {
    				System.out
    						.print("Please enter taxi2 destination(if no destination enter NONE): ");
    				dest[1] = input.next();
    			} else if (tNo == 3) {
    				System.out
    						.print("Please enter taxi3 destination(if no destination enter NONE): ");
    				dest[2] = input.next();
    			} // else instruction at end giving invalid input for anything other
    			else
    				System.out.println("Invalid entry please restart");
    				menu();
    		}
     
    		else
    			System.out.println("end program");
    			System.exit(0);
    	}
    }

    i only changed a few things , but some things i would change if i hadn't got it to work so easily was change the long if else on the main menu to a case , and have each case call a method for it .

    switch (preference) {
       case 1:
          this.changeName();
       ....
    }
     
    private void changeName(){
        the actions to change the name 
    }


    also , in future posts , please use the code tags . the button to auto add them is # ,then just paste the code between them
    Programming: the art that fights back

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

    Default Re: runs once only

    so when adding a third submenu can i just copy and paste submenu 2 and change where it stores the information entered eg another array.

    And big thanks to i spent nearly 2 hours yesterday and was considering head banging as a new hobby