![]() |
|
[split] User input check - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Java, JVM, & JRE (https://sinister.li/Forum-Java-JVM-JRE) +--- Thread: [split] User input check (/Thread-split-User-input-check) Pages:
1
2
|
[split] User input check - blackeagle - 10-06-2013 now it's me who have a question !! Code: System.out.println("Hello there what's your name");
personsName = in.nextLine();
System.out.println("hello " + personsName);in this code it will ask the user to enter his name :
what i wish to do is if the user enter a number instead of a name he gets an error and re-ask him to add his name i already know that i need to use while or do while for repeating the question but what to do to make it forbidden to use a number instead of a name ? [split] User input check - blackeagle - 10-06-2013 now it's me who have a question !! Code: System.out.println("Hello there what's your name");
personsName = in.nextLine();
System.out.println("hello " + personsName);in this code it will ask the user to enter his name :
what i wish to do is if the user enter a number instead of a name he gets an error and re-ask him to add his name i already know that i need to use while or do while for repeating the question but what to do to make it forbidden to use a number instead of a name ? RE: Chatbot - Deque - 10-06-2013 (10-06-2013, 12:14 PM)blackeagle Wrote: now it's me who have a question !! 1. Check if the input string is a number. 2. If it is a number repeat the question, otherwise go on as usual How to check if it is a number: Code: public static boolean isNumber(String input) {
try {
Double.parseDouble(input);
} catch(NumberFormatException e) {
return false;
}
return true;
}RE: Chatbot - Deque - 10-06-2013 (10-06-2013, 12:14 PM)blackeagle Wrote: now it's me who have a question !! 1. Check if the input string is a number. 2. If it is a number repeat the question, otherwise go on as usual How to check if it is a number: Code: public static boolean isNumber(String input) {
try {
Double.parseDouble(input);
} catch(NumberFormatException e) {
return false;
}
return true;
}RE: Chatbot - blackeagle - 10-06-2013 @Deque thank you i was thinking about another way to do it cs i don't know how to use try and catch yet i'll be learning it soon so is there any other way to test it ?
RE: Chatbot - blackeagle - 10-06-2013 @Deque thank you i was thinking about another way to do it cs i don't know how to use try and catch yet i'll be learning it soon so is there any other way to test it ?
RE: Chatbot - Deque - 10-06-2013 (10-06-2013, 12:56 PM)blackeagle Wrote: @Deque thank you Yes, regex, but they are more difficult to understand than the solution above. But for your special case you could try a different approach. You want to ensure that the string given is a name. So you have to check if it is not -- and it doesn't matter if it is a number instead or something else that isn't valid. So you might just check for invalid characters. Use input.toCharArray() to retrieve the char array of the string given and iterate through every character if it meets your conditions. I.e. you could say: I only allow the use of a-zA-Z and space for the name. So you would if just check if every character is a letter or a space. The Character class of Java has the method isLetter() that will help you with this: http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isLetter%28char%29 RE: Chatbot - Deque - 10-06-2013 (10-06-2013, 12:56 PM)blackeagle Wrote: @Deque thank you Yes, regex, but they are more difficult to understand than the solution above. But for your special case you could try a different approach. You want to ensure that the string given is a name. So you have to check if it is not -- and it doesn't matter if it is a number instead or something else that isn't valid. So you might just check for invalid characters. Use input.toCharArray() to retrieve the char array of the string given and iterate through every character if it meets your conditions. I.e. you could say: I only allow the use of a-zA-Z and space for the name. So you would if just check if every character is a letter or a space. The Character class of Java has the method isLetter() that will help you with this: http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isLetter%28char%29 RE: Chatbot - blackeagle - 10-06-2013 @Deque so if i understood right i can use for and is letter to check if every character is a letter !! RE: Chatbot - blackeagle - 10-06-2013 @Deque so if i understood right i can use for and is letter to check if every character is a letter !! |