-1

So I'm trying to take a series of "scores" from a text file to put into an array and then sort in order, rows of four, and write other methods to get the highest, lowest, average, etc. The println commands are in there but I haven't written the methods yet. I've been working all day and I'm starting to confuse myself, and now I'm getting a NullPointerException error in the main method. Any help?

package arrayops1d;

import java.io.*;
import java.util.*;

public class ArrayOps1D {

    static int scores[];
    public static void main(String[] args) throws Exception{
        FileReader file = new FileReader("C:/Users/Steve/Documents/"
                + "NetBeansProjects/ArrayOps1D/Scores.txt");
        BufferedReader reader = new BufferedReader(file);

        String scores = "";
        String line = reader.readLine();
        while (line != null){
            scores += line;
            line = reader.readLine();
        }


        System.out.println(scores);
        System.out.println(getTotal());
        System.out.println(getAverage());
        System.out.println(getHighest());
        System.out.println(getLowest());
        System.out.println(getMedian());
        System.out.println(getPosition());
        System.out.println(getDeviations);
        System.out.println(getStdDev);


}

2 Answers2

0

First Issue with your code: In your file path, Instead using / , you must use \\ or better File.separator if your program wants to be ran in different platform.

If you do not , you will have java.io.FileNotFoundException

You are reading line by line, so you can use split Function and use Integer.paraseInt or Float.parseFloat to convert each splited elements and added to your array

  1. How to use Split in Java
  2. How to convert String to int
Community
  • 1
  • 1
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
0

Here is one way you can read the int values from a file into an array of Integer using a Scanner and a List -

Integer[] scores = null;
File file = new File("C:/Users/Steve/Documents/"
        + "NetBeansProjects/ArrayOps1D/Scores.txt");
if (file.exists() && file.canRead()) {
    try {
        List<Integer> al = new ArrayList<>();
        Scanner scanner = new Scanner(file);
        while (scanner.hasNext()) {
            if (scanner.hasNextInt()) {
                al.add(scanner.nextInt());
            } else {
                System.out.println("Not an int: " + scanner.next());
            }
        }
        scores = al.toArray(new Integer[al.size()]);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
} else {
    System.out.println("Can't find file: " + file.getPath());
}
if (scores != null) {
    System.out.println("Scores Read: " + Arrays.toString(scores));
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • how do you expect when the OP shows this code to his or her teacher, the teacher can accept it? – Kick Buttowski Aug 31 '14 at 23:31
  • It is not right to give the answer away. Just hint to lead the OP to the right palce and I am sure you agree with me – Kick Buttowski Aug 31 '14 at 23:33
  • @KickButtowski Read OP's question again. There's a lot to this task besides reading the `int`(s) into an array. And OP actually posted code that appears to at least attempt it. It was either this, or show how to parse all of the `int`(s) from `String`(s) and frankly I think this `Scanner` example is pretty minimal. – Elliott Frisch Aug 31 '14 at 23:34
  • I always admire you but I am sure you could put it as a hint. cuz the teacher gonna go nuts how novice student that could not write a function can go like this. If you read the question, the op has difficulty to write simple high score function. your code is too advance for the op and not helping him – Kick Buttowski Aug 31 '14 at 23:36