1

I'm trying to create a method that adds to a variable for each character inside a file. If the file was:

abcd
abc
ab

then after the function ran the variable it would return would be equal to 9.

Here's the code I have so far:

public static double getRow(String filename) {
   double size = 0;
   File f;
   Scanner infile;
   try{
      f = new File(filename);
      infile = new Scanner(f);
   }
   catch (IOException e){
       System.out.println("Error opening the file");
       //System.exit(0); not good
   }
   while(infile.hasNext()) {
       size++;
   }
   infile.close();
   return size;

}

But I keep getting that infile has not been initialized. I'm not sure how to get the solution I want.

user207421
  • 305,947
  • 44
  • 307
  • 483

2 Answers2

1

Because you are initializing infile in the try block, if anything goes wrong in the try, infile will never be initialized when you attempt to use it after the catch block.

What you want to do is having all you processing in the try block, included looping through and closing infile:

public static double getRow(String filename) {
    double size = 0;
    File f;
    Scanner infile;
    try {
        f = new File(filename);
        infile = new Scanner(f);
        while(infile.hasNext()) {
            size++;
        }
        infile.close();
    }
    catch (IOException e) {
        System.out.println("Error opening the file");
        //System.exit(0); not good
    }
    return size;
}
jbihan
  • 3,053
  • 2
  • 24
  • 34
  • Do you know of a way to add up all the characters in the file? – Benji Frank Feb 01 '15 at 01:40
  • As mentioned by EJP, have a look at `BufferedBeader`, or just search for `java read file character by character`, you will find plenty of examples. Like this one: http://stackoverflow.com/questions/811851/how-do-i-read-input-character-by-character-in-java, but there are many others. – jbihan Feb 01 '15 at 01:44
  • Reading character by character won't work. It will count the line terminators, whatever they are. – user207421 Feb 01 '15 at 01:47
0

I just tried this; the error is "variable infile might not have been initialized", which is because if there's an exception in the line new File, it wouldn't have been.

There are several solutions, but the best would be to make sure you don't try to use infile if you're not guaranteed it's been initialized, for example by putting the code inside the try block as above.

Here's my version:

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

class Foo {
    public static double getRow(String filename) {
        double size = 0;
        File f;
        Scanner infile ;
        try{
            f = new File(filename);
            infile = new Scanner(f);
            while(infile.hasNext()) {
                size++;
            }
            infile.close();
            return size;

        }
        catch (IOException e){
            System.out.println("Error opening the file");
            //System.exit(0); not good
        }
        return -1;
    }               

    public static void main(String[] argv){
        System.out.printf("ResultsL %d\n",
                          getRow("foo.txt"));

        return ;
    }
}

Ideally, you'd also make this a public static int with int size=0;, because you're counting something, which is a discrete value, not a real.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263