Ask coding questions

← Back to all posts
Fix please
DeliciousBurger (0)

If anyone could fix my code so if the input is equal to a list it will print out the message below it. https://repl.it/@DeliciousBurger/MildHeftyCell

Commentshotnewtop
vedprad1 (727)

First of all, you should delete import java.util.S because you do not need it.
Second, you are messing up good list, badlist, and username. This is what you wrote in the contains() part:

if (username.contains(goodlist)){
    System.out.println("Good for you");
 }
if (username.contains(badlist)){
    System.out.println("Get better");
}

But you are not checking if the username contains the list. You are checking that the list contains the username, like this:

if (goodlist.contains(username)){
    System.out.println("Good for you");
 }
else if (badlist.contains(username)){
    System.out.println("Get better");
}

Also add else if for the second one. Less complexity.

If this answers your question, please check the checkmark on the left side of this message. Thanks and Good Luck!

Edgod (58)

You can use the contains( ) method.

if (goodlist.contains(username)){
    System.out.println("Good for you");
 }
if (badlist.contains(username)){
    System.out.println("Get better");
}

Edit: You might want to fix some of your code. import java.util.*; the * imports all classes and methods from the file, making your first two import statements redundant. Your code seems too spread out, you don't need that many blank lines between.
Just wondering, what are you using to learn Java?