-1

This is the part of a code I made. The reader seems to read the first string "2" but it is not converting it to an integer for some reason.

public void fileinput2() 
{
try 
{
        BufferedReader file=new BufferedReader(new FileReader("ddv.txt"));
        try 
{
            while((line=file.readLine())!=null){
                String[] s=line.split("\\t+");
                int firstindex=Integer.parseInt(s[0].trim());
                int secondindex=Integer.parseInt(s[1].trim());
                                    adj[firstindex-1][secondindex-1]=1;
                adj[secondindex-1][firstindex-1]=1;
                                    /*for(int i=0;i<s.length;i++)
                                    {System.out.println(s[i]);
                                    int x=Integer.valueOf(s[i].trim());
                                    }*/

                }
        } catch (NumberFormatException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

The DDV is a text file that goes like this:

2    8
6    9
4    10

and so on.

However I get this error

java.lang.NumberFormatException: For input string: "2"
at       
    java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615).

Please Help me out Thanks.:)

Kevin Kloet
  • 1,086
  • 1
  • 11
  • 21

1 Answers1

0

It not real "2", you have unprintable symbol \uFEFF before "2". It is invisible utf-8 prefix in your file

You can analyze first char of file or just add lazy s.replace("\uFEFF", "") or parse lines with regex

rustot
  • 331
  • 1
  • 11