Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


[split] User input check filter_list
Author
Message
[split] User input check #1
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 :
  • if i enter blackeagle it will say then hello blackeagle
  • if i enter 123456 it will say hello 123456

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 ?
[Image: blackeagle_zps6ad86521.gif]

Reply

[split] User input check #2
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 :
  • if i enter blackeagle it will say then hello blackeagle
  • if i enter 123456 it will say hello 123456

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 ?
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: Chatbot #3
(10-06-2013, 12:14 PM)blackeagle Wrote: 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 :
  • if i enter blackeagle it will say then hello blackeagle
  • if i enter 123456 it will say hello 123456

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 ?

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;  
}
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Chatbot #4
(10-06-2013, 12:14 PM)blackeagle Wrote: 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 :
  • if i enter blackeagle it will say then hello blackeagle
  • if i enter 123456 it will say hello 123456

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 ?

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;  
}
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Chatbot #5
@Deque thank you Smile 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 ?
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: Chatbot #6
@Deque thank you Smile 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 ?
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: Chatbot #7
(10-06-2013, 12:56 PM)blackeagle Wrote: @Deque thank you Smile 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 ?

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...%28char%29
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Chatbot #8
(10-06-2013, 12:56 PM)blackeagle Wrote: @Deque thank you Smile 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 ?

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...%28char%29
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Chatbot #9
@Deque so if i understood right i can use for and is letter to check if every character is a letter !!
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: Chatbot #10
@Deque so if i understood right i can use for and is letter to check if every character is a letter !!
[Image: blackeagle_zps6ad86521.gif]

Reply







Users browsing this thread: 3 Guest(s)