Được rồi ... Tôi có tập tin này .txt (UTF-8)Không thể phân tích như integer
4661,SOMETHING,3858884120607,24,24.09
4659,SOMETHING1,3858884120621,24,15.95
4660,SOMETHING2,3858884120614,24,19.58
Và mã này
FileInputStream fis = new FileInputStream(new File(someTextFile.txt));
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader in = new BufferedReader(isr);
int i = 0;
String line;
while((line = in.readLine()) != null) {
Pattern p = Pattern.compile(",");
String[] article = p.split(line);
// I don't know why but when a first line starts with
// an integer - article[0] (which in .txt file is 4661)
// becomes someWeirdCharacter4661 so I need to trim it
// *weird character is like |=>|
if (i == 0) {
StringBuffer articleCode = new StringBuffer(article[0]);
articleCode.deleteCharAt(0);
article[0] = articleCode.toString();
}
SomeArticle**.addOrChange(mContext, Integer.parseInt(article[0]), article[1], article[2], Integer.parseInt(article[3]), Double.parseDouble(article[4]));
i++;
}
On giả lập nó là tốt nhưng trên thiết bị thực (HTC Desire) Tôi gặp lỗi này (lạ):
E/AndroidRuntime(16422): java.lang.NumberFormatException: unable to parse '4661' as integer
Có vấn đề gì?
** nó chỉ là một số lớp học của tôi mà cần những thông số như là đầu vào (bối cảnh, int, string, string, int, double)
Có, nó hoạt động! :) Nhưng, tại sao nó chỉ hiển thị chữ số '4661'? Đâu là phần "có vấn đề" khác của chuỗi? – svenkapudija
Không có ý tưởng nào nếu không thấy tệp của bạn. Có thể tệp gốc của bạn có mã hóa lẻ hoặc bị hỏng. – monkjack