![](/img/trans.png)
[英]Reading input file into array, sorting it, then outputting it to a file
[英]Reading file input data and loading it into an Array
我正在開發一個程序,該程序從文件中讀取數據並將其輸入到數組中。 似乎應該可以,但是我從程序中得到了一個奇怪的輸出(沒有錯誤,它運行了,只是給了我一個奇怪的結果)。
這是我的代碼:
Scanner s = new Scanner(new File("../Computer/src/computers/computer.txt"));
String[] comps = new String[2];
int i = 0;
while (s.hasNextLine()) {
comps[i] = s.nextLine();
i++;
}
s.close();
System.out.println(comps);
我得到的輸出是:
[Ljava.lang.String;@3d62b333
BUILD SUCCESSFUL (total time: 1 second)
另外,如果我的文本文件有問題,我的文本文件看起來也像這樣:
12344555 Dell Intel 499.99
23623626 Asus AMD 299.99
您不能打印這樣的數組。 您得到的輸出是Object
的默認toString()
,它輸出哈希碼。
您必須遍歷並打印每個String
for (String s : comps)
{
System.out.println(s);
}
您正在正確讀取文件,但是以錯誤的方式打印陣列。
在下面使用以打印陣列。
for(int j =0;j<comps.length;j++)
{
System.out.println(comps[j]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.