Suhvezdia
Suhvezdia

Reputation: 11

Integer.parseInt() issues

I'm having a bit of a problem trying to get my code to work. I am working on a project for my computer science class, and I have to get my program to read the file and perform some math. When I tried doing this, the code was not working. I then checked with a friend who wrote the exact same code, and it did not work.

The input .txt file that the program reads looks like this: 2/3,4/5 -1/6,2/4 1/1,1/1

The code I have written looks like this:

import javax.swing.JFileChooser;

import java.util.*;

public class ProjectTest
{

    public static void main(String[] args) throws Exception
    {           

        JFileChooser chooserRational = new JFileChooser();
        int returnValRational = chooserRational.showOpenDialog(null);
        if(returnValRational == JFileChooser.APPROVE_OPTION)
        {
            System.out.println("You chose to open this file: " + chooserRational.getSelectedFile().getName());

            Scanner input = new Scanner(chooserRational.getSelectedFile());

            while(input.hasNext() == true)
            {
                String line = input.nextLine();
                String[] output = line.split(",");
                String[] output1 = output[0].split("/");
                String[] output2 = output[1].split("/");

                String a = output1[0];
                String b = output1[1];
                String c = output2[0];
                String d = output2[1];

                int int1 = Integer.parseInt(a);
                int int2 = Integer.parseInt(b);
                int int3 = Integer.parseInt(c);
                int int4 = Integer.parseInt(d);

                System.out.println(int1 + " " + int2 + " " + int3 + " " + int4);


            }
            input.close();
        }
    }
}

When I output just the Strings a, b, c, and d, the code works perfectly fine and outputs the values perfectly. When the code sees Integer.parseInt(a), however, it gives me an error that looks like this:

Exception in thread "main" 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)
  at ProjectTest1.main(ProjectTest1.java:33)

Any help would be greatly appreciated.

Upvotes: 1

Views: 2703

Answers (2)

vz0
vz0

Reputation: 32923

Because your data file contains an UTF-8 BOM.

You have two alternatives: edit your source data file to remove the BOM, or you can add some code to deal with the BOM. For the first option use Notepad++ and remove the BOM. For the second alternative:

Scanner input = new Scanner(chooserRational.getSelectedFile());

if (input.nextByte() == 0xFE) {
  input.nextByte();
  input.nextByte();
} else {
  input = new Scanner(chooserRational.getSelectedFile());
}

Upvotes: 2

Dici
Dici

Reputation: 25950

You should replace

String line = input.nextLine();

with

String line = input.next();

since you have multiples groups of data on the same line.

Edit :

I ran your code and did not get the same exception as you. I had a NumberFormatException due to the nextLine call, I now fixed it and it runs with no error. I think like the others that you have an encoding problem. Search on the internet how to display invisible characters on your preferred text editor.

Upvotes: 1

Related Questions