我目前正在研究一个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文件,使用用户输入的大写名称,以及如何使其返回整行。到目前为止,似乎有很多搜索引导我回车,我至少知道它们是如何工作的,而不是如何使用它。如果有人有时间解释我可以做的事情来解决这个问题,我将不胜感激!
答案 0 :(得分:0)
该方法称为nextLine()
:
File f = new File("text.txt");
Scanner s = new Scanner(f);
String line = s.nextLine();
答案 1 :(得分:0)
您可以使用BufferedReader逐行读取文本文件,并随意执行任何操作。
看看这里:http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/