扫描文本文件并返回结果

时间:2013-12-02 01:26:57

标签: java text input java.util.scanner

所以,我在扫描文本文件方面仍有很多问题。

我目前正在做婴儿的名字'项目,一个非常常见的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(完成)然后返回scan.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...");

3 个答案:

答案 0 :(得分:0)

你应该使用一个循环。您的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...");

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

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");
}