Results 1 to 3 of 3
  1. #1
    peachyco is offline Member
    Join Date
    Nov 2007
    Posts
    10
    Rep Power
    0

    Default Problems with readLine() and calling methods

    Hi, y'all!

    I'm new to the forums, and also to Java programming. I'm trying to write a Cash Till program for a project but I keep having a problem with my input.

    Java Code:
    //Main Method
    public static void main(String[] args)
    {
    	userVerify();
    	
    	drawMainMenu();
    	userInput();
    		
    	while (inputStr != "1" && inputStr != "2")
    	{
    		System.out.println("\n--------------------------"	+
    						   "\nERROR: Input Out of Range."	+
    						   "\n--------------------------\n\n");
    		drawMainMenu();
    		userInput();
    	}
    		
    	//DUMMY EFFECTS: This part includes what happens
            //  after the selection is made.
    	System.out.println(inputStr);
    	System.exit(0);
    		
    }//end main
    
    .
    .
    .
    
    //Method to acquire input from user
    static String userInput()
    {
    	try
    	{
    		BufferedReader br = new BufferedReader(
                                               new InputStreamReader(System.in));
    		inputStr = br.readLine();
    		
    	}//end try
    		
    	catch (IOException ioe)
    	{
    		ioe.printStackTrace();
    		System.exit(1);
    	}//end catch
    		
    	return inputStr;
    		
    }//end method userInput
    The drawMainMenu() method does no logic; it just displays the menu. It displays, however, the two choices the user can take: "1" to go to Transactions, and "2" to Exit. The String inputStr is declared globally as a static String.

    There seems to be nothing wrong with userInput(), 'coz the userVerify() method also calls the same method and it works just fine. At the main method, however, if I enter "1" or "2" from userInput(), it still goes through the while loop! I can't understand what's wrong. Please help!

    Thanks so much in advance for any advice/comment. All suggestions are very much appreciated.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Use the equals method with Strings.
    Java Code:
    while (!inputStr.equals("1") && !inputStr.equals("2"))

  3. #3
    peachyco is offline Member
    Join Date
    Nov 2007
    Posts
    10
    Rep Power
    0

    Default

    Whew! Thanks a lot for the quick help. I thought I had a problem with the logic and the calling of methods. I didn't realize it was just a simple issue with the While argument.

    Thanks again a ton! :)

Similar Threads

  1. Calling Java methods form Python
    By mew in forum Advanced Java
    Replies: 1
    Last Post: 12-21-2007, 02:30 PM
  2. Calling Methods
    By bluegreen7hi in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 06:22 AM
  3. DataInputStream readLine()
    By ravian in forum New To Java
    Replies: 2
    Last Post: 11-26-2007, 10:44 PM
  4. need help calling methods
    By lowpro in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 09:53 AM
  5. BufferedReader: readLine method problems
    By bbq in forum Advanced Java
    Replies: 2
    Last Post: 06-30-2007, 02:27 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
  •