Results 1 to 5 of 5
  1. #1
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default System in problem ?

    I have some code , in which when someone types something in they get a result back from a system out , it works at one at a time , but i need it do it more than once using a while loop , but when i do that the system out dont work , what do i need to do ?
    public RepeatMenuInput()
    {
    System.out.println("Welcome");
    System.out.println("1 . Add Item");
    System.out.println("2 . Get Postion");
    System.out.println("3 . Get Item");
    System.out.println("4 . Remove Item");
    System.out.println("5 . Check Empty");
    System.out.println("6 . Check Size");
    System.out.println("7 . Print List");
    System.out.println("8 . Terminate Program");

    Scanner s1 = new Scanner (System.in);

    String input = s1.nextLine();

    if(input.equals("1"))
    {
    System.out.println("first");
    }
    else if(input.equals("2"))
    {
    System.out.println("second");
    }
    else if(input.equals("3"))
    {
    System.out.println("third");
    }
    else if(input.equals("4"))
    {
    System.out.println("fouth");
    }
    else if(input.equals("5"))
    {
    System.out.println("five");
    }
    else if(input.equals("6"))
    {
    System.out.println("six");
    }
    else if(input.equals("7"))
    {
    System.out.println("seven");
    }
    else if(input.equals("8"))
    {
    System.out.println("8");
    }

    s1.close();
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

    Default Re: System in problem ?

    Please place your code between [code][/code] tags and provide a short compilable example which demonstrates the problem.

    Also, the signature:
    Java Code:
    public RepeatMenuInput()
    is syntactically incorrect for a method.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Kramko is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default Re: System in problem ?

    Not entirely sure what you are asking.

    Here is some code that I think does what you want -- please ask any questions you have!

    Java Code:
    import java.util.Scanner;
    
    
    class Echo {
    	public static void main(String[] args) {
    		Scanner scan = new Scanner(System.in);
    
    		//print welcome and options
    		System.out.println("Welcome");
    		System.out.println("1 . Add Item");
    		System.out.println("2 . Get Postion");
    		System.out.println("3 . Get Item");
    		System.out.println("4 . Remove Item");
    		System.out.println("5 . Check Empty");
    		System.out.println("6 . Check Size");
    		System.out.println("7 . Print List");
    		System.out.println("8 . Terminate Program");
    
    		//var to track whether program should continue
    		boolean exit = false;
    		while(!exit) {
    			//ensures the user has input an int
    			if (scan.hasNextInt()) {
    				int userChoice = scan.nextInt();
    
    				switch (userChoice) {
    				case 1:
    					System.out.println("First");
    					break;
    				case 2:
    					System.out.println("Second");
    					break;
    				case 3:
    					System.out.println("Third");
    					break;
    				case 4:
    					System.out.println("Fourth");
    					break;
    				case 5:
    					System.out.println("Fifth");
    					break;
    				case 6:
    					System.out.println("Sixth");
    					break;
    				case 7:
    					System.out.println("Seventh");
    					break;
    				case 8:
    					System.out.println("Goodbye!");
    					exit = true;
    					break;
    				default:
    					System.out.println("Not a valid input. Input 8 to exit.");
    					break;
    				}
    
    			}
    			else {
    				System.out.println("Please input something valid!");
    			}
    			//consumes any extra tokens input by the user
    			scan.nextLine();
    		}
    		scan.close();
    	}
    }

  4. #4
    Icandoit is offline Member
    Join Date
    Oct 2013
    Posts
    12
    Rep Power
    0

    Default Re: System in problem ?

    You can just reformat a string and do something like

    Java Code:
    while(!input.toLowerCase().contains("exit"))

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

    Default Re: System in problem ?

    Please do not spoon feed solutions to the posters. Just help them find the problems themselves. And since you are new to this forum, you should read the guide for posting. It is in the FAQ section among the other useful topics.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Problem with System.nanoTime()
    By karl_marx in forum New To Java
    Replies: 3
    Last Post: 06-18-2013, 05:48 PM
  2. System Problem
    By DPdieciocho in forum New To Java
    Replies: 4
    Last Post: 06-17-2013, 02:28 PM
  3. jdb and System.in.read() problem
    By wavydavy in forum New To Java
    Replies: 3
    Last Post: 01-04-2012, 11:07 PM
  4. System.out.println problem
    By luke in forum New To Java
    Replies: 5
    Last Post: 10-05-2010, 07:45 PM
  5. problem with System.nanoTime()
    By Bryan500 in forum New To Java
    Replies: 5
    Last Post: 11-01-2008, 03:39 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
  •