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

    Default Problem reading input from cmd.

    Greetings, folks.

    I'm using JDK1.6.0_21 and Eclipse on Windows XP (32bit).

    I'm trying to read user input from the command prompt using BufferedReader and InputStreamReader (System.in), and everything seems fine when I echo it (i.e. print bf.readLine directly, or store it to another variable and print that).

    However, when I try to pass the string from readLine through my If-Else statement, it all fails. Any help would be much appreciated.

    ~ peachyco


    [MY CODE]

    public class CabinMain {

    //=====| GLOBAL DECLARATIONS |===============
    //Instance Variables
    static String inputStr = ""; //Holds user input as String.
    static boolean errorFlag = false; //Flags any runtime error, usually for user input.

    //===========================================


    //=====| THE MAIN METHOD |===================

    public static void main(String[] args) {

    //Display the main menu.
    menuMain();

    } //end method main

    //===========================================



    //=====| OTHER METHODS |=====================

    //METHOD FOR ACQUIRING USER-INPUT FROM COMMAND PROMPT
    static String getInput() {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String input = null;
    try {
    input = br.readLine();
    } catch (IOException ioe) {
    ioe.printStackTrace();
    System.exit(1);
    } //end try-catch
    return input;
    } //end method getInput


    //METHOD FOR HANDLING THE MAIN MENU
    static void menuMain() {
    do {
    System.out.print("\n\n=====| MAIN MENU |=====" +
    "\n C = Create New Cabin" +
    "\n X = Exit Program" +
    "\n\n Enter Selection: ");
    errorFlag = false;
    inputStr = getInput().toLowerCase();

    if (inputStr == "x") {
    System.exit(0);//Exit the program.

    } else if (inputStr == "c") {
    return; //Proceed with program.

    } else {
    errorFlag = true; //Input is out of bounds and is meaningless.
    System.out.print("\n\n============================ ====" +
    "\n<!>ERROR: Input Out of Bounds!" +
    "\n================================\n");
    } //end if-else
    } while (errorFlag);
    } //end method menuMain

    //============================================

    } //end CabinMain class


    EDIT 1: After reading thru my post, I realized I wasn't clear on the problem.

    The program should terminate when the user types "X" or "x", should continue if "C" or "c", and loop until a proper input is acquired. The problem is that whatever I type, it keeps on looping (as in going straight for the ELSE part of my If-ElseIf-Else statement).

    The BuffReader seems fine, though, because when I echoed the string and checked its length, it is "X" (1 char long) when I do type "X". Maybe a problem with my If-Else, or with using the global variables (i.e. errorFlag, inputStr)?
    Last edited by peachyco; 10-11-2010 at 01:27 PM. Reason: Additional Information

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Did you get any errors?

    Can you specifically post your code segment. And also please use code tags next time.

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

    Default

    @Eranga: Thanks for pointing that out. I hadn't posted on any board in quite a while so I've forgotten about tags.

    I've copied the code directly from the .java file.

    Java Code:
    public class CabinMain {
    
    	//=====| GLOBAL DECLARATIONS |===============
    	//Instance Variables
    	static String inputStr = ""; //Holds user input as String.
    	static boolean errorFlag = false; //Flags any runtime error, usually for user input.
    	
    	//===========================================
    	
    	
    	//=====| THE MAIN METHOD |===================
    	
    	public static void main(String[] args) {
    		
    		//Display the main menu.
    		menuMain();
    		
    	} //end method main
    	
    	//===========================================
    	
    	
    	
    	//=====| OTHER METHODS |=====================
    	
    	//METHOD FOR ACQUIRING USER-INPUT FROM COMMAND PROMPT
    	static String getInput() {
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		String input = null;
    		try {
    			input = br.readLine();
    		} catch (IOException ioe) {
    			ioe.printStackTrace();
    			System.exit(1);
    		} //end try-catch
    		return input;
    	} //end method getInput
    	
    	
    	//METHOD FOR HANDLING THE MAIN MENU
    	static void menuMain() {
    		do {
    			System.out.print("\n\n=====| MAIN MENU |=====" +
    					"\n C = Create New Cabin" +
    					"\n X = Exit Program" +
    					"\n\n Enter Selection: ");
    			errorFlag = false;
    			inputStr = getInput().toLowerCase();
    
    			if (inputStr == "x") {
    				System.exit(0);//Exit the program.
    				
    			} else if (inputStr == "c") {
    				return; //Proceed with program.
    				
    			} else {
    				errorFlag = true; //Input is out of bounds and is meaningless.
    				System.out.print("\n\n================================" +
    						"\n<!>ERROR: Input Out of Bounds!" +
    						"\n================================\n");
    			} //end if-else
    		} while (errorFlag);
    	} //end method menuMain
    	
    	//============================================
    	
    } //end CabinMain class
    I don't get any error message, but it's not doing what I imagined it to be doing. If the user enters "X" or "x", the program should terminate via System.exit(0). But it doesn't; instead, it goes through the Do-While loop again as if I entered a wrong value.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    use like this for String comparison

    inputStr.trim().equals("x").....

    trim() will truncate the leading and trailing space and
    String Comparison
    Strings can not be compared with the usual <, <=, >, or >= operators, and the == and != operators don't compare the characters in the strings.
    Comparing objects vs. primitive types
    [to be supplied]
    Comparing Strings: ==, .equals(), .compareTo(), ...
    To compare Strings for equality, don't use ==. The == operator checks to see if two objects are exactly the same object. Two strings may be different objects, but have the same value (have exactly the same characters in them). Use the .equals() method to compare strings for equality.
    Ramya:cool:

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

    Default

    @Eranga and Ramya: Thanks for your quick replies. Yep, string1.equals(string2) did the job for me. I just knew it wasn't a logical error... :D

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Did you read more about that. My suggestion is read more about that at this time. Because in my experience, even lots of experienced people too confused with that and had lots of issues on their way.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

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

    Default

    @Eranga: Yes, I've read more on Strings and object equalities since then. Thanks a lot. Marking thread as solved.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Need help wit reading txt file into input.
    By ShinTec in forum New To Java
    Replies: 7
    Last Post: 02-02-2010, 04:40 PM
  2. reading from input file and then write on it
    By sara12345 in forum New To Java
    Replies: 9
    Last Post: 01-19-2010, 11:41 AM
  3. reading user input as collection from JSP
    By RSP in forum Web Frameworks
    Replies: 1
    Last Post: 04-11-2009, 02:37 PM
  4. Problem in reading HTML input field while uploading file
    By sudipanand in forum Java Servlet
    Replies: 1
    Last Post: 11-27-2008, 09:26 AM
  5. Reading input file into an array
    By littlefire in forum New To Java
    Replies: 6
    Last Post: 10-18-2008, 11:51 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
  •