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 06-28-2007, 05:44 PM
Member
 
Join Date: Jun 2007
Posts: 6
Revelation is on a distinguished road
String Compare not working
Hi all, sorry for simplicity of this question but its bugging me and I'm just frustrated now. Java and how their strings work in IF statements confuses me a little. They don't behave like C++.

basically I have a function that looks like

Code:
String option = read_input(); System.out.println(option); if(option.compareTo("y") == 1) { do thing 1 } else { do thing 2 }
my read_input function looks like
Code:
InputStreamReader isr = new InputStreamReader(System.in); BufferedReader stdin = new BufferedReader(isr); output = stdin.readLine(); return output;
So, when I press "y" it does thing 2. I want thing 1 dang it! So, I must be doing something fundamentally incorrect here. Is my string compare wrong.


I repeat the function a little later in the program to enter number of days.
Code:
String select_days = read_input(); int num_days = Integer.parseInt(select_days.toString());
and when I enter
50

It throws a number exception at me.

What am I doing wrong?

Thanks,
James

Last edited by Revelation : 06-30-2007 at 02:41 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-29-2007, 12:49 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Hi James,

Please use [code] tag next time to make your posts more readable.

What you wrote looks ok to me. But why don't you write select_days to the screen to see what you obtained from the console? I dont have an IDE here and i could not test your code right now. Let us know the output..
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-30-2007, 02:39 PM
Member
 
Join Date: Jun 2007
Posts: 6
Revelation is on a distinguished road
I actually was outputting my input in the second part as well. It looks more like:
Code:
String select_days = read_input(); System.out.println(select_days); int num_days = Integer.parseInt(select_days.toString());
It was receiving exactly what I expected. if i type 58, it was printing 58 to the screen, then throwing number exceptions. I found that if I set the variable manually just before both of the if statements the compare worked. ie.
Code:
String select_days = read_input(); System.out.println(select_days); select_days = "58"; int num_days = Integer.parseInt(select_days.toString());
So, despite the output looking correct, it fails compares and integer parses.

I will do a restart on the environment and have a look tomorrow. I find java will randomly stop working correctly and you will spend an hour trying to debug phantom behaviour until you realise "oh, its not compiling new code anymore". Seems to be a problem in eclipse and SunOne

Last edited by Revelation : 06-30-2007 at 02:44 PM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-30-2007, 07:43 PM
Member
 
Join Date: Jun 2007
Posts: 11
creativehacker is on a distinguished road
change main as follows..
"public static void main(String args[]) throws Exception"

can u give the entire code so that I can help u..
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
Compare 5 numbers Snowboardmylife New To Java 5 04-15-2008 08:04 PM
Compare lists JavaNoob New To Java 2 08-08-2007 04:11 PM
how to compare two strings elizabeth New To Java 7 08-06-2007 04:57 AM
Compare 2 XML Peter XML 1 07-05-2007 03:58 AM
compare speed bbq Database 1 06-28-2007 06:34 PM


All times are GMT +3. The time now is 10:33 PM.


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