写入文件时未知来源
unknownSource when writing into a file
每次对文件进行更改时,我都必须写入文件,即有人更改数量或类似的东西。我选择的方法是用项目填充 ArrayList,将它们创建到 类 中,然后将它们串回文本文件中并进行更改。
public void readStock() {
Scanner scanner = null;
try {
File input = new File("Stock.txt");
scanner = new Scanner(input);
Mouse mouse = null;
Keyboard keyboard = null;
while (scanner.hasNextLine()) {
String[] info = scanner.nextLine().split(",");
//System.out.println(Connectivity.valueOf(info[5].trim().toUpperCase()) instanceof Connectivity);
if (info[1].trim().equals("mouse")) {
mouse = new Mouse(Integer.parseInt(info[0].trim()),
info[3].trim(), info[4].trim(),
Connectivity.valueOf(info[5].trim().toUpperCase()),
Double.parseDouble(info[6].trim()),
Double.parseDouble(info[7].trim()),
Integer.parseInt(info[6].trim()),
Integer.parseInt(info[9].trim()),
MouseType.valueOf(info[2].trim().toUpperCase()));
productList.add(mouse);
} else {
keyboard = new Keyboard(Integer.parseInt(info[0].trim()),
info[3].trim(), info[4].trim(),
Connectivity.valueOf(info[5].trim().toUpperCase()),
Double.parseDouble(info[6].trim()),
Double.parseDouble(info[7].trim()),
Integer.parseInt(info[6].trim()),
KeyboardType.valueOf(info[2].trim().toUpperCase()),
KeyboardLayout.valueOf(info[9].trim().toUpperCase()));
productList.add(keyboard);
}
默认数据工作正常,但当我决定添加一个额外的项目时,它把一切搞砸了。
public void addStock() {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("Stock.txt"));
int max = productList.size();
for(int i = 0; i < max-2; i++) {
bw.write(productList.get(i).toString() + "\n");
}
bw.write(productList.get(max-1).toString());
} catch(IOException e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
finally {
try {
if(bw != null) {
bw.close();
}
} catch(IOException e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
}
以及它给我的错误:
java.lang.NumberFormatException: For input string: "15.0"
For input string: "15.0"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Reader.readStock(Reader.java:42)
at MainTest.main(MainTest.java:48)
以及文本文件的内容:
112233, mouse, GAMING, Logitech, black, WIRELESS, 15.0, 7.5, 15, 3
123456, keyboard, Corsair, black, WIRED, 2.0, 30.0, 2, GAMING, UK
124455, keyboard, Advent, white, WIRED, 10.0, 3.5, 10, STANDARD, UK
124566, mouse, STANDARD, Advent, grey, WIRED, 15.0, 2.5, 15, 2
125567, keyboard, Logitech, black, WIRELESS, 4.0, 25.99, 4, INTERNET, US
221101, mouse, STANDARD, Logitech, black, WIRED, 3.0, 3.0, 3, 3
221122, mouse, GAMING, Razer, black, WIRED, 1.0, 28.0, 1, 7
223044, mouse, GAMING, Anker, blue, WIRED, 3.0, 16.0, 3, 10
234555, keyboard, Apple, white, WIRELESS, 10.0, 75.5, 10, STANDARD, US
235066, keyboard, Microsoft, black, WIRELESS, 5.0, 45.5, 5, FLEXIBLE, UK
236677, mouse, STANDARD, Asus, blue, WIRELESS, 8.0, 10.0, 7, 5
237788, keyboard, Logitech, grey, WIRED, 15.0, 6.5, 15, STANDARD, UK
您正在将浮点数解析为整数。
将 parseInt 替换为 parseFloat,然后转换为 int
System.out.println( (int) Float.parseFloat("15.0".trim()) );
如果您需要一个浮点数形式的值,则不需要转换 :)
每次对文件进行更改时,我都必须写入文件,即有人更改数量或类似的东西。我选择的方法是用项目填充 ArrayList,将它们创建到 类 中,然后将它们串回文本文件中并进行更改。
public void readStock() {
Scanner scanner = null;
try {
File input = new File("Stock.txt");
scanner = new Scanner(input);
Mouse mouse = null;
Keyboard keyboard = null;
while (scanner.hasNextLine()) {
String[] info = scanner.nextLine().split(",");
//System.out.println(Connectivity.valueOf(info[5].trim().toUpperCase()) instanceof Connectivity);
if (info[1].trim().equals("mouse")) {
mouse = new Mouse(Integer.parseInt(info[0].trim()),
info[3].trim(), info[4].trim(),
Connectivity.valueOf(info[5].trim().toUpperCase()),
Double.parseDouble(info[6].trim()),
Double.parseDouble(info[7].trim()),
Integer.parseInt(info[6].trim()),
Integer.parseInt(info[9].trim()),
MouseType.valueOf(info[2].trim().toUpperCase()));
productList.add(mouse);
} else {
keyboard = new Keyboard(Integer.parseInt(info[0].trim()),
info[3].trim(), info[4].trim(),
Connectivity.valueOf(info[5].trim().toUpperCase()),
Double.parseDouble(info[6].trim()),
Double.parseDouble(info[7].trim()),
Integer.parseInt(info[6].trim()),
KeyboardType.valueOf(info[2].trim().toUpperCase()),
KeyboardLayout.valueOf(info[9].trim().toUpperCase()));
productList.add(keyboard);
}
默认数据工作正常,但当我决定添加一个额外的项目时,它把一切搞砸了。
public void addStock() {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("Stock.txt"));
int max = productList.size();
for(int i = 0; i < max-2; i++) {
bw.write(productList.get(i).toString() + "\n");
}
bw.write(productList.get(max-1).toString());
} catch(IOException e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
finally {
try {
if(bw != null) {
bw.close();
}
} catch(IOException e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
}
以及它给我的错误:
java.lang.NumberFormatException: For input string: "15.0"
For input string: "15.0"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Reader.readStock(Reader.java:42)
at MainTest.main(MainTest.java:48)
以及文本文件的内容:
112233, mouse, GAMING, Logitech, black, WIRELESS, 15.0, 7.5, 15, 3
123456, keyboard, Corsair, black, WIRED, 2.0, 30.0, 2, GAMING, UK
124455, keyboard, Advent, white, WIRED, 10.0, 3.5, 10, STANDARD, UK
124566, mouse, STANDARD, Advent, grey, WIRED, 15.0, 2.5, 15, 2
125567, keyboard, Logitech, black, WIRELESS, 4.0, 25.99, 4, INTERNET, US
221101, mouse, STANDARD, Logitech, black, WIRED, 3.0, 3.0, 3, 3
221122, mouse, GAMING, Razer, black, WIRED, 1.0, 28.0, 1, 7
223044, mouse, GAMING, Anker, blue, WIRED, 3.0, 16.0, 3, 10
234555, keyboard, Apple, white, WIRELESS, 10.0, 75.5, 10, STANDARD, US
235066, keyboard, Microsoft, black, WIRELESS, 5.0, 45.5, 5, FLEXIBLE, UK
236677, mouse, STANDARD, Asus, blue, WIRELESS, 8.0, 10.0, 7, 5
237788, keyboard, Logitech, grey, WIRED, 15.0, 6.5, 15, STANDARD, UK
您正在将浮点数解析为整数。
将 parseInt 替换为 parseFloat,然后转换为 int
System.out.println( (int) Float.parseFloat("15.0".trim()) );
如果您需要一个浮点数形式的值,则不需要转换 :)