hi there I am doing a java project. It is mainly a DVD rental system. In the client class I have the get and set methods. Now, my problem is that when I am adding a client to the arraylist, the program asks the user to enter the ID of the client. I have set the ID as an integer. Now, I want the program to inform the user that an invalid input has been entered when a negative number or a decimal number is entered. Can you please help??
This is what I did s far;
This is in the Runner class ;
case 1:
//Adding a new Client
Client newClient = new Client();
//Ask user to input details of client being added
System.out.println("Please enter ID");
newClient.SetId(sc.nextInt());
System.out.println("Please enter name");
newClient.SetName(sc.next());
System.out.println("Please enter surname");
newClient.SetSurname(sc.next());
System.out.println("Please enter email");
newClient.SetEmail(sc.next());
System.out.println("Please enter address");
newClient.SetAddress(sc.next());
System.out.println("Please enter mobile no.");
newClient.SetMobile(sc.nextInt());
//Add to DvdShop
myDvdShop.addClient(newClient);
break;
I don't know how to continue can somebody please help?? thanks . (Code snippets are really appreciated )
I believe that your code is not giving any compile time errors (i.e. SetId takes int as input parameter).
In that case, instead of saying
you can say
By the way, I assumed that you do not want ID to be a negative number. But I didn't at all get these two statements of yours:
Yani Abela wrote:1) I have set the ID as an integer
2) an invalid input has been entered when a negative number or a decimal number is entered
So, do you want valid ID to be non-decimal integer?
Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD)
Yani Abela
Greenhorn
Joined: Jan 30, 2012
Posts: 17
posted
0
Hi there Thanks very much for your reply it worked perfectly well for when entering a negative number.
But I still have another problem. I don't want the ID to be a decimal number. For example; if the user enters 5.5 as the ID number, the program gives an error that the input is invalid.
Hope this time I explained myself better! thanks again
John Jai
Rancher
Joined: May 31, 2011
Posts: 1372
posted
0
Yani Abela wrote:if the user enters 5.5 as the ID number, the program gives an error that the input is invalid.
You can check if it's a valid integer using hasNextInt() method of the Scanner class.
Yani Abela
Greenhorn
Joined: Jan 30, 2012
Posts: 17
posted
0
umm... i don t know how to do that can you please give me an example?? thanks
John Jai
Rancher
Joined: May 31, 2011
Posts: 1372
posted
0
Yani Abela
Greenhorn
Joined: Jan 30, 2012
Posts: 17
posted
0
thanks for your reply to be honest I did not understand your reply very well. Can you please give me an example regarding my problem please?? Sorry for asking you again but I am really confused!!
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 3141
posted
0
Yani Abela wrote:thanks for your reply to be honest I did not understand your reply very well.
What exactly didn't you understand? And did you read the docs for the classes and methods he used in his sample code?
if my criteria is that the number entered has to be and integer and has to be a positive number, I just add the other criteria in the if loop??
I did this but the output is not correct!! when I enter a negative number the program does nothing at all!!
here is how I tried to do it;
System.out.println("Please enter ID");
int id = sc.nextInt();
if(sc.hasNextInt() || id >0)
{
newClient.SetId(sc.nextInt());
}
else
{
System.out.println("Invalid!");
}
what am i doing wrong now?? I am really confused!!
John Jai
Rancher
Joined: May 31, 2011
Posts: 1372
posted
0
You want the input to be valid integer AND also not negative. But you have checked for OR in the condition.
<Edit> You are making mistake of reading the input twice. First check with hasNextInt() before reading
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 26720
posted
0
I would go farther and say you would do well to write a utility class. You will find an example here. That does something different from what you want, but it will give you a hint what a utility class looks like.
Rather than multiply methods, you would want methods like readIntFromKeyboard, and you can find old threads showing that sort of thing too. If you create such a utility class, you would give it a private static final Scanner field, which reads from the keyboard, and all methods would be static. If you need to use it in a multi-threaded application later, you would have to change the methods to prevent simultaneous access by two threads. You can pass messages like "Enter next number" and "That wasn't a number: try again" to the methods if you wish.
The problem is in if condition you've used. Due to or condition, it is checking that if number is integer or greater than 0. If at least one condition is satisfied, it goes inside. Thus, when you enter a negative integer, the number is integer condition is evaluated to true.
It should be
This is because technically, hasNextInt() should be called before nextInt(). (hint : assume user enters abcd in which case, your old code would throw java.util.InputMismatchException)
I hope this helps.
John Jai
Rancher
Joined: May 31, 2011
Posts: 1372
posted
0
Anayonkar Shivalkar wrote:
nextInt() method is called twice; which means you check on one input but store a different one.
Campbell Ritchie wrote:Don’t use an if else statement. Use a while loop.
That was elegant. Thanks
Yani Abela
Greenhorn
Joined: Jan 30, 2012
Posts: 17
posted
0
thanks for your replies I inserted the code you gave me. but why is the program reading an input twice?? What is happening is that the program prompts the user to enter the ID and i enter for example 6 and it does nothing until I insert another number!! why is that ??