如何解决Txtfile搜索不起作用
| 好的,所以我已经向文本文件输入了许多记录,并且可以写入和读取该文件,但是现在我试图搜索该文本文件,但是遇到了问题。package assignmentnew;
// Import io so we can use file objects
import java.io.*;
import java.util.Scanner;
public class SearchProp {
public void Search() throws FileNotFoundException {
try {
String details,input,id,line;
int count;
Scanner user = new Scanner(System.in);
System.out.println();
System.out.println();
System.out.println(\"Please enter your housenumber: \");
input = user.next();
Scanner housenumber = new Scanner(new File(\"writeto.txt\"));
while (housenumber.hasNext())
{
id = housenumber.next();
line = housenumber.nextLine();
if (input.equals(id))
{
System.out.println(\"House number is: \" + id + \"and\" + line);
break;
}
if(!housenumber.hasNext()) {
System.out.println(\"no house with this number\");
}
}
}
catch(IOException e)
{
System.out.print(\"File failure\");
}
}
}
无论我输入什么值,都会告诉我文件中没有门牌号,但显然是,有什么主意吗?
附录:
文本文件中的文件结构。
27,Abbey View,Hexham,NE46 1EQ,4,150000,Terraced
34,Peth Head,NE46 1DB,3,146000,Semi Detached
10,Downing Street,London,sw19,9,1000000,Terraced
解决方法
扫描仪的默认定界符为空格,而不是“ 2”。
,您必须使用
housenumber.useDelimiter(\",\");
,代码才能正常工作。
编辑:
暂时将其设置。
这就是我27岁时得到的。
Please enter your housenumber:
27
House number is: 27 and,Abbey View,Hexham,NE46 1EQ,4,150000,Terraced
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。