I / O-TxtFile线距离程序| JAVA

时间:2018-12-02 21:48:17

标签: java arrays file-io boolean double

我正在尝试编写一个通过.txt输入文件循环的实验室/程序。以字符串形式读取整行数据,然后将该字符串拆分为以空格分隔的数组。如果结果数组中有4个项目,并且每个项目都是有效的double,则我希望它确定点之间的距离并将返回信息返回到单独的output.txt文件。

我遇到的问题是无法通过if语句if(testDoubleLow()){让x1,x2,y1,y2全部运行,如果我将其写为if(testDoubleLow(item1,0)){的话,那只会验证x1还是aka txt行上的第一个输入?

{我的密码}

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];
                outFile.printf(Line);
                if (testDoubleLow()) {
                    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(" 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 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 > l) {
                return true;
            }
        }
        return false;
    }

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

1 个答案:

答案 0 :(得分:0)

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)&&(testDoubleLow(item2,0)&&(testDoubleLow(item3,0)&&(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 > l ) {
                    return true;
                }
            }

            return false;
        }

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


    }

    }
}

我不知道这是否是一个正确的答案,或者只是一个临时的解决方法,但我希望这可能是正确的? if(testDoubleLow(item1,0)&&(testDoubleLow(item2,0)&&(testDoubleLow(item3,0)&&(testDoubleLow(item4,0))))){