【问题标题】:How to determine the end of a line with a Scanner?如何使用扫描仪确定行尾?
【发布时间】:2012-03-01 11:04:57
【问题描述】:

我的程序中有一个扫描仪,可以读取部分文件并将其格式化为 HTML。当我阅读我的文件时,我需要知道如何让扫描仪知道它位于行尾并开始写入下一行。

这是我的代码的相关部分,如果我遗漏了任何内容,请告诉我:

//scanner object to read the input file
Scanner sc = new Scanner(file);

//filewriter object for writing to the output file
FileWriter fWrite = new FileWriter(outFile);

//Reads in the input file 1 word at a time and decides how to
////add it to the output file
while (sc.hasNext() == true)
{
    String tempString = sc.next();
    if (colorMap.containsKey(tempString) == true)
    {
        String word = tempString;
        String color = colorMap.get(word);
        String codeOut = colorize(word, color);
        fWrite.write(codeOut + " ");
    }
    else
    {
        fWrite.write(tempString + " ");
    }
}

//closes the files
reader.close();
fWrite.close();
sc.close();

我发现了sc.nextLine(),但我仍然不知道如何确定我何时处于行尾。

【问题讨论】:

  • 当你结束时,sc.hasNext() 将是假的,不是吗?
  • 澄清一下,你确定你不想知道如何让FileWriter 在另一行开始写作吗?从您的帖子中可以采用这种方式,因为 Scanner 具有您需要的功能。

标签: java java.util.scanner delimiter


【解决方案1】:

哇,我已经使用 java 10 年了,从来没有听说过扫描仪! 默认情况下,它似乎使用空格分隔符,因此您无法判断何时出现行尾。

看起来您可以更改扫描仪的分隔符 - 请参阅 Scanner Class 的示例:

 String input = "1 fish 2 fish red fish blue fish";
 Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
 System.out.println(s.nextInt());
 System.out.println(s.nextInt());
 System.out.println(s.next());
 System.out.println(s.next());
 s.close();

【讨论】:

    【解决方案2】:

    如果您只想使用 Scanner,您需要创建一个临时字符串将其实例化到数据网格的 nextLine()(因此它只返回它跳过的行)和一个扫描临时字符串的新 Scanner 对象。这样,您只使用该行并且 hasNext() 不会返回误报(这并不是真正的误报,因为这就是它的本意,但在您的情况下,从技术上讲是这样)。您只需保持 nextLine() 处理第一个扫描仪并更改临时字符串和第二个扫描仪以扫描每个新行等。

    【讨论】:

    • “你需要创建一个临时字符串将其实例化到 nextLine()”是什么意思?
    【解决方案3】:

    您可以使用 hasNextLine 方法逐行而不是逐字迭代文件,然后用空格分隔行并对单词进行操作

    这里是使用 hasNextLine 和 split 的相同代码

    //scanner object to read the input file
    Scanner sc = new Scanner(file);
    
    //filewriter object for writing to the output file
    FileWriter fWrite = new FileWriter(outFile);
    
    //get the line separator for the current platform
    String newLine = System.getProperty("line.separator");
    
    //Reads in the input file 1 word at a time and decides how to
    ////add it to the output file
    while (sc.hasNextLine())
    {
        // split the line by whitespaces [ \t\n\x0B\f\r]
        String[] words = sc.nextLine().split("\\s");
        for(String word : words)
        {
            if (colorMap.containsKey(word))
            {
                String color = colorMap.get(word);
                String codeOut = colorize(word, color);
                fWrite.write(codeOut + " ");
            }
            else
            {
                fWrite.write(word + " ");
            }
        }
        fWrite.write(newLine);
    }
    
    //closes the files
    reader.close();
    fWrite.close();
    sc.close();
    

    【讨论】:

      【解决方案4】:

      行通常由\n\r 分隔,因此如果您需要检查它,您可以尝试这样做,但我不确定您为什么要这样做,因为您已经在使用@987654323 @ 读取整行。

      如果您担心 hasNext() 不适用于您的特定情况,可以使用 Scanner.hasNextLine()(不知道为什么它不起作用)。

      【讨论】:

        猜你喜欢
        • 2015-11-23
        • 2013-08-06
        • 2014-11-22
        • 1970-01-01
        • 2014-05-20
        • 2016-12-01
        • 1970-01-01
        • 2015-10-02
        • 2017-05-30
        相关资源
        最近更新 更多