我目前正在开发一个java程序,该程序要求您在多个txt文件上使用多个扫描程序来返回名称的定义,以及一些与名称流行程度相关的数字。目前,我很难将这些数据返回到原始程序。.txt文件如何定义的示例如下:
ADAMO m Italian Italian form of ADAM
ADAN mf (no meaning found)
ADANNA f Igbo Means "father's daughter" in Igbo.
ADANNAYA f Igbo Means "her father's daughter" in Igbo.
ADAOIN f Irish Modern form of TAN
到目前为止,我的程序是这样的:
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();
if (meaningCheck.hasNext(""+ upperUserName))
meaning = meaningCheck.next("" + upperUserName);
System.out.println("" + meaning);
System.out.println("" + userName +"! That's one the rarest cards in all of duel monsters!");
}
}
正如你可能注意到的,它还远远没有完成,但我似乎不知道如何使用用户输入的大写名称搜索.txt文件,以及如何让它返回整行。到目前为止,这里的大量搜索似乎让我找到了回车符,我至少了解它们是如何工作的,但不了解如何使用。如果有人有时间解释我能做些什么来解决这个问题,我将不胜感激!
该方法称为nextLine()
:
File f = new File("text.txt");
Scanner s = new Scanner(f);
String line = s.nextLine();
您可以使用BufferedReader逐行读取文本文件,并对其执行任何操作。
看看这里:http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/