Reputation: 47
I am learning JAVA and trying to write my first loop. The loop should prompt the user to guess a defined name.
The code is not performing right. I have tried to search for help on different JAVA tuturials both not found any examples where you guess a name/string but a lot where you should guess a number.
This is my code:
/**
*
* @author mso_
*/
import java.util.Scanner;
public class GuessName {
/**
* @param args the command line arguments
*/
public static final int C_Max_Trials = 10;
public static void main(String[] args) {
//Define correct name
String name = "Morten";
String guessName;
//Create a scanner
Scanner guess = new Scanner(System.in);
//Recieve a guess
do {
System.out.println("Please guess my name. Enter your guess here: ");
String guessName = guess.next(); <-- ERROR
//Create loop
} while (guessName != name); <-- ERROR
System.out.println("Sorry, wrong guess, please enter another guess: ");
if (guessName = name); <-- ERROR
System.out.println("Right on! ");
}
}
What have I done wrong?
Upvotes: 1
Views: 610
Reputation: 26597
You can't compare Strings like this. This will only compare the references. You must use the equals() method :
while (! guessName.equals(name));
A little explanation : http://www.zparacha.com/java-string-comparison/
There is another error in your code, you try to redeclare guessName inside the loop. You must declare guessName only once outside of the loop (ie before the do {
).
Thirdly, there's a some other errors in your code. I think all of them were pointed out in the others answer, but I'll do a quick list :
if (guessName = name);
This is a useless statement as is, you must open a block : if(condition) { statement; }
System.out.println()
won't be executed when you think. Re-read the doc about do { } while() loop until you really understand them.My advise : read carefully the error message of your compiler and read some doc before writing code.
Upvotes: 3
Reputation: 10541
Here is what the javac
compiler says:
GuessName.java:26: guessName is already defined in main(java.lang.String[])
String guessName = guess.next(); //<-- ERROR
^
GuessName.java:32: incompatible types
found : java.lang.String
required: boolean
if (guessName = name);// <-- ERROR
^
2 errors
For the first one, you should not declare another local variable: drop the String
. For the second, use .equals
to compare String
objects, as the doc says.
You will obtain something like that:
...
//Recieve a guess
do {
System.out.println("Please guess my name. Enter your guess here: ");
guessName = guess.next(); //<-- FIXED
//Create loop
} while (!guessName.equals(name)) //<-- FIXED
if (guessName.equals(name))// <-- FIXED
System.out.println("Right on! ");
}
...
which works correctly.
Upvotes: 0
Reputation: 7613
The line String guessName = guess.next();
needs to be changed to:
guessName = guess.next();
Because "guessName" is already defined earlier.
Additionally, when you compare strings you need to use the method equals
and not the operator ==
. So } while (guessName != name);
should be:
} while (!guessName.equals(name));
And if (guessName = name);
should be changed to:
if (guessName.equals(name))
Upvotes: 0
Reputation: 13423
Try this
public class GuessName {
/**
* @param args the command line arguments
*/
public static final int C_Max_Trials = 10;
public static void main(String[] args) {
//Define correct name
String name = "Morten";
String guessName = null;
//Create a scanner
Scanner guess = new Scanner(System.in);
//Recieve a guess
do {
System.out.println("Please guess my name. Enter your guess here: ");
guessName = guess.nextLine(); <-- ERROR
if(!guessName.equals(name))
{
System.out.println("Sorry, wrong guess, please enter another guess: ");
}
//Create loop
} while (!guessName.equals(name)); <-- ERROR
if (guessName.equals(name)) <-- ERROR
System.out.println("Right on! ");
}
}
Upvotes: 0
Reputation: 7332
Among other things there is a wrong semicolon after the if:
if (name.equals(guessName)) //removed the semicolon and use .equals
System.out.println("Right on! ");
and it's an assignment instead of a comparison as other answers state by now.
Upvotes: 0
Reputation: 103797
That won't compile for a start - you declare String guessName
on the third line of the main method, and then declare the same variable again within the do
loop. You should simply use the name of the variable to assign to it:
guessName = guess.next();
Because of this error, presumably your IDE's compiler doesn't see the variable at all, so the subsequent lines that refer to guessName
are also flagged as errors. Fixing this first line should clear those up, unless there's another problem lying there that I've missed.
Upvotes: 0
Reputation: 24150
you are comparing two objects. Not two string values.
use equals
Upvotes: 0
Reputation: 38195
You're comparing references (well, with guessName = name
, you're actually assigning a value to guessName). Use String.equals()
instead of ==
and !=
.
Upvotes: 0
Reputation: 73484
You are using the assignment operator and not the equivalence operator at
if (guessName = name)
you need to do
if(guessName.equals(name))
instead.
Upvotes: 0