Results 1 to 3 of 3
  1. #1
    dn54321 is offline Member
    Join Date
    Jul 2014
    Posts
    2
    Rep Power
    0

    Default True & False & Scanner

    So, i need a little help D: i was just wondering how i can fix this error and what i did wrong? 2 questions :P
    My goal is to ask them for their name and if they got it right then it says yes while if they wrote it wrong, it would say no.

    Java Code:
    	public static void main(String[] args) {
    		Scanner name = new Scanner(System.in);
    		
    		System.out.println("What is your name?");
    		String sname = name.nextLine();
    		name.close();
    	System.out.println("Are you sure it's " + sname + "?");
    	Scanner tfname = new Scanner(System.in);
    	String stfname = tfname.next();
    	
    	if (stfname == "yes") { 
    		System.out.println("yes!");
    	}	
    	else if (stfname == "no") {
    		System.out.println("no :C");
    	}
    	
    
    	tfname.close();
    		}

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    877
    Rep Power
    8

    Default Re: True & False & Scanner

    after receiving name why you are closing the scanner object?
    remove that close

    and check the string like this
    if (stfname.trim().equals("yes")) {
    System.out.println("yes!");
    }
    Ramya:cool:

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,289
    Blog Entries
    7
    Rep Power
    24

    Default Re: True & False & Scanner

    Don't compare two Strings for equality with the == operator; use the String.equals( ... ) method instead; this is Java 101

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. True and False Help
    By NBAPwns in forum New To Java
    Replies: 2
    Last Post: 12-19-2013, 01:55 PM
  2. How Do I Change True False Statements
    By RSdroid in forum New To Java
    Replies: 5
    Last Post: 12-16-2013, 04:53 PM
  3. Check a list , true false
    By schvuu in forum New To Java
    Replies: 1
    Last Post: 10-17-2013, 10:11 PM
  4. how to balance true and false instances per id ?
    By aneuryzma in forum New To Java
    Replies: 1
    Last Post: 03-27-2011, 02:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •