1

I'm reading from a pretty simple file that displays items and how much they cost. It looks like this:

Shoes 10.00
Jersey 20.00
Cookies 15.00
Light Bulbs 2.00
Paper 5.00

I want to map every item to how much it costs and my current code works just fine. However, it looks a bit clunky and initializes variables with null that my project's submission server doesn't like and treats as a bug. I'm looking for a way to translate this into something a lot more elegant and thus learn to read files another way apart from relying on the Scanner class. Maybe using BufferedReader or PrintReader or something of the ilk that I have never really grasped. Help appreciated.

    private TreeMap<String, Double> prices = new TreeMap<String, Double>();

     public void readFromFile(String fileName){
                File file = new File(fileName);
                Scanner sc = null; //Server treats this as a bug. 
                try {
                    sc = new Scanner(file);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                while (sc.hasNextLine()) {
                    Scanner sc2 = new Scanner(sc.nextLine());
                    while (sc2.hasNext()) {
                        String s = sc2.next(); //Gets the item name
                        prices.put(s, Double.parseDouble(sc2.next())); //The next word is the price
                        }
                    sc2.close();
                    }
}
  • Instead of doing try/catch, you can use `throws` for the exceptions, which will let you assign `sc` directly to the `new Scanner(file)` – pushkin Nov 29 '15 at 02:46
  • Yes could always do that but the null thing was simply a sneaky way to discover how to do it some other way too. I have always used scanner so looking for something different. Thanks for your input. – move_slow_break_things Nov 29 '15 at 02:48
  • If you're interested in `BufferedReader`, just look at examples online - there are [plenty](http://stackoverflow.com/questions/16265693/how-to-use-buffered-reader-in-java). – pushkin Nov 29 '15 at 02:49
  • I looked around. From what I tried, there's only ways to read a complete line or character by character. I couldn't figure out how to do it word by word on every line which is what I needed here. Thanks again. – move_slow_break_things Nov 29 '15 at 02:53

2 Answers2

2

Here is an example. Exceptions are thrown instead handling them inside the method. BufferedReader and a StringTokenizer is used to get needed text from the file.

private TreeMap<String, Double> prices = new TreeMap<String, Double>();

public void readFromFile(String fileName) throws FileNotFoundException, IOException {
    BufferedReader br = new BufferedReader(new FileReader(fileName));
    StringTokenizer st;
    String line;
    while ((line = br.readLine()) != null) {
        st = new StringTokenizer(line.trim(), " ");
        prices.put(st.nextToken(), Double.parseDouble(st.nextToken()));
    }
    br.close();
}

If fileName is null a NullPointerException will automatically be thrown. If you want to handle that by your method, you can add the following code at the top of your method..

    if (fileName == null) {
        throw new IllegalArgumentException("Invalid String input for fileName. fileName: " + fileName);
    }
Ramesh-X
  • 4,853
  • 6
  • 46
  • 67
  • Is there no way to do this without throwing the exceptions (as handling them elsewhere is more of a pain) and without any null assignments to begin with? I'm simply curious. – move_slow_break_things Nov 29 '15 at 03:09
  • You can handle exceptions within the method using `try...catch`. But it is not a good way. – Ramesh-X Nov 29 '15 at 16:31
0

Instead of using try,catch statements, you can use throws for exception handling.

public void readFromFile(String fileName) throws FileNotFoundException {
    File file = new File(fileName);
    Scanner sc = new Scanner(file);
    ...
}

This should solve the problem of your server treating the null assignment as a bug.

pushkin
  • 9,575
  • 15
  • 51
  • 95