How to Compare strings in java [duplicate]

旧城冷巷雨未停 提交于 2019-12-20 03:46:09

问题


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");
            }
        }
    }
} 

回答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:

  • if-else statement
  • Strings
  • Java String.equals versus ==



回答2:


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




回答3:


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");



回答4:


it should be like this

    if ("happy".equals(mood)
{
    System.out.println("IM HAPPYYYYYYY!!!!");
}



回答5:


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");
 }



回答6:


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");
    } 


来源:https://stackoverflow.com/questions/19827161/how-to-compare-strings-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!