Results 1 to 7 of 7
  1. #1
    birdmcfarland is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default 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

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    birdmcfarland is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default 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.

  4. #4
    birdmcfarland is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: parseInt and NumberFormatException in music exercise

    this is what I'm thinking this does but clearly I'm wrong:

    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
    Here's the txt file:


    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

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default 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
    Java Code:
    System.out.println("["+lineOfMusicData+"]");
    Then print the first element of the array immediately after you split the line
    Java Code:
    System.out.println("["+splitLine[0]+"]");
    I rather suspect your file ends with a blank line.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    birdmcfarland is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default 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.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: parseInt and NumberFormatException in music exercise

    You're welcome, and use the approach of printing and examining anything that's suspect whenever you have a runtime problem. That's debugging 101.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. error w/ parseInt
    By katiebear128 in forum New To Java
    Replies: 2
    Last Post: 11-02-2011, 02:26 AM
  2. Problem with parseInt
    By fr0s1yjack in forum New To Java
    Replies: 4
    Last Post: 06-25-2011, 05:09 PM
  3. parseInt and getText
    By esallender in forum New To Java
    Replies: 2
    Last Post: 01-14-2011, 03:16 PM
  4. parseInt
    By trefoil in forum New To Java
    Replies: 4
    Last Post: 09-09-2009, 07:12 PM
  5. Integer.parseInt?
    By Exhonour in forum New To Java
    Replies: 4
    Last Post: 01-20-2009, 02:31 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •