I want to check if the user inputed in a string, and if he does I want to print it out or warn him to enter a string next time. I know that my problem is with the condition in the if statement but I don't know which method I should use in this case. I use hasNextInt
for Integer
s but from the look of it this doesn't work like I would hope with String
.
EDIT: Just to clarify from the comments: the text should hold only a specific character set like a-z, A-Z and spaces.
import java.util.Scanner;
public class JavaLessonTwoTest {
static Scanner userInput = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("Enter string please: ");
if (userInput.hasNext(String("")) {
String stringEntered = userInput.next();
System.out.println("You entered " + stringEntered);
} else {
System.out.println("Enter a string next time.");
}
}
}