好吧,所以我已經輸入了一些記錄到一個文本文件,我可以寫入和從這個文件中讀取,但我現在試圖通過搜索這個文本文件並遇到了問題。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,Hexham,NE46 1DB,3,146000,Semi Detached
10,Downing Street,London,sw19,9,1000000,Terraced
你可以給門牌號碼文件的結構? – phtrivier 2011-05-25 11:29:50
輸入文件是怎樣的? (將其粘貼並將其格式化爲問題中的代碼。) – aioobe 2011-05-25 11:29:51
難道是user.next()和housenumber.nextLine()之一有尾隨空格/行分隔符而不是其他嗎?也許一個忽略空白的比較會有所幫助... – phtrivier 2011-05-25 11:32:30