【发布时间】: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