我在使用stackoverflow atm进行编程时非常糟糕,对我很轻松,我正在努力学习。从阅读此代码,用于避免NumberFormatExpection的正确语法是什么? `
package in.parapengu.spork.rotation;
import com.google.common.base.Charsets;
import in.parapengu.commons.utils.file.TextFile;
import in.parapengu.spork.Spork;
import in.parapengu.spork.exception.rotation.RotationLoadException;
import in.parapengu.spork.map.MapLoader;
import in.parapengu.spork.util.Log;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
public class Rotation {
private static int MAX_SIZE = 30;
private static boolean OVERFLOW = true;
private File file;
private int index;
private List<MapLoader> loaders;
private List<RotationSlot> slots;
public Rotation(File file, List<MapLoader> loaders, int index) {
this.file = file;
this.index = index;
this.loaders = loaders;
this.slots = new ArrayList<>();
if(loaders.size() > MAX_SIZE && OVERFLOW) {
for(MapLoader loader : loaders) {
try {
slots.add(new RotationSlot(loader));
} catch(Exception ex) {
ex.printStackTrace();
}
}
} else {
try {
while(slots.size() < MAX_SIZE) {
for(int i = 0; i < loaders.size() && slots.size() < MAX_SIZE; i++) {
slots.add(new RotationSlot(loaders.get(i)));
}
if(slots.size() == 0) {
Log.warning("No slots were loaded");
break;
}
}
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
public Rotation(File file, List<MapLoader> loaders) {
this(file, loaders, 0);
}
public File getFile() {
return file;
}
public int getIndex() {
return index;
}
public List<RotationSlot> getSlots() {
return slots;
}
public boolean save() {
TextFile file;
try {
file = new TextFile(this.file);
} catch(IOException ex) {
ex.printStackTrace();
Log.exception(ex);
return false;
}
file.line(index + "");
for(MapLoader loader : loaders) {
file.line(loader.getName());
}
file.save();
return true;
}
public static Rotation parse(File file) throws RotationLoadException {
if(!file.exists()) {
throw new RotationLoadException(file, file.getName() + " does not exist");
}
if(file.isDirectory()) {
throw new RotationLoadException(file, file.getName() + " was a directory");
}
List<String> lines;
try {
lines = Files.readAllLines(file.toPath(), Charsets.UTF_8);
} catch(IOException ex) {
ex.printStackTrace();
throw new RotationLoadException(file, ex);
}
String first = lines.get(0);
int index;
try {
index = Integer.parseInt(first);
} catch(NumberFormatException ex) {
ex.printStackTrace();
throw new RotationLoadException(file, "The rotation index was not a valid number");
}
lines.remove(0);
List<MapLoader> maps = new ArrayList<>();
for(String line : lines) {
MapLoader loader = Spork.getMaps().getMap(line);
if(loader == null) {
throw new RotationLoadException(file, "Could not find a map matching \"" + line + "\"");
}
maps.add(loader);
}
return new Rotation(file, maps, index);
}
}
`
这是我在控制台中遇到的错误: `
[06:27:59 WARN]: java.lang.NumberFormatException: For input string: "1Test"
[06:27:59 WARN]: at java.lang.NumberFormatException.forInputString(Unknow
n Source)
[06:27:59 WARN]: at java.lang.Integer.parseInt(Unknown Source)
[06:27:59 WARN]: at java.lang.Integer.parseInt(Unknown Source)
[06:27:59 WARN]: at in.parapengu.spork.rotation.Rotation.parse(Rotation.j
ava:115)
`
答案 0 :(得分:1)
NumberFormatException的原因是因为,您正在尝试转换alpha数字字符串&#34; 1Test&#34;数字。
尝试删除字符串中的所有字符,如:
String name ="1Test";//why are you reading string?
Integer number = Integer.parseInt(name.replaceAll("[^0-9]", ""));
System.out.println(number);
您正在正确捕获异常并重新抛出RotationLoadException。