2

I have a program I'm making where when the user type in a mood, it will output a quote based on it. I need to tell the program
if the user is happy, then output this text The problem is, I don't know how to get the program to recognize the input and output the text based on it... here's what I have for code so far.

import java.util.Scanner;
public class modd {
    public static void main(String arrgs[]) {
        System.out.println("Enter your mood:");
        Scanner sc = new Scanner(System.in);
        String mood = sc.nextLine();

        if (sc = happy) {
            System.out.println("test");

            if (sc = sad) {
                System.out.println("I am sad");
            }
        }
    }
} 
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
Hasteify
  • 63
  • 4
  • I'd start by taking a look at the [Java Tutorial](http://docs.oracle.com/javase/tutorial/java/data/strings.html) on `String`s – MadProgrammer Nov 07 '13 at 02:57
  • Learn how to compare strings. `switch` also might help. Also your input is `mood` (not `sc`). – PM 77-1 Nov 07 '13 at 03:00

6 Answers6

3

Cant compare strings like this

if (sc = happy)  // also, you never declare a happy variable. So use the 
                 // stirng literal like I did below
// also can't compare to Scanner instance
// instead compare to mood

Use equals

if ("happy".equals(mood)) {  // Caught commenter, can't use sc to compare, use mood
    // do something
}

Also, if in the future, you needed to use an = operation for comparison (for anything but strings), you would use a double ==

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
1

Always make use of .equals(..) method to compare String values..

if (mood.equals("happy")) 
  System.out.println("test");

if (mood.equals("sad")) 
    System.out.println("I am sad");
Dark Knight
  • 8,218
  • 4
  • 39
  • 58
1

it should be like this

    if ("happy".equals(mood)
{
    System.out.println("IM HAPPYYYYYYY!!!!");
}
Albert Laure
  • 1,702
  • 5
  • 20
  • 49
1

First of all, it looks like you are dealing with the wrong variable sc. I think you meant to compare mood.

When dealing with strings, always use .equals(), not ==. == compares the references, which is often unreliable, while .equals() compares the actual values.

It's also good practice to either convert your string to all uppercase or all lower case. I'll use lower case in this example with .toLowerCase(). .equalsIgnoreCase() is also another quick way around any case problems.

I'd also recommend an if-else-statement, not a second if-statement. Your code would look like this:

mood=mood.toLowerCase()

if (mood.equals("happy")) {
    System.out.println("test");
}

else if (mood.equals("sad")) {
    System.out.println("I am sad");

}

These are all pretty basic java concepts, so I'd recommend reading more thoroughly about some of them. You can check out some of the documentation and/or other questions here:

Community
  • 1
  • 1
Wold
  • 952
  • 1
  • 13
  • 25
  • Thanks for the answer! I really appreciate it. If you wouldn't mind telling me one more thing; How do I set the output to anything else. Say the user inputs text that's not recognized by the program, how would I output something like "Mood not Understood" or something? – Hasteify Nov 07 '13 at 03:10
  • You could a final `else` statement that would print your message. If `mood` went through every condition without matching any of them, then your program would print the message at the end. Also glad I could help. – Wold Nov 07 '13 at 03:13
  • @Hasteify a Switch would be better if that's the case – Albert Laure Nov 07 '13 at 09:09
  • 1
    @AsshO.Le I agree, but since he was already implementing an if-else ladder, I kept with the theme. He didn't want to completely rewrite, he just wanted to solve his problem. – Wold Nov 07 '13 at 09:12
  • @Wold yes you are correct :) im just adding some thing that would help him in the future :) – Albert Laure Nov 07 '13 at 09:13
  • @AsshO.Le No harm in that! – Wold Nov 07 '13 at 09:15
1

How I think you can approach this problem is by specify a set of pre-defined input parameter for the user to choose from then respond accordingly based on there choice e.g:

 System.out.println ("Enter you mood: [1 = happy,2 = sad,3 = confused]");
 int input = new Scanner(System.in).nextInt ();

 switch (input)
 {
   case 1:  System.out.println ("I am happy");break;
   case 2:  System.out.println ("I am sad");break;
   default: System.out.println ("I don't recognize your mood");
 }
Mario Dennis
  • 2,986
  • 13
  • 35
  • 50
0

You need to correct the following things:

  1. single = means indicates assignment, not comparison.

  2. I assume you would like to check whether the string entered is equal to "happy" and "sad". Use equals method rather than "==" for checking string values.

  3. why you put if (sc = sad) inside if (sc = happy). the inside check will never executed.

  4. You need to check the values entered from console, rather than using Scanner sc itself.

So I think you need to change the code like follows:

String mood = sc.nextLine();

    if (mood.equals("happy")) {
        System.out.println("test");
    }

    if (mood.equals("sad")) {
        System.out.println("I am sad");
    } 
Mengjun
  • 3,159
  • 1
  • 15
  • 21