Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-23-2007, 04:32 PM
Member
 
Join Date: Nov 2007
Posts: 6
peachyco is on a distinguished road
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.

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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-23-2007, 08:06 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,146
hardwired is on a distinguished road
Use the equals method with Strings.
Code:
while (!inputStr.equals("1") && !inputStr.equals("2"))
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-24-2007, 08:44 AM
Member
 
Join Date: Nov 2007
Posts: 6
peachyco is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling Java methods form Python mew Advanced Java 1 12-21-2007 03:30 PM
Calling Methods bluegreen7hi New To Java 3 12-17-2007 07:22 AM
DataInputStream readLine() ravian New To Java 2 11-26-2007 11:44 PM
need help calling methods lowpro New To Java 2 11-15-2007 10:53 AM
BufferedReader: readLine method problems bbq Advanced Java 2 06-30-2007 03:27 AM


All times are GMT +3. The time now is 05:30 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org