Results 1 to 6 of 6
  1. #1
    EdenC is offline Member
    Join Date
    Aug 2011
    Posts
    19
    Rep Power
    0

    Default Java If OStatement Error

    Hi Guys,

    When I run this code:

    Java Code:
    OS = System.getProperty("os.name");
    			InputStreamReader isr = new InputStreamReader(System.in);
    			BufferedReader br = new BufferedReader(isr);
    			input = br.readLine();
    			input = input.replaceAll("\\\\n", "");
    			if(input == OS){System.out.println("Thank god - you know what your operating system is (" + OS + ")");}
    			else if(input != OS){System.out.println("Wrong! - You thought your OS is " + input + " but is actually " + OS + "!");}
    			else{System.out.println("Error");};
    Even when I enter the correct OS, I still get the else if statement. I assume it is something wrong with the the if statement?

    Thanks,

    EdenC

    [PS. I'm also having another problem with this piece of code which you can read about here - read to end to see. Thanks!]

    EDIT1: Title should be "Java If Statement Error", but I can't see how to change the title.
    Last edited by EdenC; 08-23-2011 at 12:39 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default

    Java Code:
    if(input == OS)
    You should use the equals method to compare the contents of String objects.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default

    Java Code:
    if(input == OS)
    You should use the equals method to compare the contents of String objects.

  4. #4
    EdenC is offline Member
    Join Date
    Aug 2011
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Java Code:
    if(input == OS)
    You should use the equals method to compare the contents of String objects.
    I'm not sure what you mean here, but if you mean to use the code you have inserted here then I already am (see line 6).

    Thanks,

    EdenC

  5. #5
    EdenC is offline Member
    Join Date
    Aug 2011
    Posts
    19
    Rep Power
    0

    Default

    Nevermind - issue has been fixed in my other thread.

    Thanks,

    EdenC

  6. #6
    Join Date
    Aug 2011
    Location
    India
    Posts
    1
    Rep Power
    0

    Default

    Well.. its because your conditional check is wrong...
    instead of " == "
    your statement should be..
    if(input.equals(OS))

Similar Threads

  1. Replies: 0
    Last Post: 01-21-2011, 12:43 PM
  2. Replies: 5
    Last Post: 08-13-2010, 10:04 AM
  3. java out of memory error-heap space error
    By elsanthosh in forum NetBeans
    Replies: 4
    Last Post: 06-15-2010, 09:31 AM
  4. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  5. Replies: 1
    Last Post: 10-26-2009, 04:38 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
  •