Results 1 to 4 of 4
  1. #1
    Revelation is offline Member
    Join Date
    Jun 2007
    Posts
    6
    Rep Power
    0

    Default 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
    }
    my read_input function looks like
    Java 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.
    Java 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.

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    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..

  3. #3
    Revelation is offline Member
    Join Date
    Jun 2007
    Posts
    6
    Rep Power
    0

    Default

    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());
    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.
    Java 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.

  4. #4
    Join Date
    Jun 2007
    Posts
    11
    Rep Power
    0

    Default

    change main as follows..
    "public static void main(String args[]) throws Exception"

    can u give the entire code so that I can help u..

Similar Threads

  1. Compare 5 numbers
    By Snowboardmylife in forum New To Java
    Replies: 5
    Last Post: 04-15-2008, 08:04 PM
  2. Compare lists
    By JavaNoob in forum New To Java
    Replies: 2
    Last Post: 08-08-2007, 04:11 PM
  3. how to compare two strings
    By elizabeth in forum New To Java
    Replies: 7
    Last Post: 08-06-2007, 04:57 AM
  4. Compare 2 XML
    By Peter in forum XML
    Replies: 1
    Last Post: 07-05-2007, 03:58 AM
  5. compare speed
    By bbq in forum JDBC
    Replies: 1
    Last Post: 06-28-2007, 06:34 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
  •