• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

runtime error?

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all, when i try to run my program I get the following run time error.

Please enter 6 integers followed by a space.
12 3 4 5 6 7
Exception in thread "main" java.lang.NumberFormatException:
at java.lang.Integer.parseInt(Integer.java:435)
at java.lang.Integer.parseInt(Integer.java:463)
at change.main(change.java:53)
Press any key to continue . . .

First of all what does that mean.

here what i got

public class change {

public static void main(String[] args) {

System.out.println("Please enter 6 integers followed by a space.");

String input;
input=Stdin.readln(); //read input from keyboard (console).


int blank = input.indexOf(" ");
String pen = input.substring(0, blank); // Reads first number.

//manipulating strings




String first= input.substring(blank+1, input.length()); // Reads rest of the string.
int blank2= first.indexOf(" ");
String nick= first.substring(0, blank2);

String second= input.substring(blank2+1, input.length());
int blank3= second.indexOf(" ");
String dime= second.substring(0, blank3);

String third = input.substring(blank3+1, input.length());
int blank4= third.indexOf(" ");
String quart= third.substring(0, blank4);

String fourth= input.substring(blank4+1, input.length());
int blank5=fourth.indexOf(" ");
String loon = fourth.substring(0, blank5);

String toon= input.substring(blank5+1);

// convert strings to integers.

int pen1=Integer.parseInt(pen);
int nick1=Integer.parseInt(nick);
int dime1=Integer.parseInt(dime);
int quart1=Integer.parseInt(quart);
int loon1=Integer.parseInt(loon);
int toon1=Integer.parseInt(toon);

//Calculate total amount in cents.

int cents=(pen1*1) + (nick1*5) + (dime1*10) + (quart1*25) + (loon1*100) + (toon1*200);

//Calculate total amount in dollars.

int dollars = (cents/100);


System.out.println(dollars + cents); // Prints out the total amount of money.








}
}
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Integer class is very picky. It will throw a number format exception if it encounters anything (including a space) that is not a number.

You should print the strings to see what it is converting... however, I can guess... take a peek at some of your code.



You are taking substrings from the beginning, for all of the fields.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I drew to conclusions too fast... Basically, you are calculating the blanks from one string (third), but then you use a different string to pull the next string (input).

In any case, I recommend that you print the output, to see what is being parsed.

Henry

 
Mike Smith
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, thanks for the input. But, I am confused as to am I pulling just the string that contains the numbers I want or am I pulling the whole string each time....As for example, I find the first characters, then I pull the rest of that string omiting what I just found. And then I exact the rest of it. Then extract the second word of characters. Is it just the work that I am converting.. I don't know if I made this clear enough, I sure hope so. WEll thanks again.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To see what your program is doing, add println() statements everywhere.
To know if there are non trimmed blanks add ">" and "<" before and after the string you want to see.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Smith:
String first= input.substring(blank+1, input.length()); // Reads rest of the string.



Just as an asidewill read the rest of the string.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic