I/O-TxtFile Line Distance Program I am trying to write a lab/program that loops through a .txt input file. Reads an entire line of data as a string, then splits that string into an array delimited by a space. If there are 4 items in the resulting array, and each item is a valid double, I want it to determine the distance between the points &amp; return info to a separate output.txt file.

RLabs.java

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

public class RLabs {

    public static void main(String[] args) throws FileNotFoundException {
        double dealtaX, deltaY, distance,x1, y1, x2, y2;
        String Line, item1, item2, item3, item4;
        String[] Sect;
        Scanner inFile;
        PrintWriter outFile;

        System.out.println("I/O File Line Distance Program Starting. Opening file...");
        System.out.println("Please ensure data is entered per line in the format of (X1 Y1 X2 Y2).");
        inFile = new Scanner(new File("input.txt"));
        outFile = new PrintWriter(new File("countresults.txt"));


        while(inFile.hasNext()){

            Line = inFile.nextLine();
            Sect = Line.split(" ");
            if(Sect.length == 4) {
                item1 = Sect[0];
                item2 = Sect[1];
                item3 = Sect[2];
                item4 = Sect[3];

                if(testDoubleLow(item1,0)&amp;&amp;(testDoubleLow(item2,0)&amp;&amp;(testDoubleLow(item3,0)&amp;&amp;(testDoubleLow(item4,0))))){
                    x1 = Double.parseDouble(item1);
                    y1 = Double.parseDouble(item2);
                    x2 = Double.parseDouble(item3);
                    y2 = Double.parseDouble(item4);
                    dealtaX = (x2-x1);
                    deltaY = (y2-y1);

                    distance = Math.sqrt(Math.pow(deltaY,2)+ Math.pow(dealtaX,2));
                    outFile.printf(Line);

                    outFile.printf(" The distance between (%1.1f,%1.1f) and (%1.1f,%1.1f) is %1.1f\n", x1,y1,x2,y2,distance);

                }
                else {
                    outFile.println("Line did not correct data");
                }






            }
            else {
                outFile.println("Line did not have 4 item(s)");
            }
        }

        outFile.close();
        inFile.close();


        System.out.println("Done...");
    }

        private static boolean testDoubleLow(String token, double l){

            if(testDouble(token)) {
                double num = Double.parseDouble(token);
                if(num &gt; l ) {
                    return true;
                }
            }

            return false;
        }

        private static boolean testDouble(String s) {
            try {
                Double.parseDouble(s);
                return true;
            }
            catch(Exception e) {
                return false;


    }

    }
}


Input data I'm using

17.2 23.2 15.6 17.76

1

frog

123 frog 2 98

22 99 19 76

-13 03 92 03

l 123 03 96 

1293.333333333333333 3 4 111111111111111111

2 1 1 2

21.4518345 94 21 mom

12 67 tti 30

128 30 -2 69 

670 20 49 100

1230 230 304 500 60 30 60 606 59

Output from program

17.2 23.2 15.6 17.76 The distance between (17.2,23.2) and (15.6,17.8) is 5.7

Line did not have 4 item(s)

Line did not have 4 item(s)

Line did not have correct data

22 99 19 76 The distance between (22.0,99.0) and (19.0,76.0) is 23.2

Line did not have correct data

Line did not have correct data

1293.333333333333333 3 4 111111111111111111 The distance between (1293.3,3.0) and (4.0,111111111111111104.0) is 111111111111111104.0

2 1 1 2 The distance between (2.0,1.0) and (1.0,2.0) is 1.4

Line did not have correct data

Line did not have correct data

Line did not have correct data

670 20 49 100 The distance between (670.0,20.0) and (49.0,100.0) is 626.1

Line did not have 4 item(s)
Object-Orientated

your code is very procedural (honestly, it's just one long main methode) not very object-orientated... seperate your code into seperate concern(Segregation of concerns), at least a kind of DistanceParser class and a ErrorHandler class. Doing this make your ready open for the open-closed principal

DistanceParser fileParser = DistanceParser.fileInstance(inputFile);   //applying dependecy injection
fileParser.setErrorHandler(new FileErrorHandler(logFile));
fileParser.parse();


if you would apply that logic you were able to create easily any other parser (maybe DistanceParser.inputStreamInstance(inputStream) ), you could easily replace your FileErrorHandler with any other ErrorHandler and you were able to test your functionality with a proper unit test

@Test
DistanceParser fileParser = DistanceParser.fileInstance(invalidInput);
ErrorHandler errorHandlerMock = mock(ErrorHandler.class);
fileParser.setErrorHandler(errorHandlerMock );
fileParser.parse(); //errors during execution are handled by the errorHandler (handleError())
verify(errorHandlerMock , atLeastOnce()).handleError();


You know you could implement interfaces on your Parser class and your Errorhandler class, so you can apply Liskovs Substitution Principal ...

Tell, don't ask

another minor issue is naming of your methods, i'm still wondering about your testDoubleLow() methode. if you would use proper names your code would be far more readable.

misleading naming here: 


testDouble(String s) should be named into isDouble(String doubleAsString)
testDoubleLow(String token, double l) should be named into isBiggerThan(String doubleAsString, double minimalValue)