Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Edit Adapted code since then. Would still like to eliminate repeated bits of code... Cannot think how.

public static void readFile(String fileName) throws IOException, FileNotFoundException{

    double fileX;
    double fileY;
    double fileZ;

    // Reference the file using the the BufferedReader object
    BufferedReader input = new BufferedReader(new FileReader(fileName));    

    //scanner to scan through file 
    Scanner token = new Scanner(input);

    /*
     * skipToDouble() method makes it possible to search my file for double values
     */

    // initialize x, y and z to place inside vector A 
    fileX= skipToDouble(token);
    fileY= skipToDouble(token);
    fileZ= skipToDouble(token);

    vecA = new Vector3D(fileX,fileY,fileZ);

    // initialize x, y and z to place inside vector B 
    fileX= skipToDouble(token);
    fileY= skipToDouble(token);
    fileZ= skipToDouble(token);

    vecB = new Vector3D(fileX, fileY, fileZ);

    // initialize x, y and z to place inside vector C (dummy vector really)
    fileX= skipToDouble(token);
    fileY= skipToDouble(token);
    fileZ= skipToDouble(token);

    //initialize vecC using double values from third line of file
    vecC = new Vector3D(fileX, fileY, fileZ);   

    //close file 
    input.close();
}
//keeps looking until double is found
private static double skipToDouble(Scanner scanner) {
    while (scanner.hasNext() && !scanner.hasNextDouble()) {
        scanner.next();
    }
    return scanner.hasNextDouble() ? scanner.nextDouble() : Double.NaN;
}

EDIT: Trying the suggestion I am not getting a NullInputException and cannot see why:

// Reference the file using the the BufferedReader object
    BufferedReader input = new BufferedReader(new FileReader(fileName));    

    //scanner to scan through file 
    Scanner tokens = new Scanner(input);

    for(int i=0; i<3; i++){

        System.out.print("i = " + i);

        for(int j=0; j<3; j++){
            System.out.print("j = " + j);

            file[j]=skipToDouble(tokens);
        }
        vec[i]= new Vector3D(file[0], file[1], file[2]);
    }


    //close file 
    input.close();
share|improve this question
two methods readNextDouble and readNextVector using these two methods. – Sulthan Oct 8 '12 at 8:44

3 Answers

up vote 1 down vote accepted

You don't need to track the double in fileX, fileY, and fileZ. Instead of this:

fileX= skipToDouble(token);
fileY= skipToDouble(token);
fileZ= skipToDouble(token);

vecA = new Vector3D(fileX,fileY,fileZ);

You could consider this:

vecA = new Vector3D(
        skipToDouble(token),
        skipToDouble(token),
        skipToDouble(token)
);
share|improve this answer
Yep that worked nicely. Thanks. – Magpie Oct 6 '12 at 21:26

You could factor out the repetitive code into a separate method, and then reduce the repetition further there.

Maybe like this:

private static Vector3D readNextVector3D(Scanner token)
{
    while ( !token.hasNextDouble() )
    {
        token.next();
    }

    String[] nextLine = token.nextLine().split(" ");
    assert(nextLine.length == 3);
    return new Vector3D( Double.parseDouble(nextLine[0]),
                         Double.parseDouble(nextLine[1]),
                         Double.parseDouble(nextLine[2]) );
}
share|improve this answer
thanks for the answer. I had done something a bit similar to that which I have reposted. I still would like to cut the x y z repeated bits though – Magpie Oct 6 '12 at 2:47

Instead of using fileX, fileY, fileZ you could use double[3] to represent them. Same with VecA, VecB, VecC. Then you could write something like:

for(i=0; i<3; i++)
{
   for(j=0;j<3;j++)
   {
      file[j]=skipToDouble(token);
   }
   vec[i]=new Vector3D(file[0], file[1], file[2]);
}
share|improve this answer
ok I will try this. Thanks. – Magpie Oct 6 '12 at 13:24
I am getting a null input exception // Reference the file using the the BufferedReader object BufferedReader input = new BufferedReader(new FileReader(fileName)); //scanner to scan through file Scanner tokens = new Scanner(input); for(int i=0; i<3; i++){ System.out.print("i = " + i); for(int j=0; j<3; j++){ System.out.print("j = " + j); file[j]=skipToDouble(tokens); } vec[i]= new Vector3D(file[0], file[1], file[2]); } //close file input.close(); – Magpie Oct 6 '12 at 20:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.