5 Replies - 559 Views - Last Post: 20 August 2013 - 08:06 PM Rate Topic: -----

#1 tigran_tal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 30-July 13

newbie question :user input and strings

Posted 20 August 2013 - 07:19 PM

hi

i am positive i am doing something wrong here..the program always exits. my question is how do i compare strings with user input?

import java.util.*;
public class RoughWork {

	
public static void main(String args[]){
			       
Scanner readInput = new Scanner(System.in);
				
System.out.println("Please enter a word");
String word = readInput.next();
				
if (word == "john")
System.out.println("thats me");
				
else 

System.exit(0);
				
				
				
				
			        }
			    }




Is This A Good Question/Topic? 0
  • +

Replies To: newbie question :user input and strings

#2 StrongJoshua  Icon User is offline

  • D.I.C Head

Reputation: 48
  • View blog
  • Posts: 170
  • Joined: 19-July 13

Re: newbie question :user input and strings

Posted 20 August 2013 - 07:21 PM

For Strings you need to use
if(word.equals("john"))
  System.out.println("That's me");


Strings are objects so what you are doing is comparing reference values, not the actual String. Hope this helps :)
Was This Post Helpful? 2
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12135
  • View blog
  • Posts: 45,119
  • Joined: 27-December 08

Re: newbie question :user input and strings

Posted 20 August 2013 - 07:22 PM

You need to use the String equals() method rather than the == operator. The equals() method compares Strings based on their literal values (the text), whereas the == operator compares them based on their locations in memory.

So: if(word.equals("John")).

Also, you should work on adopting an indentation convention. Your code is a tad messy and hard to read. That makes it more difficult to debug, follow the logic, etc.
Was This Post Helpful? 2
  • +
  • -

#4 ChrisNt  Icon User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 894
  • Joined: 31-July 13

Re: newbie question :user input and strings

Posted 20 August 2013 - 07:22 PM

To compare Strings use method equals
if (name.equals("John") ) { }




P.S
Sorry guys i didnt see it was already answered :P

This post has been edited by ChrisNt: 20 August 2013 - 07:45 PM

Was This Post Helpful? 2
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12135
  • View blog
  • Posts: 45,119
  • Joined: 27-December 08

Re: newbie question :user input and strings

Posted 20 August 2013 - 07:39 PM

@ChrisNt: Don't forget the end parenthesis here: if( name.equals("John"). :)
Was This Post Helpful? 2
  • +
  • -

#6 tigran_tal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 30-July 13

Re: newbie question :user input and strings

Posted 20 August 2013 - 08:06 PM

thanks people that helped
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1