• Post Reply Bookmark Topic Watch Topic
  • New Topic

EvenOrOdd --- Oddity  RSS feed

 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working through the cattle drive and currently working on the EvenOrOdd assignment. I can have the program determine whether a number is odd or even right up until I break the 11 character limit. Is there a limit as to how long the arguments from a command line can be? (I would post the code but that would allow others who are going through to have a short cut and all I wanted to know is if there is a limit to the characters).

Here is what I get from the command line.

C:\Java>java EvenOrOdd 1235
That is an ODD number

C:\Java>java EvenOrOdd 1234
That is an EVEN number

C:\Java>java EvenOrOdd 12345678901
Exception in thread "main" java.lang.NumberFormatException: For input string: "1
2345678901"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at EvenOrOdd.main(EvenOrOdd.java:4)

C:\Java>
 
Kirill Varivoda
Greenhorn
Posts: 20
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Morgan wrote:I am working through the cattle drive and currently working on the EvenOrOdd assignment. I can have the program determine whether a number is odd or even right up until I break the 11 character limit. Is there a limit as to how long the arguments from a command line can be? (I would post the code but that would allow others who are going through to have a short cut and all I wanted to know is if there is a limit to the characters).

Here is what I get from the command line.

C:\Java>java EvenOrOdd 1235
That is an ODD number

C:\Java>java EvenOrOdd 1234
That is an EVEN number

C:\Java>java EvenOrOdd 12345678901
Exception in thread "main" java.lang.NumberFormatException: For input string: "1
2345678901"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at EvenOrOdd.main(EvenOrOdd.java:4)

C:\Java>


As far as I understand try to you use this:



 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The changes suggested will not get rid of the Exception. The reason for the Exception is that you have an 11‑digit number which won't fit into an int. You can read about parseInt here (this version of the link is more informative), and the limits to the int type in the Java Language Specification.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I challenge you to write the print statement without using the keywords if and else. It is quite simple, if you remember all the operators.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!