epheterson
11-04-2009, 07:28 PM
I have a scanner running, which correctly retrieves tokens how I want them (using white space as the delimiter). It prints out the tokens, as I'd like, to the screen, but when I run an if(token == "string"), using the exact output from the terminal, the if statement never runs.
while(s.hasNext())
{
String token = s.next();
System.out.println(token);
if(token == "insert")
{
// call insert command
System.out.println("Inserting");
}
else if(token == "process")
{
// call process command
System.out.println("Processing…");
}
else if(token == "count")
{
// call count command
System.out.println("Counting…");
}
else if(token == "list")
{
// call list command
System.out.println("Listing…");
}
else if(token == "priority")
{
// call priority command
System.out.println("Prioritizing…");
}
else
{
System.out.println("Chuck Norris can unscrable an egg.");
}
}
The output:
insert
Chuck Norris can unscrable an egg.
Smith,
Chuck Norris can unscrable an egg.
John
Chuck Norris can unscrable an egg.
1
Chuck Norris can unscrable an egg.
15
Chuck Norris can unscrable an egg.
insert
Chuck Norris can unscrable an egg.
Oldman,
Chuck Norris can unscrable an egg.
Richard
Chuck Norris can unscrable an egg....
Any ideas what I can change?
while(s.hasNext())
{
String token = s.next();
System.out.println(token);
if(token == "insert")
{
// call insert command
System.out.println("Inserting");
}
else if(token == "process")
{
// call process command
System.out.println("Processing…");
}
else if(token == "count")
{
// call count command
System.out.println("Counting…");
}
else if(token == "list")
{
// call list command
System.out.println("Listing…");
}
else if(token == "priority")
{
// call priority command
System.out.println("Prioritizing…");
}
else
{
System.out.println("Chuck Norris can unscrable an egg.");
}
}
The output:
insert
Chuck Norris can unscrable an egg.
Smith,
Chuck Norris can unscrable an egg.
John
Chuck Norris can unscrable an egg.
1
Chuck Norris can unscrable an egg.
15
Chuck Norris can unscrable an egg.
insert
Chuck Norris can unscrable an egg.
Oldman,
Chuck Norris can unscrable an egg.
Richard
Chuck Norris can unscrable an egg....
Any ideas what I can change?