Results 1 to 2 of 2
  1. #1
    dfarni is offline Member
    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

    Default Validating user input

    I cant figure this out, it has to be incredibly simple. All I want to do is verify the users input. It seems easy, but no matter the input the if statment is always false.

    Java Code:
    class Cube {
    	  public static void main (String args[]) throws IOException {
    		  int x=2;
    		  System.out.println("Please enter test");
    		  
    		  BufferedReader dataIn= new BufferedReader(new InputStreamReader(System.in));
    		  
    		  //while(x > 1){
    		  String move = dataIn.readLine();
    		  move = move.toUpperCase();
    		  System.out.println(move);
    		  if(move == "TEST"){// || move == "O"){
    			  System.out.println("You've entered an test");				  
    		  }
    		  else {
    			  System.out.println("Not test");
    		  }
    		}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Never use == to compare Strings or anyother Objects (unless you are testing for identity equality, which means testing that the variables refer to the same Object).

    Use the .equals(...) method.
    Java Code:
    if (move.equals("TEST")) { // or if ("TEST".equals(move))
    Also, your formatting could do with improvement. Read Code Conventions for the Java(TM) Programming Language: Contents

    db

Similar Threads

  1. User Input???
    By jonytek in forum New To Java
    Replies: 8
    Last Post: 01-13-2013, 03:52 PM
  2. Help with validating input into an array
    By kcoltharp in forum New To Java
    Replies: 1
    Last Post: 02-25-2011, 10:05 AM
  3. Help with user input
    By sconniegorilla in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 03:00 PM
  4. SDF validating date input
    By getshum in forum New To Java
    Replies: 2
    Last Post: 02-03-2011, 09:37 AM
  5. User input- Pop Up Box
    By dedachi in forum AWT / Swing
    Replies: 3
    Last Post: 03-23-2009, 05:47 AM

Posting Permissions

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