Results 1 to 5 of 5
Thread: Text Input to If Statement?
- 07-02-2012, 11:27 PM #1
Member
- Join Date
- Jun 2012
- Posts
- 5
- Rep Power
- 0
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!
- 07-02-2012, 11:31 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 785
- Rep Power
- 12
Re: Text Input to If Statement?
What's your try? What`s your problem?
Java Code:if("start".equals(inputString)) { doSomething(); }
- 07-02-2012, 11:43 PM #3
Member
- Join Date
- Jun 2012
- Posts
- 5
- Rep Power
- 0
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{ } } }
Just remember, this is my first language, so...
- 07-03-2012, 12:34 AM #4
Member
- Join Date
- Jul 2012
- Posts
- 4
- Rep Power
- 0
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-03-2012 at 12:35 AM. Reason: space
- 07-03-2012, 03:04 AM #5
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 12
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();
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
-
Interget instead of String input and If Statement re-loop?
By FadedAura in forum New To JavaReplies: 8Last Post: 11-18-2011, 07:38 PM -
Getting input from 3 input text boxes and store into 1 values
By niksipandit in forum New To JavaReplies: 1Last Post: 11-14-2011, 02:03 PM -
Input statement gets skipped
By rjkfsm in forum New To JavaReplies: 2Last Post: 05-15-2011, 08:45 PM -
End Scanner int input with text value
By BillyB in forum New To JavaReplies: 3Last Post: 03-07-2011, 06:13 PM -
Trying to workout how to input text
By mcollins in forum New To JavaReplies: 1Last Post: 03-03-2009, 06:45 AM
Bookmarks