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: If Statement - Check for two thinks

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question If Statement - Check for two thinks

    Hi, i got this problem.
    I'm trying to make an if statement check for this, at the same time:
    If the userInput is equal to some text and if a random number is greather than another random number.

    I can't make it work, please help me.

    My code:

    		if(userInput.equals("h".toLowerCase()) && userNumber > comNumber){
    			System.out.println("You are right! ");
    			}
    		if(userInput.equals("l".toLowerCase()) && userNumber < comNumber){
    			System.out.println("You are right! ");
    			}
    		if(userInput.equals("e".toLowerCase()) && userNumber == comNumber){
    			System.out.println("You are right! ");
    			}
    		else{
    			System.out.println("You are wrong! ");
    			}


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,628
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: If Statement - Check for two thinks

    What is happening? How do you know it doesn't work?

    Also, why are you doing this: "h".toLowerCase()
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: If Statement - Check for two thinks

    First, I couldn't have my whole code, cause I'm new.
    I'm making a small game and i'm trying to check for if the user types in the right answer, so i want to make it all lower case, before i check (To the "h".toLowerCase())

    What happens:
    My code just like skips past the part where the user needs to type in the answer.
    I couldn't have my whole code cause i'm new, so i posted the last part...

    Here is the first part:

    Scanner userInput = new Scanner(System.in);

    Random rand = new Random();
    int userNumber = rand.nextInt(11);
    int comNumber = rand.nextInt(11);

    System.out.println("Your number is: " + userNumber + " / 10");
    System.out.println("Do you think that YOUR NUMBER is Heigher(h), Lower(l) or Equal To(e), than the COMPUTER's NUMBER: ");
    Last edited by Nicken99; February 26th, 2014 at 12:43 PM.

  4. #4
    Member
    Join Date
    Dec 2013
    Posts
    40
    My Mood
    Amazed
    Thanks
    2
    Thanked 11 Times in 11 Posts

    Default Re: If Statement - Check for two thinks

    Hello Nicken99, you seem to be using the 'toLowerCase()' method in the wrong place. This is what you should do,
     userInput.toLowerCase().equals("h");
    or you could use
     userInput.equalsIgnoreCase("h");

    Come to think of it, you didn't seem to make use of the new instance of your Scanner class.
    Who holds the KEY to all knowledge?

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: If Statement - Check for two thinks

    Now I'm confused, (I'm from Denmark, and 14 years old, so I'm not perfect at English) I'm not sure that I'm using Scanners right now.
    If I'm wrong then please tell me. (Just like: "You are using scanners wrong. They are used like this...")

    I know how to compare Integers, but when I have to compare strings then I'm not sure how to do it.

    I Thinks it's like this (Bad Example, I would use Integers Here, but i couldn't come up with any better):
    Scanner userInput = new Scanner(System.in);

    System.out.println("Enter Your Age: ");

    if(userInput.equals("10")){
    System.out.println("You are 10 years old. ");
    }else{
    System.out.println("Please Try Again. ");

    - Thanks a lot for your help, I hope that you will help me again

  6. #6
    Member
    Join Date
    Dec 2013
    Posts
    40
    My Mood
    Amazed
    Thanks
    2
    Thanked 11 Times in 11 Posts

    Default Re: If Statement - Check for two thinks

    Well-done Nick! You seem do be doing well. I hope you have something like this after the last part of the code you posted?

     String  letterInputed = userInput.next();

    If you do, your if statement should look like this:

     if (letterInputed.equalsIgnoreCase("h") && userNumber > comNumber ){
       System.out.println("You're right!");
    }

    And so on...

    If you still have problems, post all the code, compile and run & then post the errors displayed ok? Nice work.
    Who holds the KEY to all knowledge?

  7. The Following User Says Thank You to Mugambbo For This Useful Post:

    GregBrannon (February 28th, 2014)

  8. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: If Statement - Check for two thinks

    Thans Alot... I've learn so much. But every time i got my code right now - it shows the answer two times, how can i make it go away?

    Code:

    public static void main(String[] args) {

    @SuppressWarnings("resource")
    Scanner userInput = new Scanner(System.in);

    Random rand = new Random();
    int userNumber = rand.nextInt(11);
    int comNumber = rand.nextInt(11);

    System.out.println("Your number is: " + userNumber + " / 10");
    System.out.println("Do you think that YOUR NUMBER is Heigher(h), Lower(l) or Equal To(e), than the COMPUTER's NUMBER: ");

    String letterInput = userInput.next();

    if(letterInput.equalsIgnoreCase("h") && userNumber > comNumber){
    System.out.println("You are right! ");
    System.out.println("The computer's number is: " + comNumber);
    }
    if(letterInput.equalsIgnoreCase("l") && userNumber < comNumber){
    System.out.println("You are right! ");
    System.out.println("The computer's number is: " + comNumber);
    }
    if(letterInput.equalsIgnoreCase("e") && userNumber == comNumber){
    System.out.println("You are right! ");
    System.out.println("The computer's number is: " + comNumber);
    }
    else{
    System.out.println("You are wrong! ");
    System.out.println("The computer's number is: " + comNumber);
    }

    }

Similar Threads

  1. Using an If statement to check boxes
    By me10lee83 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2013, 08:57 AM
  2. Replies: 2
    Last Post: June 25th, 2013, 06:33 AM
  3. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 03:19 PM
  4. Check This! :-?
    By shen_punkz21 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 3rd, 2013, 12:59 AM
  5. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 3rd, 2013, 11:21 PM