Results 1 to 5 of 5
  1. #1
    Ecthelion II is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Question Text Input to If Statement?

    Hi guys! I just recently started coding in java, and I have a question.

    How do I put text input into an if statement? Like, (after all the Scanner stuff) if you type "start", it will say something? I can put text input into a statement (ex. "Enter your name" and the it says "Hello (whatever you entered)"), but I don't know how use it in an if statement. Any help will be greatly appreciated!

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: Text Input to If Statement?

    What's your try? What`s your problem?
    Java Code:
    if("start".equals(inputString)) {
    doSomething();
    }
    .....remember strings are objects, and thus you have to compare the content with equals and not with == (if that was your try :D)

  3. #3
    Ecthelion II is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default Re: Text Input to If Statement?

    This is what I tried:

    PHP Code:
    import java.util.Scanner;
    
    public class apples {
    	public static void main(String args[]){
    		Scanner input = new Scanner(System.in);
    		System.out.println("Type start to begin!");
    		if("start".equals(input)){
    			System.out.println("Hi.");
    		}else{
    		} 
    	}
    }
    But it doesn't seem to let me type anything.
    Just remember, this is my first language, so...

  4. #4
    rosishere is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default Re: Text Input to If Statement?

    import java.util.Scanner;
    class a{
    public static void main(String[] args){
    Scanner a = new Scanner(System.in);
    System.out.println("yes or no?");

    if(a.equals("yes")){
    System.out.println("yes");
    }
    else{
    System.out.println("no");
    }

    }
    }

    same here, everything works, but i can't type.
    Last edited by rosishere; 07-02-2012 at 11:35 PM. Reason: space

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default Re: Text Input to If Statement?

    Scanners are not strings, so it won't be equal to a string. In fact, your program would just exit immediately, because you're not reading anything from the console. The scanner reads strings from an input source, in your case the console, but you have call a method to get it to read something. What you need to do is call nextLine() to read a line of text. To get the input, use something like this:

    Java Code:
    Scanner input = new Scanner(System.in);
    String inputString = input.nextLine();
    Then use the String from nextLine() in your if statement, and you should get what you want.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Replies: 8
    Last Post: 11-18-2011, 06:38 PM
  2. Replies: 1
    Last Post: 11-14-2011, 01:03 PM
  3. Input statement gets skipped
    By rjkfsm in forum New To Java
    Replies: 2
    Last Post: 05-15-2011, 07:45 PM
  4. End Scanner int input with text value
    By BillyB in forum New To Java
    Replies: 3
    Last Post: 03-07-2011, 05:13 PM
  5. Trying to workout how to input text
    By mcollins in forum New To Java
    Replies: 1
    Last Post: 03-03-2009, 05:45 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
  •