#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Problem with invoking a method


    Hi, I have a problem in Java with invoking the method "handleInput" in the method "topLoop". The thing is that when you type something the message "Unknown command 'command'" should appear ('command' is the output from index 0 in a string-array) but when the input is empty I DON'T want the message to appear at all, the message should only appear once you write something. And when you type "quit" it will simply exit the program after you've confirmed with "y". Everything works except that when the input is empty the message is still shown, so how do I invoke the handleInput-method in the topLoop-method so that this will work?

    Here is the Java-code for handleInput:
    PHP Code:
        public static void handleInput() {

            
    Scanner input = new Scanner(System.in);
            
    String command input.nextLine();

            
    String[] split command.split(" "); //Split string in array after space

            
    printLine("Unknown command '"split[0] +"'"); //Print out index 0 from array
            
        

    And here is the Java-code for topLoop:

    PHP Code:
        public static void topLoop() {
            
            while (
    true//While true - loop forever
            
    {     
                print(
    ">> ");

                
    Scanner input = new Scanner(System.in); //Create new scanner-input
                
    String answer input.nextLine(); //Input for string-value

                
    if ("".equals(answer)) {
                    continue;
                }
                else if ((
    "quit".equals(answer)) && (confirm() == true)) { //If input is "quit" and confirmed with "y":
                    
    break; //exit program
                
    }
    handleInput(); //Invoke method
            
    }
        } 
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,719
    Rep Power
    348
    One possible problem is having more than one instance of the Scanner class. The Scanner class will read input and save it in a buffer. So the first instance reads and saves some of the input, the second instance will miss it.
    Define ONE instance of the Scanner class object and share it with all the methods that are reading user input.

    when the input is empty I DON'T want the message to appear at all,
    Use an if statement with one of the String class's methods to test for the empty String and skip printing a message if it is.

    Can you copy the contents of the command prompt window and paste it here showing the input to the program and what it prints out?
    Last edited by NormR; December 22nd, 2013 at 07:13 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by NormR
    One possible problem is having more than one instance of the Scanner class. The Scanner class will read input and save it in a buffer. So the first instance reads and saves some of the input, the second instance will miss it.
    Define ONE instance of the Scanner class object and share it with all the methods that are reading user input.


    Use an if statement with one of the String class's methods to test for the empty String and skip printing a message if it is.

    Can you copy the contents of the command prompt window and paste it here showing the input to the program and what it prints out?
    The problem is now solved and the new topLoop code looks like this:

    Code:
    public static void topLoop() throws IOException {
    
         while (true) //While true - loop forever
         { 	
    	print(">> ");
    			
    	BufferedReader answer = new BufferedReader(new InputStreamReader(System.in));
    	String line = answer.readLine();
    
    	if (("quit".equals(line)) && (confirm() == true)) { //If input is "quit" and confirmed with "y":
    		break; //exit program
    	}
    	else if (!line.isEmpty()) { //Else if answer is not empty:
    		handleInput(line); //Invoke handleInput-method
    		}
    	}
    }
    And here's the modified handleInput code:

    Code:
    public static void handleInput(String command) {
    
    	String[] split = command.split(" "); //Split string in array after space
    	printLine("Unknown command '"+ split[0] +"'"); //Print out index 0 from array
    
    }
    So now everything works just fine, and the codes look much better, thank you! :-D

IMN logo majestic logo threadwatch logo seochat tools logo