【问题标题】:Why my Java program works perfectly in windows but it's a disaster in linux?为什么我的 Java 程序在 Windows 中运行良好,但在 linux 中却是一场灾难?
【发布时间】:2020-03-26 10:37:44
【问题描述】:

我编写了一个程序,它读取一个文本文件,删除请求的字符串并在没有字符串的情况下重写它。该程序从终端获取三个参数:1) 输入文件 2) 字符串 3) 输出文件。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

class wordfilter {
    public static void main(String[] args) {
        Scanner scanner = new Scanner("");
        Scanner conteggio = new Scanner("");
        int numel = 0;
        File file = new File(args[0]); // Argomento 0: il file
        try {
            conteggio = new Scanner(file);
        }
        catch (FileNotFoundException e) {
            System.out.println("File non trovato");
        }

        while (conteggio.hasNext()) {
            numel++;
            conteggio.next();
        }
        conteggio.close();
        String[] lettura = new String[numel];
        int i = 0;
        try {
            scanner = new Scanner(file);
        }
        catch (FileNotFoundException e) {
            System.out.println("File non trovato");
        }
        while (scanner.hasNextLine()) {
            lettura[i] = scanner.next();
            i++;
        }
        System.out.println("Contarighe -> " + numel);
        for (i = 0; i < lettura.length; i++) {
            System.out.println("Elemento " + i + " - > " + lettura[i]);
        }
        scanner.close();
        String escludi = args[1]; // Argomento 1: il filtro
        String[] filtrato = rimuovi(escludi, lettura);
        if (args.length == 3) stampaSuFile(filtrato, args[2]);
    }
    public static String[] rimuovi(String esclusione, String[] input) {
        String[] nuovoV;
        String escludi = esclusione;
        int dim = 0;
        for (int i = 0; i < input.length; i++) {
            if (!input[i].equals(escludi))
                dim++;
        }
        nuovoV = new String[dim];

        int j = 0;
        for (int i = 0; i < input.length; i++) {
            if (!input[i].equals(escludi)) {
                nuovoV[j] = input[i];
                j++;
            }
            ;
        }
        return nuovoV;
    }

    public static void stampaSuFile(String[] out, String path) {
        String closingstring = "";
        File destinazione = new File(path);
        try {
            destinazione.createNewFile();
        } catch (IOException e) {
            System.out.println("Errore creazione file");
        }
        try {   
            FileWriter writer = new FileWriter(destinazione);
            for (int i = 0; i < out.length; i++)
                writer.write(out[i] + (i == (out.length-1) ? closingstring : " "));
            writer.close();
            System.out.println("Scrittura eseguita correttamente");
        } catch (IOException e) {
            System.out.println("Errore scrittura file");
        }
    }
}

在 Windows 上没问题,它可以完美运行。

当我写像java wordfilter in.txt word out.txt这样的东西时,在 Linux 上

我明白了

Exception in thread "main" java.util.NoSuchElementException
    at java.base/java.util.Scanner.throwFor(Scanner.java:937)
    at java.base/java.util.Scanner.next(Scanner.java:1478)
    at wordfilter.main(wordfilter.java:42)

有什么问题?是因为linux上的一些差异?

【问题讨论】:

  • 如果您一致地缩进您的代码,人们会更容易阅读它,并且您将更有可能获得帮助。
  • @khelwood 你是对的,我希望现在好多了。对不起。

标签: java linux windows java.util.scanner nosuchelementexception


【解决方案1】:

您正在混合使用基于行和标记的函数:hasNextLine() 和 next()。如果输入以换行结束(Linux 上的典型),hasNextLine 在文件末尾返回 true,但没有下一个“项目”。

    while (scanner.hasNextLine()) {
        lettura[i] = scanner.next();
        i++;
    }

您应该将 hasNext 与 next 一起使用,或者将 hasNextLine 与 nextLine 一起使用,将它们混合使用会令人困惑。

    while (scanner.hasNext()) {
        lettura[i] = scanner.next();
        i++;
    }

【讨论】:

    【解决方案2】:

    输入文件在 Linux 上以换行符结尾。因此,还有另一行,但它是空的。如果您从输入中删除最后一个换行符,程序将开始正常工作。

    或者,导入异常

    import java.util.NoSuchElementException;
    

    在代码中忽略它

        while (scanner.hasNextLine()) {
            System.out.println("" + i);
            try {
                lettura[i] = scanner.next();
            } catch (NoSuchElementException e) {}
            i++;
        }
    

    【讨论】:

    • 我很困惑,即使我输入了一个以换行符结尾的文件,程序也会忽略它
    • 非常感谢,真的!它工作得很好,请帮我解决这个疑问,是什么导致了异常?第一台扫描仪是否在文件中无休止地搜索?异常指的是哪个元素?
    • 如果最后一行以换行符结尾,扫描器会为hasNextLine() 返回true,但在next() 上,它不能返回任何下一项,因为换行符后面没有任何内容。
    猜你喜欢
    • 1970-01-01
    • 2016-07-30
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多