Hi, I am really struggling with a bufferedReader method i am trying to implement. I have to read text from a file like this :

Poland
Franciszek,Smuda,63,2009
1,GK,Wojciech,Szczesny,22,11,0
2,DF,Sebastian,Boenisch,25,9,0
3,DF,Grzegorz,Wojtkowiak,28,19,0
4,DF,Marcin,Kaminski,20,3,0
5,MF,Dariusz,Dudka,28,65,2
6,MF,Adam,Matuszczyk,23,20,1

and store it in an array of objects (mine being an array of Player objects which stores information on the players for the polish football team).

Now I have created the method in a different class called the group class. The specification asks to create a readFromFile() method which will read the above text and output it.

The error I receive is java.lang.NumberFormatException: For input string: "Poland"

I believe it is because I am maybe parsing my types wrong but i an confused and unsure.

My method looks like this:

// Buffered Reader Method
    public void readFromFile(String f){
        Player[] players = null;

        try {
            BufferedReader br = new BufferedReader(new FileReader(new File("data.txt")));
            int size = Integer.parseInt(br.readLine());
            players = new Player[size];
            String newLine = br.readLine();
            int count = 0;
            while(newLine != null){
                StringTokenizer st =  new StringTokenizer(newLine, ",,,,,,");
                String desc = st.nextToken();
                int squadNumber = Integer.parseInt(st.nextToken());
                String position = (st.nextToken());
                String firstName = (st.nextToken());
                String surname = (st.nextToken());
                int noOfCaps = Integer.parseInt(st.nextToken());
                int goalsScored = Integer.parseInt(st.nextToken());
                boolean isCaptain = Boolean.parseBoolean(st.nextToken());
                int age = Integer.parseInt(st.nextToken());
                players[count] = new Player(squadNumber, position, firstName, surname, noOfCaps, goalsScored, isCaptain, age);

                count ++;
                newLine = br.readLine();


            }
            br.close();
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }

        for (int i = 0; i < players.length; i++){
            System.out.println(players[i].toString());

        }

    }

Any help would be greatly appreciated.

java.lang.NumberFormatException: For input string: "Poland"

You should post the full text of the error message which gives the name of the method that threw the error and the location in the program where the error happened.

I guess that the program had a String containing: Poland that it tried to convert to a numeric value.
As you can see "Poland" is not a number.

Check that the order of the data in the file is as you expect it to be.

The errors that appear :

at hutchisonbarry.Group.readFromFile(Group.java:93)
at hutchisonbarry.HutchisonBarry.main(HutchisonBarry.java:92)

then at the bottom:

Testing Group Class
java.lang.NumberFormatException: For input string: "Poland"
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

You need to post the FULL text of the error message. You have left off some of the first lines.

Did you look at the file and compare it to how you are processing its contents?
If the first thing in the file is "Poland", then the code on line 7 is wrong because it requires a number.

My full output is:

run:
Testing Person Class
Testing Player Class
Is He Captain?
false
First Name = Tam Surname = Nathan Squad Number = 15, Position = LM Number of Caps = 10, Goals Scored = 9 Is Captain? false

Testing Country Class
Person added at index 0 of array
Person added at index 1 of array
Person added at index 2 of array
Exception in thread "main" java.lang.NullPointerException
Person added at index 3 of array
Person added at index 0 of array
Person added at index 1 of array
First Name = Bob Surname = Macdonald Squad Number = 7, Position = RM Number of Caps = 50, Goals Scored = 20 Is Captain? true
at hutchisonbarry.Group.readFromFile(Group.java:93)

at hutchisonbarry.HutchisonBarry.main(HutchisonBarry.java:92)
First Name = Tam Surname = Nathan Squad Number = 15, Position = LM Number of Caps = 10, Goals Scored = 9 Is Captain? false

First Name = Gregor Surname = Clyne Squad Number = 10, Position = ST Number of Caps = 20, Goals Scored = 15 Is Captain? false

Testing toString
Country Name Poland First Name = Bob Surname = Macdonald Squad Number = 7, Position = RM Number of Caps = 50, Goals Scored = 20 Is Captain? true
Country Name Poland First Name = Tam Surname = Nathan Squad Number = 15, Position = LM Number of Caps = 10, Goals Scored = 9 Is Captain? false
Country Name Poland Manager First Name = Harry Surname = EnfieldAge = 59 Start Year = 1989
Country Name Poland First Name = Arnold Surname = Pike Squad Number = 15, Position = GK Number of Caps = 30, Goals Scored = 0 Is Captain? false

Testing Group Class
java.lang.NumberFormatException: For input string: "Poland"
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Everything bolded are red in my output box if that helps.

Exception in thread "main" java.lang.NullPointerException
Person added at index 3 of array
Person added at index 0 of array
Person added at index 1 of array
First Name = Bob Surname = Macdonald Squad Number = 7, Position = RM Number of Caps = 50, Goals Scored = 20 Is Captain? true
at hutchisonbarry.Group.readFromFile(Group.java:93)
at hutchisonbarry.HutchisonBarry.main(HutchisonBarry.java:92)

It looks like you also have a problem with a null value at line 93. What variable has a null value at line 93?

It looks like you also have a problem with a null value at line 93. What variable has a null value at line 93?

line 93 is the for loop which looks like this:

 for (int i = 0; i < players.length; i++){
            System.out.println(players[i].toString());

        }

which is lines 34/35 above.

What variable has a null value?

The catch block at line 30 hides the full text of the error message. You should always call the printStackTrace() method in a catch block to get the full text of the error message.

I dont understand, the only variable I can see which has a null value is my variable for the array of objects Players. Which is on line 3.

The java program says there is a null value somewhere. Put the call to printStackTrace() in the catch block and execute it again to get the full text of the error message.

It looks like Poland is the first line of your file, which is a different content from all the other lines. If so you need first to read that line (eg into a String called teamName), then use your existing loop to read the remaining lines.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.