-5

I am getting this error, but I am not really sure what it means and how can I get rid of the error. This program adds and deletes the records, as specified in input file after reading from it. Then, it writes the output to another file.

Error :

java.lang.NumberFormatException: For input string: "1   Acconci"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at ReadArtistList.getList(ReadArtistList.java:23)
at UpdateArtistList.<init>(UpdateArtistList.java:12)
at p3a.main(p3a.java:9)

p3a.java:

   `import java.io.*;
    //import java.lang.*;
    public class p3a
  {
    public static void main(String args[])
   {
   try
   {
      UpdateArtistList up=new UpdateArtistList();
      PrintWriter writer = new PrintWriter("C:/Users/patel/Desktop/JavaFiles/p3artists.txt");
   ArtistList a=up.getUpdatedList();
   ArtistNode temp=a.getListNode();
       while(temp!=null)
       {
           writer.println(temp.toString());
           temp=temp.getNext();
       }
   writer.close();
   }
   catch(Exception e)
   {
       e.printStackTrace();
   }
      }
   }`

UpdateArtistList.java:

import java.io.*;
//import java.lang.*;

public class UpdateArtistList
{
 private ArtistList t;
 public UpdateArtistList()
  {
   try
    {
       t = new ReadArtistList().getList();
        BufferedReader reader = new BufferedReader(new InputStreamReader(new            FileInputStream("C:/Users/patel/Desktop/JavaFiles/p2changes.txt")));
        String line = reader.readLine();
        while (line != null)
        {
            //splitting file by comma
            String[] array = line.split(" ");
            //checking the string of whether it is for adding new item or deleting an item
            if(array[0].equals("A") || array[0].equals("a"))
            {
               t.add(array[1]);
            }
            else if(array[0].equals("D") || array[0].equals("d"))
            {
                //iterating through array for finding an item to delete
                 t.delete(Integer.parseInt(array[1]));
            }
            line = reader.readLine();
        }
        reader.close();

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    }
           public ArtistList getUpdatedList()
             {
               return t;
            }
           }

ReadArtistList.java:

  import java.io.*;
 import java.lang.*;
 public class ReadArtistList
 {
   /*   public ReadArtistList()
    {

   }
     */   public ArtistList getList()
    {
         ArtistList a=new ArtistList();
      try
        {
        //reading from file "items.txt"

        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("C:/Users/patel/Desktop/JavaFiles/p1artists.txt")));
        String line = reader.readLine();
        while (line != null)
        {
            //splitting file by comma
            String[] array = line.split(" ");
            a.addNode(Integer.parseInt(array[0]),array[1]);
            line = reader.readLine();
        }
        reader.close();
        //return a;
    }
    catch(Exception e)
    {
       e.printStackTrace();
    }
    return a;
        }

           }
  • 2
    What is NumberFormatException and how can i fix it? ---> http://stackoverflow.com/questions/39849984/what-is-a-numberformatexception-and-how-can-i-fix-it – Ousmane D. Mar 16 '17 at 01:19
  • 1
    The stacktrace tells you the line at which the error happens, the message tells both the invalid input and what error happened. The rest can be solved with a few minutes of googling and debugging. Hint: keywords for the search would be NumberFormatException and Integer.parseInt –  Mar 16 '17 at 01:19
  • 5
    Possible duplicate of [What is a NumberFormatException and how can I fix it?](http://stackoverflow.com/questions/39849984/what-is-a-numberformatexception-and-how-can-i-fix-it) –  Mar 16 '17 at 01:20
  • It's printing a number format exception at line 9 for p3a..Which is where you instantiate the update article list class. I suggest debugging the stack trace to see which class is misbehaving. Also, there seems to be an error during parsing the array unread article list. Wrap it in a try catch block or print the array after splitting to see what you get. – Araphel Mar 16 '17 at 01:22

1 Answers1

0

NumberFormatException at line 9 of p3a is where you're creating the object. The error also says that the Integer.parseInt is acting up. Do the following:
1. Debug the stack trace.
2. In your third class, where you are splitting the array, wrap it in a try catch block including the parseInt in it, or print the array after splitting. You'll understand what's wrong. Also, are you really splitting by comma? Your remark and code are off on that line.

Araphel
  • 309
  • 1
  • 10