Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

program does not run, simple java prgram  RSS feed

 
Biran Quach
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm trying to add all the numbers and find average of string of numbers separated by a space, this is what i have so far. The error message is
Exception in thread "main" java.lang.NumberFormatException: For input string: "1 2 3 4 5 6 8"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at addtext.main(addtext.java:11)

 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your String.split() is not working, your expressions is not correct. All you need is a space " " and it will work. You need to learn to debug, until you can, you are really less than half a programmer. You can insert println statements in appropriate place or learn to use your debugger, but until you do, you are always going to be very crippled, at best, in programming.

Biran Quach wrote:i'm trying to add all the numbers and find average of string of numbers separated by a space, this is what i have so far. The error message is
Exception in thread "main" java.lang.NumberFormatException: For input string: "1 2 3 4 5 6 8"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at addtext.main(addtext.java:11)

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Name of a class should start with capital letter. Please refer this Java Programming Style Make separate method for finding average of those numbers. You can name method like getNumbersAvg() etc. Don't write that code in main method. You need only one loop, no need of second.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The String “1 2 3 4 5 6” does not constitute a number. It is six numbers, but the method only want to read one number.
Try a Scanner. I bet you didn't know you can pass a String to a Scanner, but you can:-Now try “1.0 2.0 3.0 4.0 5.0 6.0” instead and see what happens.

[edit:]Remove wayward [ character from line 13 of the code.
 
salvin francis
Bartender
Posts: 1588
35
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to the suggestions given above, I would like to add one more thing: http://www.coderanch.com/t/660020/Wiki/Main-Pain
 
Piet Souris
Rancher
Posts: 1979
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, we could just have said to OP that he/she needs to use back slashes in his/her regex, in stead of forward slashes. in a friendly way.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!