Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 11-04-2009, 08:28 PM   PM User | #1
epheterson
New Coder

 
Join Date: Nov 2007
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
epheterson is an unknown quantity at this point
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.");
			}
					

			
		}
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?
epheterson is offline   Reply With Quote
Old 11-04-2009, 11:15 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 5,123
Thanks: 2
Thanked 554 Times in 542 Posts
Fou-Lu is a jewel in the roughFou-Lu is a jewel in the roughFou-Lu is a jewel in the rough
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
Fou-Lu is offline   Reply With Quote
Old 11-05-2009, 03:37 AM   PM User | #3
epheterson
New Coder

 
Join Date: Nov 2007
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
epheterson is an unknown quantity at this point
Thank you! Works like a charm, I guess I'm too used to PHP
epheterson is offline   Reply With Quote
Old 11-05-2009, 03:46 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 5,123
Thanks: 2
Thanked 554 Times in 542 Posts
Fou-Lu is a jewel in the roughFou-Lu is a jewel in the roughFou-Lu is a jewel in the rough
Yep, you can alter that if you code php as if it were more typestrong:
PHP Code:
if (=== strcmp($myVar'search'))
{
    
// Indicates they are equal

Us PHP developers are just spoiled with the loose comparisons
__________________
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
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:15 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.