【问题标题】:Scanning a text file and returning results扫描文本文件并返回结果
【发布时间】:2013-12-02 01:26:57
【问题描述】:

所以,我在扫描文本文件时仍然遇到很大的问题。

我目前正在做“婴儿名字”项目,这是一个非常常见的 Ap 计算机科学项目,并且正在努力让它返回几行文本。该程序执行以下操作,它要求用户输入名称,然后将其转换为全部大写。它应该做的是,它应该在文本文档中搜索名称的精确副本,然后打印该行。 文本文件布局示例:

MIREIO f Provencal Original Provenal form of MIREILLE
MIREK m Czech, Polish Short form of MIROSLAV and other beginning with the Slavic element   
mir "peace".
MIRELA f Romanian Romanian form of MIREILLE
MIRELE f Yiddish Yiddish form of MIRIAM
MIRELLA f Italian Italian form of MIREILLE
MIREMBE f African Means "peace" in Luganda.

因此,如果下面的程序从扫描仪“Mirek”获取输入,我试图让它做的是将其转换为 MIREK(Done),然后将其余文本返回到与scanner.nextLine 一致。然而,我得到的每一个结果都说找不到名字。如果有人能帮我解决这个逻辑错误,我将不胜感激。

原始代码:

import java.io.*; 
import java.util.Scanner;//For reading the file.

public class BabyNamesProject {

//This program will use user input to scan several text documents for information regarding names.
//It will give the popularity of the name in the last few decades, it's meaning, and a Drawing Panel chart with it's popularity.
public static void main(String[] args) 
    throws FileNotFoundException {

    File f = new File("Names.txt");
    File g = new File("Meanings.txt");
    File h = new File("Names2.txt");
    Scanner nameCheck = new Scanner(f);
    Scanner meaningCheck = new Scanner(g);
    Scanner popularityCheck = new Scanner(h);
    Scanner Ask = new Scanner(System.in);
    String userName = "";
    String upperUserName = "";
    String meaning = "";

    System.out.println("Hello! This application will allow you to check how popular certain names are compared to others.");
    System.out.println(" ");
    System.out.println("Please type in your name!");
    userName = Ask.next();
    upperUserName = userName.toUpperCase();
    System.out.println(upperUserName);

    if (meaningCheck.hasNext(upperUserName))
        System.out.println(meaningCheck.nextLine());
else
    System.out.println("Name was not found...");

【问题讨论】:

    标签: java text input java.util.scanner


    【解决方案1】:

    您应该使用循环。你的if 语句只检查一次

    Scanner input = Scanner(System.in);
    String name = input.nextLine();
    String upperUserName = name.toUpperCase();
    
    Scanner namesScanner = new Scanner(new File("names.txt"));
    
    while (namesScanner.hasNextline()){
        String line = namesScanner.nextLine();
    
        if (line.contains(upperUserName)){
            System.out.println(line);
        }
    }
    System.out.println("Name was not found...");
    

    【讨论】:

    • 这不会总是吐出一个未找到的名称,不管它是否找到?
    【解决方案2】:

    您只在此处检查文件的第一行:

    if (meaningCheck.hasNext(upperUserName))
        System.out.println(meaningCheck.nextLine());
    else
       System.out.println("Name was not found...");
    

    相反,您应该这样做:

    boolean inputExists = false; //boolean flag determining whether your input exists.
    while ( meaningCheck.hasNext() ) //while there are more lines
    {
      String currentLine = meaningCheck.nextLine();
      if (currentLine.contains(upperUserName))
      {
        System.out.println(currentLine);
        inputExists = true; //The message "Name not found" should not appear because something is found
      }
    }
    
    if ( !(inputExists) ) //If it does NOT exist
    {
      System.out.println("Name was not found");
    }
    

    【讨论】:

    • 就个人而言,这是我会使用的解决方案,除了一件事。一些定义中还包含其他名称,例如,如果您在列表中搜索“DANIEL”,您会找到其他名称,例如“DEINIOL m Welsh Welsh form of DANIEL”。有没有办法让它只打印带有确切文本的确切行?
    • 所以,如果我错了,请纠正我,您要检查的唯一输入是每行开头的名称。所以你要检查: Mirek, Mirela, Mirele 但不是 Mireille, Miriam ##unless## 下面有一行实际上以 Mirreille, Miriam 开头。如果是这种情况,您应该将currentLine.contains(upperUserName) 替换为currentLine.startsWith(upperUserName)。还是我误解了你的评论?
    【解决方案3】:

    您应该检查javadoc for hasNext(pattern)。它的工作不是搜索文件。它应该做的就是检查模式是否与下一个标记匹配。在这种情况下,您所做的只是检查文件中的名字是否为upperUserName,而通常情况并非如此。

    如果您确实想搜索文件中的所有行,那么您需要自己创建一个循环并考虑文件的每一行。

    【讨论】:

      猜你喜欢
      • 2020-07-01
      • 2016-08-15
      • 1970-01-01
      • 2014-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多