0

I am trying to differentiate and store data (Point: x, y) from a text file in two arraylists: knots and zeros. Data File just contains integers or decimal values.

Data File:
x    y  type(zero/knot) 
46  10      2
13  5       2
27  21      1

But my code is throwing NumberFormatException:48

import java.util.*;
import java.util.ArrayList;
import java.io.*;
import java.awt.Point;

public class program {

    public static void main(String []args)  {
        ArrayList knots = new ArrayList<Point>();
        ArrayList zeros = new ArrayList<Point>();
        List<Integer> list = new ArrayList<Integer>();
        String line = null;
        String file = "data1.txt";
        BufferedReader reader;
        try  {
            reader = new BufferedReader(new FileReader(file));
            while((line = reader.readLine()) != null)  {
                String tmp[] = line.split(" ");
                System.out.println(line);
                for (String s:tmp) {
                    s = s.replace("\r\n","");
                    int i = Integer.parseInt(s.trim());
                    //knots.add(new Point();
                System.out.println(s);
              }
            }
        }

        catch (FileNotFoundException e)  {
            e.printStackTrace();
        } catch (IOException e)  {
            e.printStackTrace();
        }
            }
        }
4
  • Could it be due to your file header line? Commented Sep 3, 2013 at 4:00
  • so the line "x y type(zero/knot) " isn't actually in the file? Commented Sep 3, 2013 at 4:00
  • You are trying to foramt a string Commented Sep 3, 2013 at 4:00
  • Yeah,"x y type" is not in the file Commented Sep 3, 2013 at 4:03

4 Answers 4

1

Assuming the file contents is EXACTLY...

46  10      2
13  5       2
27  21      1

When I run you code, I get the output...

46  10      2
46
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:504)
    at java.lang.Integer.parseInt(Integer.java:527)
    at testparser.TestParser.main(TestParser.java:47)

This would seem to indicate that there are blank/empty Strings in the output...

If I replace this section...

for (String s : tmp) {
    s = s.replace("\r\n", "");
    int i = Integer.parseInt(s.trim());
    //knots.add(new Point();
    System.out.println(s);
}

With...

for (String s : tmp) {
    s = s.trim();
    if (s.length() > 0) {
        int i = Integer.parseInt(s.trim());
        //knots.add(new Point();
        System.out.println(s);
    }
}

I get...

46  10      2
46
10
2
13  5       2
13
5
2
27  21      1
27
21
1

Don't forget, readline will not return the line terminator, so you should be able to ignore it.

line.split(" ") is spliting on each "individual" space, meaning that if you have more than one space character together, then it will return the second space...;)

If, instead, you use String tmp[] = line.split("\\s+");, it will split on any spaces between all the other characters...not returning any "blank" Strings (making the if (s.length() > 0) { check redundant)

Sign up to request clarification or add additional context in comments.

1 Comment

@user2398101 Glad I was able to help
1

The problem is in your split line. It should be like this String a[]=s.split(" +"); What happened in your case is : When it split "46 10 2" string with " ", it counts only one space for spliting the String. So it ignores one space after 46 and 10 and then looks for the String. But there is no string after one space so it takes "" as a string and try to parse it in integer. Though "" can't be parsed into integer,it throws NumberFormatException

Comments

0

You said you are parsing decimal numbers as well. Those cannot be parsed with Integer.parseInt(). You need to use Double.parseDouble() or Float.parseFloat() to parse decimal values from a String.

Also, it looks like you are trying to parse your header in your file. Make sure to do a token readLine() call before you begin the reading of the data, otherwise you'll try to parse text and that makes no sense and will crash.

3 Comments

Right now, I am just trying to do integer values, I have two separate data files.
Then the advice in my second paragraph likely applies, but it will be important to know about parsing floating point values for when you read the other file.
"x y type" is just for illustration, it is not there in the actual file.
0

You can try this code.. It works fine for me

import java.util.ArrayList;
import java.io.*;
import java.awt.Point;

public class program 
{
    public static void main(String []args)  
    {
        ArrayList knots = new ArrayList<Point>();
        ArrayList zeros = new ArrayList<Point>();
        List<Integer> list = new ArrayList<Integer>();
        String line = null;
        String file = "data1.txt";
        BufferedReader reader;
        try
        {
            reader = new BufferedReader(new FileReader(file));
            while((line = reader.readLine()) != null) 
            {
                String tmp[] = line.split(" ");
                System.out.println(line);
                for (String s:tmp) 
                {   
                    if(! (s.isEmpty()) )
                    {
                        int i = Integer.parseInt(s.trim());
                        System.out.println(s);
                    }
                }
            }
        }
        catch (FileNotFoundException e)  
        {
            e.printStackTrace();
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
}

I hope this will help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.