*
The moose likes Beginning Java and the fly likes Validation:  check to see whether it is all integers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Validation:  check to see whether it is all integers" Watch "Validation:  check to see whether it is all integers" New topic
Author

Validation: check to see whether it is all integers

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
I am receiving an account number which is integers.
If it is not entered as an integer I wish to state not valid and re-enter account number.
If it is all ok I wish to convert all the integer values to a String.

Is it possible to say if accountNumber not equal to an integer?



Am I going in the wrong direction here?
Prashanth Lingala
Ranch Hand

Joined: Nov 13, 2004
Posts: 66
if (newAccNo == int)

int is a reserved word...just not possible...


Have A Nice Day !!!
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
>I am receiving an account number which is integers.
>If it is not entered as an integer

then it must be received in some form other than an integer.

a simple way <aircode>

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Ok, I had ago at this.

I have the following code in my constructor:



However, I get the following error when compiling:

C:\java\BankAccount>javac Current.java
Current.java:28: cannot resolve symbol
symbol : method parseInt (int)
location: class java.lang.Integer
newAccNo = Integer.parseInt(newAccNo);

I also noted your comment with:


however, I kept this in and didn't use the message to enter an integer:


as my input is coming into the constructor from the TestClass i.e.


In the test class the current account number in the example is primitive however I wanted to validate whether it was all numbers/integers and if not then say invalid account number. This is for future use.

Thanking you in advance!
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489


as my input is coming into the constructor from the TestClass i.e.

code:
--------------------------------------------------------------------------------

Current currentAcct = new Current ("Maureen Charlton", 12345);

--------------------------------------------------------------------------------


in which case you really do not need to validate the account number. The constructor accepts a String and an int. Any attempt to pass anything else other than an int would cause a compilation error.

Some other points.

1. Consider moving the kind of work that's in your constructor to a private method and call that method from your constructor.

2. Again what is it that you seek to achieve by the while loop. You would ideally have a loop such as the one you have written when you have multiple values to check. Inside the constructor, there is only one account number.

3. your catch statement jusr prints out a warning - this is dangerous practise.

All the above's general advice. As mentioned earlier, in this particular case, you need not check for integer in the constructor as java would throw an exception if you attempt to pass anything else.

Thanks,
ram.
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Many, many thanks for your reply.

I have changed my constructor to the following:


And I have moved various things to other methods.

Still irrated at the following:

Suppose I have a String coming in i.e "1234L"
And I wish to check whether or not the String only contains numbers...
i.e. excludes everything else other than integers

Would I:


Also you mention about not using warning messages? Could you elaborate here i.e. why not? And what is the alternative?

Thanking you in advance!
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489


Suppose I have a String coming in i.e "1234L"
And I wish to check whether or not the String only contains numbers...
i.e. excludes everything else other than integers


Just do Integer.parseInt(input);
This wopuld throw a NumberFormatException, catch this exception, inform the user about the error and exit.


Also you mention about not using warning messages? Could you elaborate here i.e. why not? And what is the alternative?


OK, this is about the way you code your catch blocks. All you do in your catch blocks is print out an error msg. The program would do exactly that and continue with executing the rest of the code where it may potentially fail.

take a look at the following




Hence ideally your prog ought to have been



Exception handling is a feature which allows the programmer to take corrective action and continue with the program. For example lets assume Iam reading from a file and displaying contents. When the file's not found I read from a default file.



In your program, all you could/should do is exit if the account no is invalid.

ram.
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
Ramprasad Madathil,

Many thanks for your reply post. It was very helpful!

I have successfully implemented your suggestions using the following code:


Please note that the System.out.prinln's are there for testing purposes only.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Validation: check to see whether it is all integers