Results 1 to 4 of 4
Thread: String Compare not working
- 06-28-2007, 05:44 PM #1
Member
- Join Date
- Jun 2007
- Posts
- 6
- Rep Power
- 0
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
Java Code:String option = read_input(); System.out.println(option); if(option.compareTo("y") == 1) { do thing 1 } else { do thing 2 }
Java Code:InputStreamReader isr = new InputStreamReader(System.in); BufferedReader stdin = new BufferedReader(isr); output = stdin.readLine(); return output;
I repeat the function a little later in the program to enter number of days.
Java Code:String select_days = read_input(); int num_days = Integer.parseInt(select_days.toString());
50
It throws a number exception at me.
What am I doing wrong?
Thanks,
JamesLast edited by Revelation; 06-30-2007 at 02:41 PM.
- 06-29-2007, 12:49 PM #2
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..
- 06-30-2007, 02:39 PM #3
Member
- Join Date
- Jun 2007
- Posts
- 6
- Rep Power
- 0
I actually was outputting my input in the second part as well. It looks more like:
Java Code:String select_days = read_input(); System.out.println(select_days); int num_days = Integer.parseInt(select_days.toString());
Java Code:String select_days = read_input(); System.out.println(select_days); select_days = "58"; int num_days = Integer.parseInt(select_days.toString());
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 SunOneLast edited by Revelation; 06-30-2007 at 02:44 PM.
- 06-30-2007, 07:43 PM #4
Member
- Join Date
- Jun 2007
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
Compare 5 numbers
By Snowboardmylife in forum New To JavaReplies: 5Last Post: 04-15-2008, 08:04 PM -
Compare lists
By JavaNoob in forum New To JavaReplies: 2Last Post: 08-08-2007, 04:11 PM -
how to compare two strings
By elizabeth in forum New To JavaReplies: 7Last Post: 08-06-2007, 04:57 AM -
Compare 2 XML
By Peter in forum XMLReplies: 1Last Post: 07-05-2007, 03:58 AM -
compare speed
By bbq in forum JDBCReplies: 1Last Post: 06-28-2007, 06:34 PM
Bookmarks