2

I'm trying to teach myself java by writing a simple weekly planner program and the if statment in my code is giving me grief, here is my code:

import java.util.Scanner; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class Week { public static void main(String[] args) throws IOException { Scanner inorout = new Scanner(System.in); System.out.println("Do you want to read or write (r/w)"); String decision = inorout.nextLine(); System.out.println(decision); if(decision == "r") { System.out.println("It has gone into the if"); Scanner namer = new Scanner(System.in); System.out.println("What is the name of the week you want to read?"); String r = namer.nextLine(); Scanner s = new Scanner(new File(r + ".txt")); System.out.println("What is Your Name?"); String name = s.nextLine(); System.out.println("Welcome " + name); } } } 

When I compile the program using Drjava and input "r" it doesn't run the if statment, and i'm at a loss as to whats wrong, help would be much appriciated

2
  • Please format the code using the code tag. Commented Jul 11, 2011 at 5:37
  • @Jarred looking at your comment as in if(decision equals "r") it seems you do not have even basic understanding of Java syntax. -1 Commented Jul 11, 2011 at 5:48

9 Answers 9

8

Strings in Java are compared usually with equals not with ==.

Sign up to request clarification or add additional context in comments.

4 Comments

Not usually. Always. If you use == you're not comparing a String, you're comparing its pointer.
as in if(decision equals "r") ? because thats throwing up errors =/
@aroth: Thats not completely true. Strings are also immutable so if two strings have the same reference they will have also the same Object value. But i said usually because in 95% you will compare the values of strings with equals.
@aroth: in extremely unusual situations, you could use intern() to ensure that object equality is equivalent to reference equality, slightly improving the comparison speed. This is almost never a useful thing to do, but it's worth being aware of.
3

The String decision will not have reference-equality with the literal "r". You need to use String.equals method.

1 Comment

Also note that a good habit to get into is to write code like "r".equals(decision) instead of decision.equals("r"). The former will never throw a NullPointerException, the latter might.
2

You should use decision.equals("r").

== check for equality of the references to the objects, not for equality of the objects themselves.

Comments

2

As others have said, you need to use if(decision.equals("r")) instead of if(decision == "r").

Here's a brief explanation on how string references work in Java:

The String class keeps a pool of strings that are currently in use, and if a literal "r" was assigned to decision, or decision was internalized (put into the string pool) using .intern(), it will fetch it from the string pool and the references will be equal. But because decision is set at runtime using user input, it will not be internalized automatically, and the references will not be equal.

In short, in some situations decision == "r" can evaluate to true. But don't rely on it. Use .equals() instead.

Comments

1
if(decision == "r") 

== checks for object equality, not string equality you instead need to use the equals() method

if(decision.equals("r")) 

Comments

0

Just want to point out that while the decision.equals("r") suggested in many answers will work just fine, it is equally valid to say:

if ("r".equals(decision)) { //do something... } 

The benefit of this is that you don't have to worry about "r" ever being null. Also note that you may want to do a case-insensitive comparison in this case (so the user can type 'R' if they want), like:

if ("r".equalsIgnoreCase(decision)) { //do something... } 

Comments

0

Try using something like:

if(decision.equals("r")) 

1 Comment

cheers to all who preovided answers, this solved the problem =)
0

you should use the equals method to compare the two strings,the string is a object,this is a reference,the equals() method will compare the content of two strings,but the == will compare the address of two strings

so,you should write like this:

if ("r".equals(decision)) { //do something code... } 

Comments

0

Equality vs. Identity

You check for identical references (==), or equal objects (.equals()).

Primitive data types (Int, Float, etc.) and references are compaired with ==, complex data types (any normal or self-defined class, String, any object intantiatable by hand via new) have to be compaired by object1.equals(object2).

Strings for example can return false positives for ==, as can be seen in this runnable example here: (copy&paste into File StringComparisonMain.java, compile, run)

/** * Difference between identity and equality in java. */ public class StringComparisonMain { public static void main(String[] args) { /* * * CREATE TWO DIFFERENT REFERENCES TO TWO DIFFERENT BUT EQUAL STRING * OBJECTS * * 'two' must be created like this else test will create false * positives: one = "asdf"; two = "asdf"; ... will falsely yield 'true' * for identity check. This is due to optimizations going in inside the * JRE, I believe. */ String one = "bla"; String two; two = "b" + "l"; two = two + "a"; String three = "bla"; /* * ACTUAL TESTING CODE */ System.out.println("different reference leading to equal objects:"); stringCompare(one, two); System.out.println("as above, but false positive: (!)"); stringCompare(one, three); // changing to same references to the same String object one = two; System.out.println("identical references to same single object:"); stringCompare(one, two); // different Strings (and ofc different objects...) one = "test"; System.out.println("different Strings/objects:"); stringCompare(one, two); System.out.println("\nDONE!"); } private static void stringCompare(String stringOne, String stringTwo) { // first test for identity (reference to same object) if (stringOne == stringTwo) { System.out.println("== true"); } else { System.out.println("== false"); } // second test for equality (two referenced objects are equal) if (stringOne.equals(stringTwo)) { System.out.println("equals true"); } else { System.out.println("equals false"); } // there is not just 'System.out.println' out there... System.out.print("\n"); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.