![]() |
|
|
|||||||
![]() |
|
|
Thread Tools | Rate Thread |
|
|
PM User | #1 |
|
New Coder ![]() Join Date: Nov 2007
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
Using token string in if statement
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.
Code:
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.");
}
}
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? |
|
|
|
|
|
PM User | #2 |
|
God Emperor ![]() ![]() Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 5,123
Thanks: 2
Thanked 554 Times in 542 Posts
![]() ![]() ![]() |
Token is a string object, you cannot compare using ==. == in this context will match the address for the variable.
Instead, use token.equals("insert") for a comparison.
__________________
Code:
struct User *upFou;
userInit(upFou, "Fou-Lu", 1);
printf("%s has %s to %s\n", (*upFou).Name, !quitSmoking(upFou) ? "FAILED" : "SUCCEEDED", (*upFou).Smoker == 1 ? "FAIL" : "PASS");
// Fou-Lu has FAILED to FAIL? Lol
|
|
|
|
|
|
PM User | #4 |
|
God Emperor ![]() ![]() Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 5,123
Thanks: 2
Thanked 554 Times in 542 Posts
![]() ![]() ![]() |
Yep, you can alter that if you code php as if it were more typestrong:
PHP Code:
__________________
Code:
struct User *upFou;
userInit(upFou, "Fou-Lu", 1);
printf("%s has %s to %s\n", (*upFou).Name, !quitSmoking(upFou) ? "FAILED" : "SUCCEEDED", (*upFou).Smoker == 1 ? "FAIL" : "PASS");
// Fou-Lu has FAILED to FAIL? Lol
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Rate This Thread | |
|
|