Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: My 'if statements' are skipped after user input

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My 'if statements' are skipped after user input

    The program shuts down right after I receive input for a String and an int variable, and I'm really confused as to why it's doing this. Any help at all would be greatly appreciated.

     
    import java.util.Scanner;
     
    class Tempurature{
        public static void main(String args[])
    	 {
    Scanner in = new Scanner(System.in);
     
            int tempNum;
            String tempScale;
     
            System.out.print("Type F for Fahrenheit or C for Celsius: ");
    	tempScale = in.nextLine();
            System.out.print("Enter the tempurature: ");
            tempNum = in.nextInt();
     
    if (tempScale == "C" && tempNum >= 100)
     
            System.out.println("Your water is boiling."); 
     
    if  (tempScale == "F" && tempNum >= 212)
     
            System.out.println("Your water is boiling.");
     
    if  (tempScale == "F" && tempNum >= 212)
            System.out.println("Your water is liquid.");
     
     
    else if (tempScale == "C"  && tempNum <= 100)
            System.out.println("Your water is liquid.");
     
        }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: My 'if statements' are skipped after user input

    For String equality, use the equals method as opposed to ==. See the following post for more information:
    http://www.javaprogrammingforums.com...html#post18725

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My 'if statements' are skipped after user input

    Quote Originally Posted by copeg View Post
    For String equality, use the equals method as opposed to ==. See the following post for more information:
    http://www.javaprogrammingforums.com...html#post18725

    Well now I am getting errors around my "&&" in this line:
    if (tempScale = "C" && tempNum >= 100)
     
            System.out.println("Your water is boiling.");

    Do I need to convert "C" to a number? If so then could I get some help on that as well?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: My 'if statements' are skipped after user input

    Well now I am getting errors around my "&&" in this line:
    Post the error as I'm not very good at reading minds...and based upon the code snippet you just posted it doesn't seem like you took my previously posted advice.

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My 'if statements' are skipped after user input

    Quote Originally Posted by copeg View Post
    Post the error as I'm not very good at reading minds...and based upon the code snippet you just posted it doesn't seem like you took my previously posted advice.
    Sorry about that. The error I get is "not a statement" on the && part. I'm not particularly sure what I need to do to fix it.

     
    if ((tempScale) equals("C") && (tempNum >= 100));

  6. #6
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Stressed
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: My 'if statements' are skipped after user input

    cozliz, in the previously linked answer, it seems that you saw the equals, but not the format to use the method.

    for example:
          if(p1.equals(p2))
            {
                System.out.println("p1 and p2 have the same value");
            }
    Last edited by Herah; October 8th, 2011 at 06:56 PM.

  7. #7
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: My 'if statements' are skipped after user input

    Quote Originally Posted by Cozilz View Post
    Sorry about that. The error I get is "not a statement" on the && part. I'm not particularly sure what I need to do to fix it.

     
    if ((tempScale) equals("C") && (tempNum >= 100));
    You have a syntax error within this if statement. Your logic is correct though.
    Simplicity calls for Complexity. Think about it.

Similar Threads

  1. Valid user input
    By ChristopherLowe in forum Java Programming Tutorials
    Replies: 1
    Last Post: June 21st, 2011, 04:53 PM
  2. Valid user input
    By ChristopherLowe in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: June 21st, 2011, 04:53 PM
  3. User Input File Name
    By PineAppleKing in forum Java Theory & Questions
    Replies: 12
    Last Post: June 3rd, 2011, 10:23 AM
  4. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM