【问题标题】:JAVA: Trouble understanding how to work with input filesJAVA:无法理解如何处理输入文件
【发布时间】:2019-12-05 23:25:52
【问题描述】:

我是 JAVA 和这个论坛的新手,我正试图找出一个项目的问题,该项目对文件中给出的三个边的三角形进行分类。该文件可以是多条不同的行(多个三角形),行必须以'#'开头才能被程序认为有效,并且在列出三个边之后可以有不同类型的多余数据。作为参考,这里有一个示例输入文件和程序运行时应生成的相应输出文件。 input/output reference。 我对我的大部分程序都非常有信心,但我遇到的具体问题是在我的 main 方法中处理输入文件。我不明白 a) 如何让程序只识别以“#”开头的行作为有效输入。 b)如何在“#”之后到达下一个扫描器标记,并且只有当它们是整数时才将这三个分配给边 1-3(它们不一定必须是)。 c)如何将边之后的剩余数据(如果有)分配给字符串变量。 如果您知道有任何有用的扫描仪方法可以帮助我,我将不胜感激。阅读 JAVA API 对我来说仍然是希腊语。最重要的是,如果有一种方法可以返回当前令牌的数据类型,我会不断收到 InputMismatchException,因为我试图将非整数分配给我。 最后,这是我一直困扰我的主要方法。再次感谢您提供的任何见解。

public static void main (String[] args) throws FileNotFoundException, IOException
    {
        String input, output;
        int side1 = 0, side2 = 0, side3 = 0, lineCounter = 1;
        String irrelevant;
        File inputFile;
        Scanner inputRead = null;

        System.out.println("Please choose the .txt file you would like to use for triangle side input.");
        input = fileSelect();

        System.out.println("Please choose the .txt file you would like to use as the program output file.");
        output = fileSelect();

        FileWrite outputFile = new FileWrite();

        outputFile.fileCreate(output);

        inputFile = new File(input);

        try
        {
            inputRead = new Scanner(inputFile);
        }
        catch (FileNotFoundException e)
        {
            System.out.println("No file or unsupported file found: Please choose another input file.");
            input = fileSelect();
        }

        while (inputRead.hasNextLine())
        {
            String line = inputRead.nextLine();
            while (line != null)
            { 
                if(inputRead.hasNext("#"))
                {
                    side1 = inputRead.nextInt();
                    side2 = inputRead.nextInt();
                    side3 = inputRead.nextInt();
                    irrelevant = inputRead.next();

                    areaCalc(side1, side2, side3, lineCounter, output);
                }
                lineCounter ++;
            }
        }
        if (!inputRead.hasNext())
        {

        }

        inputRead.close();
    }  

【问题讨论】:

    标签: java bluej


    【解决方案1】:

    像下面这样进行行解析怎么样

    try {
                 String content[] = line.split(" ");
                if(content.length > 3) {
                     if(content[0].equals("#")) {
                       int side1 = Integer.valueOf(content[1]);
                       int side2 = Integer.valueOf(content[2]);
                       int side3 = Integer.valueOf(content[3]);
                       if(side1 > 0 && side2 > 0 && side3 > 0) {
                          areaCalc(side1, side2, side3, lineCounter, output);
                      }
                   }
                }
           }catch(NumberFormatException e){}
    

    【讨论】:

    • 感谢 kaya3,现已更新。无法快速将我的想法从 typescript 切换到 java。
    • 有趣,所以 .split 是一种按组件分解字符串的 String 方法?然后我可以为该行的其余部分做类似 String无关 = content[4,((line.length()) - 1)] 的事情吗?
    • String irrelevant = ""; if(content.length >= 4 ) for (int i = 4; i < content.length; i++) { irrelevant = irrelevant + content[i]; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多