我有一个文件,我正在导入,我想要做的是要求用户的输入,并将其用作查找要检查的正确行的基础。我有它设置如下:java根据第一个字找到文件中的特定行
public class ReadLines {
public static void main(String[] args) throws FileNotFoundException
{
File fileNames = new File("file.txt");
Scanner scnr = new Scanner(fileNames);
Scanner in = new Scanner(System.in);
int count = 0;
int lineNumber = 1;
System.out.print("Please enter a name to look up: ");
String newName = in.next();
while(scnr.hasNextLine()){
if(scnr.equals(newName))
{
String line = scnr.nextLine();
System.out.print(line);
}
}
}
现在,我只是想获得它打印出来看,我已经抓获了,但不工作。有没有人有任何想法?另外,如果它很重要,我不能使用try和catch或数组。 非常感谢!
您可以使用['String.startsWith()'](http://docs.oracle.com/javase/7/docs/api /java/lang/String.html#startsWith(java.lang.String)) – alfasin
我想你想使用'sncr.findInLine' API,而不是'equals' –
非常感谢您的帮助!这两个都很棒! – Vaak