Results 1 to 7 of 7
- 04-27-2012, 03:02 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
parseInt and NumberFormatException in music exercise
Hi. I am in a beginning programming class and we are learning Java. Out latest lab assignment is to read a txt file of songs and output the info in various formats using arrays.
One of the required methods is Integer.parseInt. When I use that method I keep getting a NumberFormatException. I read a number of websites about this but I still can't figure out what I'm doing wrong. I thought my code was correct, but I guess not.
Any ideas? thanks!
Java Code:import java.io.*; import java.util.*; public class MuTunes { /** * @param args */ public static void main(String[] args) throws FileNotFoundException { Scanner musicDataInput = new Scanner(new File("Music.txt")); processMusic(musicDataInput); } public static void processMusic(Scanner musicDataInput) { int totalNumberOfSongs = musicDataInput.nextInt(); musicDataInput.nextLine(); String[] songTitle = new String[totalNumberOfSongs]; String[] artist = new String[totalNumberOfSongs]; int[] time = new int[totalNumberOfSongs]; int songNumber = 0; System.out.printf("%-24s %-24s %4s", "TITLE", "ARTIST", "TIME\n"); while (musicDataInput.hasNextLine()) { String lineOfMusicData = musicDataInput.nextLine(); String[] splitLine; splitLine = lineOfMusicData.split(":"); String timeAsString = splitLine[0]; int songTime = Integer.parseInt(timeAsString.trim()); time[songNumber] = songTime; artist[songNumber] = splitLine[1]; songTitle[songNumber] = splitLine[2]; songNumber++; } for (int i = 0; i < totalNumberOfSongs; i++) System.out.printf("%-24s%-24s%4d\n", songTitle[i], artist[i], time[i]); } }
and here's the message I'm getting:
Java Code:Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:470) at java.lang.Integer.parseInt(Integer.java:499) at MuTunes.processMusic(MuTunes.java:30) at MuTunes.main(MuTunes.java:11) TITLE ARTIST TIME
Last edited by birdmcfarland; 04-27-2012 at 03:10 PM. Reason: added error message
- 04-27-2012, 03:26 PM #2
Re: parseInt and NumberFormatException in music exercise
The error message tells you what's wrong. What's the numeric value of the empty String? It can't be computed, hence the error.
You have to ensure that you won't be attempting to pass parseInt(...) an empty String, either by controlling the contents of the file that is being read, or by testing for an empty String (read the methods of the String class) and assigning 0 as the value, if that's reasonable for the use case.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-27-2012, 03:34 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
Re: parseInt and NumberFormatException in music exercise
The numeric value of the string is the song time. I split one of the song lines (which contains song time, artist, and title) delimited by colons. The first value is the song time which is the first value (0) in that array.
String timeAsString = splitLine[0];
int songTime = Integer.parseInt(timeAsString.trim());
I don't understand why this is considered empty when I assigned it the first value in the array.
- 04-27-2012, 03:39 PM #4
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
Re: parseInt and NumberFormatException in music exercise
this is what I'm thinking this does but clearly I'm wrong:
Here's the txt file:Java Code:splitLine = lineOfMusicData.split(":"); //separate the line into its colon-delimited sections String timeAsString = splitLine[0]; // assign the first of those sections to timeAsString int songTime = Integer.parseInt(timeAsString.trim()); //turn this string of numbers into integers
10
400:Red Headed Stranger:Willie Nelson
303:40 Oz. To Freedom:Sublime
203:White Trash:SCOTS
315:Bigmouth Strikes Again:The Smiths
429:The Singing Bird:Sinéad O'Connor
321:Oro, Se Do :Sinéad O'Connor
307:A Fine Spring Morning:Blossom Dearie
302:La Dolce Vita:Nino Rota
236:Straight to Hell:Hank Williams III
615:Rooster:Alice in Chains
- 04-27-2012, 03:45 PM #5
Re: parseInt and NumberFormatException in music exercise
Print each line immediately after you read it, with delimiters so you can see where it starts and ends
Then print the first element of the array immediately after you split the lineJava Code:System.out.println("["+lineOfMusicData+"]");I rather suspect your file ends with a blank line.Java Code:System.out.println("["+splitLine[0]+"]");
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-27-2012, 03:50 PM #6
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
Re: parseInt and NumberFormatException in music exercise
hah! It did have lines at the end of the txt file! I went back to that several times to make sure that it was formatted correctly but never thought to look for blank lines at the end. Another lesson learned! It works now after deleting the lines.
Thanks so much for your help and for the tip about printing after the line to check, it's greatly appreciated.
- 04-27-2012, 03:56 PM #7
Similar Threads
-
error w/ parseInt
By katiebear128 in forum New To JavaReplies: 2Last Post: 11-02-2011, 02:26 AM -
Problem with parseInt
By fr0s1yjack in forum New To JavaReplies: 4Last Post: 06-25-2011, 05:09 PM -
parseInt and getText
By esallender in forum New To JavaReplies: 2Last Post: 01-14-2011, 03:16 PM -
parseInt
By trefoil in forum New To JavaReplies: 4Last Post: 09-09-2009, 07:12 PM -
Integer.parseInt?
By Exhonour in forum New To JavaReplies: 4Last Post: 01-20-2009, 02:31 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks