英文:
Efficiently parse string to a class object with 2 floats in java
问题
我需要将输入的字符串解析为一个由两个浮点数组成的新类对象。我确实有一个解决方案,但看起来有些繁琐。我想知道是否有一种更优雅的方法来做这件事,尤其是如果字符串仅分割为2个子字符串,而循环似乎有点过度?
我的类:
public class Float2 {
public float width;
public float height;
}
将输入字符串解析为类对象的方法:
public Float2 parseStringToFloat2(String inputstring) {
String[] stringarray = inputstring.split(",");
float[] floats = new float[2];
for (int i = 0; i < 2; ++i) {
float number = Float.parseFloat(stringarray[i]);
floats[i] = number;
}
return new Float2(floats[0], floats[1]);
}
英文:
I need to parse an input string into a new class object consisting of two floats. I do have a solution but it looks cumbersome. I wondered if there is a more elegant way to do it, esp. if the string splits only to 2 substrings and a cycle seems to be a bit of overkill?
my class:
public class Float2 {
public float width;
public float height;
}
my method to parse the input string into a class object:
public Float2 parseStringToFloat2(String inputstring) {
String[] stringarray = inputstring.split(",");
float[] floats = new float[2];
for (int i = 0; i <2; ++i) {
float number = Float.parseFloat(stringarray[i]);
floats[i] = number;
}
return new Float2(floats[0], floats[1]);
}
答案1
得分: 2
我确实认为如果你确定只会有2个部分,那么使用循环会过于复杂。也许可以尝试这样写:
public Float2 parseStringToFloat2(String inputstring){
String[] stringarray = inputstring.split(",");
try {
return new Float2(Float.parseFloat(stringarray[0]), Float.parseFloat(stringarray[1]));
} catch (Exception e) {
// 捕获逻辑
}
return null;
}
正如评论中所说,你还应该在发生转换错误的情况下使用try catch逻辑。
英文:
I do think the loop is an overkill if you know for sure there will by only 2 parts.
Maybe try this:
public Float2 parseStringToFloat2(String inputstring){
String[] stringarray = inputstring.split(",");
try {
return new Float2(Float.parseFloat(stringarray[0]), Float.parseFloat(stringarray[1]));
} catch (Exception e) {
// catch logic
}
return null;
}
As said in a comment, you should also use try catch logic in case of a conversion error.
答案2
得分: 1
另一种解决方案是使用 Scanner
。如果您需要区域特定的解析(它在不设置默认区域设置的情况下使用默认区域设置,如果在那里“,”是小数分隔符,则可能会有问题),这是一种更灵活的解决方案。另外,如果您使用正则表达式作为分隔符,模式可以预编译以提高速度。
public static Optional<Float2> parseStringToFloat2(String inputstring) {
final Scanner in = new Scanner(inputstring).useLocale(Locale.US).useDelimiter(", ");
// in.useDelimiter(Pattern.compile("\\s*,\\s*"));
try {
return Optional.of(new Float2(in.nextFloat(), in.nextFloat()));
} catch (NoSuchElementException e) {
return Optional.empty();
}
}
英文:
Another solution would be to use a Scanner
. It is a more flexible solution if you need Locale-specific parsing (it uses the default locale without setting it, which could be problematic if a ,
is a decimal separator there). Also if you use a regex delim, the pattern can be precompiled to be faster.
public static Optional<Float2> parseStringToFloat2(String inputstring) {
final Scanner in = new Scanner(inputstring).useLocale(Locale.US).useDelimiter(", ");
// in.useDelimiter(Pattern.compile("\\s*,\\s*"));
try {
return Optional.of(new Float2(in.nextFloat(), in.nextFloat()));
} catch (NoSuchElementException e) {
return Optional.empty();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论