• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Invalid input integer.

 
Yani Abela
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 )

 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yani,

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?
 
Yani Abela
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
umm... i don t know how to do that can you please give me an example?? thanks
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Yani Abela
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is sample code:

I hope this helps.
 
Yani Abela
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your replies

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
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yani,

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
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:


nextInt() method is called twice; which means you check on one input but store a different one.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops. I put wrong code
Here is the one:
 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don’t use an if else statement. Use a while loop. Look at my previous post and there is an example in one of the links.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don’t use an if else statement. Use a while loop.

That was elegant. Thanks
 
Yani Abela
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ??

Thanks
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yani,

As John mentioned, there's an issue with my old code. Check the latest code from my post, or even better, follow Campbell's suggestion.
 
Yani Abela
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:Oops. I put wrong code
Here is the one:


BUT id has to initalised. Is it good if I do it --> int id = sc.nextInt;
thanks
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yani Abela wrote:BUT id has to initalised. Is it good if I do it --> int id = sc.nextInt;

It is getting initialized in the if condition (id = sc.nextInt()) > 0 in the example he gave. So you should not get any error.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote: . . .
That was elegant.
It’s a standard technique which people have to learn.
Thanks
You’re welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic