Hunter Landers
Hunter Landers

Reputation: 13

Anyone understand external files well?

I am supposed to create a program that reads an external file with 3 integers on every line and find the area of a triangle with those three numbers. We haven't learned arrays yet though, so i want to create the program without an array, methods and classes are fine. I just need help reading the file every three numbers by line.

The data is:

7 8 9

9 9 12

6 5 21

24 7 25

13 12 5

50 40 30

10 10 10

82 34 48

4 5 6

Here's what i have so far:

import java.io.*;
import java.util.*;
import java.lang.*;
public class Prog610a
{
    public static void main(String[] args) throws IOException
    { 
        BufferedReader reader = new BufferedReader(new FileReader("myData.in"));
        String currentLine;

        int a, b, c; 
        double s, area;

        System.out.println("A" + "\t B" + "\t C" + "\t Area");
        try
        {
            while((currentLine = reader.readLine()) != null)
            {
                Scanner scanner = new Scanner(currentLine);

                s = ((scanner.nextInt() + scanner.nextInt() + scanner.nextInt()) / 2);
                area = Math.sqrt(s * (s - scanner.nextInt()) * (s - scanner.nextInt()) * (s - scanner.nextInt()) );

                if(s < 0)
                {
                    System.out.println(scanner.nextInt() + " \t" + scanner.nextInt() + 
                    " \t" + scanner.nextInt() + "\t This is not a triangle");
                }
                else
                {
                    System.out.println(scanner.nextInt() + " \t" + scanner.nextInt() + 
                    " \t" + scanner.nextInt() + " \t" + area);
                }
            }
        } 
        finally 
        {
            reader.close();
        }
    }
}

Upvotes: 0

Views: 117

Answers (1)

Matthew Franglen
Matthew Franglen

Reputation: 4532

You have made a good start by using the Scanner. I would suggest that just using that may be insufficient, as you may end up with some malformed lines. To handle them you may wish to split the processing into two parts: get a line, and then get the individual values from that line.

That allows you to catch lines that do not have enough values, or have too many values. If you do not do this then you may become mis-aligned with the lines, reading some values from one line, and some from the following line.

The BufferedReader will allow you to read lines which you can then scan. Since you don't want to use arrays you must extract the numbers individually:

BufferedReader reader = new BufferedReader(new FileReader("myData.in"));
String currentLine;

try {
    while ((currentLine = reader.readLine()) != null) {
        Scanner scanner = new Scanner(currentLine);

        try {
            calculateTriangleArea(
                scanner.nextInt(), scanner.nextInt(), scanner.nextInt()
            );
        }
        catch (NoSuchElementException e) {
            // invalid line
        }
    }
}
finally {
    reader.close();
}

Also it may help you to understand the Java string interpolation. You have horizontalTab throughout your code. You can express that in a string by just using \t. For example:

"\tThis is indented by one tab"
"This is not"

You can find the complete list of string escape characters here.

The exception handling (or lack of) in my code may surprise you. In your code you catch the Exception that could be thrown. However you discard it and then proceed to execute the rest of the code on a Scanner that is known to be broken. In such a situation it is better to fail immediately rather than conceal the error and attempt to proceed.

The one bit of exception handling that does occur in my code is the finally block. This ensures that the reader is closed no matter what happens when reading from it. It wraps the code that is executed after the reader has been opened, and as such it is known that the reader is not null and should be closed after use.

Upvotes: 1

Related Questions