Reputation: 1
So my code tells me, at the if statement, that I need to insert != null check. Why does it tell me this and how do I create an if statement for a String variable? I am a beginner and appreciate the help but please leave an explanation with your answer as I don't always know exactly what I am doing without specific detail. Thanks a lot!
import java.util.Scanner;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game2 extends JPanel {
public void paint(Graphics g) { //creates a void in order to paint
Graphics2D g2d = (Graphics2D) g; //creates 2D graphics
g2d.setColor(Color.YELLOW); //sets color
g2d.fillOval(350, 15, 25, 70); //draws oval
g2d.fillOval(400, 15, 25, 70); //draws
g2d.setColor(Color.RED); //sets color
g2d.fillRect(350, 120, 77, 25); //draws rectangle
Scanner game = new Scanner (System.in); //creating a new scanner
String mood, Happy, Satisfied, Sad, Nervous; //creates string
System.out.println("How are you feeling today? Pick one of the following:");
System.out.println("");
System.out.println("Happy");
System.out.println("Satisfied");
System.out.println("Sad");
System.out.println("Nervous");
mood = game.nextLine();
if (mood = Satisfied) { //I NEED HELP WITH THIS. IT TELLS ME I NEED TO "Insert '!= null' check". WHY? THANKS IN ADVANCE!
g2d.setColor(Color.YELLOW); //sets color
g2d.fillOval(350, 15, 25, 70); //draws oval
g2d.fillOval(400, 15, 25, 70); //draws oval
g2d.setColor(Color.RED); //sets color
g2d.fillRect(350, 120, 77, 25); //draws rectangle
}
}
public static void main(String[] args) { //sets this class as main
JFrame frame = new JFrame("Ping Boom"); //sets title
frame.add(new Game2());
frame.setSize(800, 500); //sets size of window
frame.setVisible(true); //sets visibility as a boolean
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //sets the ability for the window to close on command
}
}
Upvotes: 0
Views: 3160
Reputation: 21
in string you cannot use = sign if you to match string you can use .equals or you can use .contains, for your question .equals will work.
if(mood.equals(satisfied))
and you can also use .equalsIgnoreCase(anotherString)
Upvotes: 0
Reputation: 2082
Firstly, some information about how Java (and languages like it) works:
In general, when you put 1myvariable = value1 (anywhere), it is as if you called a function that assigns myvariable
the value of value
. But remember that functions can return values. So when you do an assignment, the "assignment function" is called to assign the given value to the given variable, and the variable with its new value is returned. This is why you can do stuff like a = b = c = d;
You probably know that if statements work by evaluating what is in the parentheses to a value of true or false, and if that value is true, then the body of the if statement executes. That is only partly true. Underneath everything, boolean (true/false) values are just numbers. 0 is false, and anything else is true. So what an if statement checks is if the value in the parentheses is not equal to 0, then it will execute the body of the loop.
All objects in Java are kept track of through the use of references. References in Java are memory addresses, where null
is equal to 0 and all references that represent non-null objects are equal to positive hexadecimal numbers. A String
is an object, and you are keeping track of a String
through your variable mood
. The variable used to keep track of an object is a reference, which is just a memory address, which is essentially just a number.
In your if statement, you are assigning the value of Satisfied
to the variable mood
, just as you would if it were not in an if statement. But remember what I said earlier. Assignments are like function calls that return the variable with its new value. So in your if statement, the assignment you are currently doing evaluates to the value of the variable mood
after the assignment, which is the memory address of the String Satisfied
, which is actually just a number.
Satisfied
is an object that exists, so it is clearly not null
, and null is equivalent to 0, or false. So if something is not false, then it must be true, and therefore your if statement executes its body.
The reason you are getting an error is that type of construction, where an assignment happens inside a statement that evaluates to a boolean value, is considered bad programming because you don't know if only one equals sign was an accident or if the programmer actually meant to do it. Java agrees with this logic, and therefore instead of letting you test for the reference being 0 (null
) by default, it forces you to acknowledge the fact that that is what you actually meant to do by forcing you to put a != null
after it. In doing this, you are explicitly saying "yes, I meant to do that assignment and not a comparison".
What you meant to do in this situation I am assuming is compare the two strings, not assign them. When working with objects (and by extension, references), the ==
operator compares the references themselves, not the objects they represent (because references are just memory addresses, aka numbers). So what you want in this case is:
if (mood.equals(Satisfied))
{
// do stuff
}
The .equals() method does a comparison on the objects the references represent, giving you what you want.
Upvotes: 0
Reputation: 38521
In order to check if mood equals Satisfied:
change:
if (mood = Satisfied)
to:
if (mood.equals(Satisfied))
Upvotes: 0
Reputation: 1188
in the line to create String
, define their content as well, so instead of
String mood, Happy, Satisfied, Sad, Nervous; //creates string
that only declare the variable without value, do the following:
String happy = "Happy";
String satisfied = "Satisfied";
String sad = "Sad";
String nervous = "Nervous";
and for string comparison, to use .equals
or .equalsIgnoreCase
instead of =
. Btw, the correct operator for comparison is ==
. For difference between ==
and .equals
refer to this question.
if (mood.equalsIgnoreCase(happy)) {
// do happy things
} else if (mood.equalsIgnoreCase(satisfied)) {
// do satisfied things
}
As a note, this kind of "fixed number of things" is better to be represented as enum:
enum Mood {
Happy, Satisfied, Sad, Nervous;
}
and later you can use switch
statement:
Mood mood;
// codes here to assign mood
switch (mood) {
case Happy:
// do happy things
case Satisfied:
// do satisfied things
case Sad:
// do sad things
case Nervous:
// do nervous things
}
one tutorial for enum to kickstart your reading on enum: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
Upvotes: 1
Reputation: 44814
You have declared both mood
and Satisfied
as String
objects, but you only ever assign a value to mood
, so Satisfied
will always be unassigned
Also, the way to check equality of Strings is to use the String.equals (otherString)
method
try
String satisfied = "Satisfied";
....
if (mood.equals (satisfied )) {
...
}
Upvotes: 2