Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Nilla

Thread: Switch menu problem

  1. #1
    Nilla is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Switch menu problem

    I have a menusystem with several levels and I have several methods connected to the menu. Everything works fine - but only when I go through the menu from the beginning. When I go backwards in the menu to for example start a new customer a strange problem occur. I can´t put in the first name. The program skip that choice and go direct to the second name. The same problem appears with other methods when I go backwards in the menu. Here is the code:

    Java Code:
    public class Bank
    {
    
    	SavingAccounts konto = new SavingAccounts();
    	List <Customer> customers = new ArrayList();
    	List<SavingAccounts> accounts = new ArrayList(customers);
    	Scanner scanner = new Scanner(System.in);
    	
    	private int showMenu(String title, String[] items)
    	{
    		System.out.println(title);
    		for(int i = 0; i < items.length; i++)
    		{
    			System.out.println(items[i]);
    		}
    		System.out.print("\nVälj i menyn: ");
    		Scanner scan = new Scanner(System.in);
    		return scan.nextInt();
    	}
    
    	public void menu()
    	{
    		String   menuTitle = "\n### Wellcome to the bank###\n";
    		String[] menuItems = {"1. Skriv ut kunder",
    						"2. Create a customer",
    						"3. Choose customer",
    						"0. Exit"};
    		while(true)
    		{
    			{
    				case 1: showCustomers();
    					break;
    
    				case 2: createCustomer();
    					break;
    			
    				case 3: 	chooseCustomer();
    					break;
    
    				case 0:	System.out.println("Avsluta programmet...");
    					return;
    			}
    		}
    }
    	public void customerMenu(String kundNummer)
    	{
    		String menuTitle = "\n### What di you want to do?###\n";
    		String[] menuItems = {"1. Show customers and accounts",
    				      "2. Change name",
    				      "3. Create an Saving account",
    				      "4. Create an Cerdit Account",
    				      "5. Choose an account",
    				      "6. Delte the cusotmer",
    				      "0. Back to main menu"};
    
    		while(true) 
    		{
    			int input = showMenu(menuTitle, menuItems);
    			switch(input)
    			{
    				case 1:	showCustomers(kundNummer);
    					break;
    					
    				case 2: 	changeCustomerName(kundNummer);
    					break;
    
    				case 3: 	createAccount(kundNummer);
    					break;	
    					
    				case 4: 
    					System.out.println("Ännu inte i bruk.");
    					break;
    
    				
    				case 5: 	chooseAccount(kundNummer);
    					break;
    			
    				case 6: 
    					deleteCustomer(kundNummer);
    																
    				case 0:	System.out.println("Back to main menu...");
    					return;
    			}
    		}
    }
    	public void accountMenu(String kundNummer, int kontoNummer)
    	{
    		String header = "\n### Handle your accounts###\n";
    		String[] menuItems = {"1. Show information",
    				      "2. Deposit",
    				      "3. Withdrawel",
    				      "4. Close the accountot",
    				      "0. Back to customer menu"};
    
    		while(true)
    		{
    			int input = showMenu(header, menuItems);
    			switch(input)
    			{
    				case 1: 	showCustomerAccounts(kundNummer, kontoNummer);
    					break;
    
    				case 2: 	depositMoney(kundNummer, kontoNummer);
    					break;
    
    				case 3: 	withdrawMoney(kundNummer, kontoNummer);
    					break;
    				
    				case 4:	closeAccount(kundNummer, kontoNummer);
    							
    				case 0:	System.out.println("\Back to customer menu...");
    					return;
    			}
    		}
    	}
    Here is the code for the createCustomer method

    Java Code:
    public void createCustomer(){
    		System.out.println("Skriv in kunduppgifter.");
    		System.out.println("Förnamn: ");
    		String aFirstName = scanner.nextLine();
    		System.out.println("Efternamn: ");
    		String aSecondName = scanner.nextLine();
    		System.out.println("Personnummer: ");
    		String aPNumber = scanner.nextLine();
    		
    		if(customers.isEmpty()){
    			Customer aCustomer = new Customer(aFirstName, aSecondName, aPNumber);
    			customers.add(aCustomer);
    		}
    		
    		else if(!(customers.isEmpty())){
    			for(int i=0;i<customers.size();i++){ 
    				if(!(customers.get(i).getPNumber().equals(aPNumber))){ 
    					Customer aCustomer = new Customer(aFirstName, aSecondName, aPNumber);
    		        	customers.add(aCustomer);
    		        	System.out.println("Kunden är registrerad!"); 
    		        	break;
    				} 
    		        else  {
    		        	System.out.println("There is already a customer with this person number."); 
    				}
    			}
    		}
    	}
    What´s wrong? How can I change the menu to get it work??

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Switch menu problem

    :yawn:

    Scanner#nextInt() (and many other of its methods) don't consume the end-of-line, which is left behind in System.in and consumed the next time a call to nextLine() is encountered.

    Using two different Scanner objects both hooked to the same input stream doesn't change that.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Nilla is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: Switch menu problem

    Thank you for the answer but I am not sure I know what you mean. I will try to put scanner.nextLine() in between. Maybe that will solve the problem.

    No, it didn´t.
    Last edited by Nilla; 08-25-2012 at 02:15 PM.

  4. #4
    Nilla is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: Switch menu problem

    I have now solved the problem. Thank you for the advice.
    DarrylBurke likes this.

Similar Threads

  1. Irritating logical error, switch statement problem
    By Luftwaffle in forum New To Java
    Replies: 3
    Last Post: 05-20-2012, 10:42 PM
  2. Scanner and Switch problem
    By Aenohe in forum New To Java
    Replies: 8
    Last Post: 12-08-2011, 01:19 AM
  3. Replies: 9
    Last Post: 09-21-2010, 04:15 PM
  4. Problem with Struts Menu
    By sdp in forum Advanced Java
    Replies: 4
    Last Post: 10-02-2009, 02:13 AM
  5. Problem with a switch statement in Java
    By baltimore in forum New To Java
    Replies: 2
    Last Post: 08-02-2007, 04:43 AM

Posting Permissions

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