-2
我有文本文件,該文件是包含:
「我有3個蘋果和香蕉1
所有的人有4輛車,至少。」從文本文件和工作獲取整數數組列表在Java我想讀取這個.txt文件並從中獲取整數。我將在arrayList中讀取整數變量。
public class dedede {
public static void al() throws Exception {
File f = new File("C:\\users\\sony\\Desktop\\456.txt");
try {
FileReader islem = new FileReader(f);
char data[] = new char[(int) f.length()];
islem.read(data);
String metin = new String(data);
ArrayList<Integer> yapi = new<Integer> ArrayList();
StringTokenizer s = new StringTokenizer(metin);
String dizi[] = new String[s.countTokens()];
int i = 0;
while (s.hasMoreTokens()) {
dizi[i] = s.nextToken();
i++;
}
for (int c = 0; c < dizi.length; c++) {
if (Integer.parseInt(dizi[c]) > 0) {
yapi.add(Integer.parseInt(dizi[c]));
}
}
System.out.println(yapi);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
al();
}
}
什麼問題/問題,你遇到? – 2013-05-14 17:27:41
我想編程讀取文件識別整數並將它們存儲在一個數組中。 – 2013-05-14 17:34:56
除了你的代碼之外,你應該給出一些關於你在運行時遇到的問題或者你對結果有任何疑問的指示。 – 2013-05-14 17:36:17