转换 String[] 参数。在java中int/double?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23557842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting String[] args. to int/double in java?
提问by joelwmale
I'm trying to convert a main programs "String[] args" into 5 ints, and 4 doubles. (i know it's kind of confusing)
我正在尝试将主程序“String[] args”转换为 5 个整数和 4 个双精度数。(我知道这有点令人困惑)
The way i was thinking of doing it was having a forreach loop to loop through String args[] and add them to an int array (for the first 5), however i had to use for the array, so it doesn't work.
我想这样做的方式是有一个 forreach 循环来循环遍历 String args[] 并将它们添加到一个 int 数组(对于前 5 个),但是我必须使用该数组,所以它不起作用。
if (args != null) {
// If arguments where given, use the arguments and run the program
// NEED TO SOMEHOW CONVERT ARGS INTO INTEGER FORM AFTER BEING PASSED IN
ArrayList<Integer> valArr = new ArrayList<Integer>(10); // could be 9
for (String val : args) {
// Parse the string and add it to valArray
int temp = Integer.parseInt(val);
valArr.add(temp);
}
CarPark cp = new CarPark(valArr[0], valArr[1], valArr[2], valArr[3]);
this is what i'm looking at at the moment... am i completely wrong? or close?
这就是我目前正在看的……我完全错了吗?或关闭?
采纳答案by merlin2011
Just parse it with two indexing for
loops.
只需用两个索引for
循环解析它。
int[] myInts = new int[5];
double[] myDoubles = new double[4];
// Initialize these arrays with "defaults" here.
// Notice how we handle the possibility of the user not entering enough values.
for (int i = 0; i < Math.min(5, args.length); i++)
myInts[i] = Integer.parseInt(args[i]);
for (int i = 0; i < Math.min(4, args.length - 5); i++)
myDoubles[i] = Double.parseDouble(args[i+5]);
CarPark cp = new CarPark(myInts[0], myInts[1], myInts[2], myInts[3]);
回答by Vishnudev K
you have to get the values from the array list as given below
您必须从数组列表中获取值,如下所示
CarPark cp = new CarPark(valArr.get(0), valArr.get(1), valArr.get(2), valArr.get(3));
回答by subash
look at ArrayList Doc
above could be like this
上面可能是这样的
CarPark cp = new CarPark(valArr.get(0), valArr.get(1), valArr.get(2), valArr.get(3));
回答by Sanjaya Pandey
I think this should work.
我认为这应该有效。
if (args != null) {
// If arguments where given, use the arguments and run the program
// NEED TO SOMEHOW CONVERT ARGS INTO INTEGER FORM AFTER BEING PASSED IN
ArrayList<Integer> valArr = new ArrayList<Integer>(10); // could be 9
for (String val : args) {
// Parse the string and add it to valArray
int temp = Integer.parseInt(val);
valArr.add(temp);
}
CarPark cp = new CarPark(valArr.get(0), valArr.get(1), valArr.get(2), valArr.get(3));
回答by Mustafa sabir
Here is your corrected code:-
这是您更正的代码:-
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
args=new String[4];
for(int i=0;i<4;i++){
System.out.println("Enter value no- "+(i+1));
args[i]=sc.nextLine();
}
ArrayList<Integer> valArr = new ArrayList<Integer>(10);
if (args != null) {
// If arguments where given, use the arguments and run the program
// NEED TO SOMEHOW CONVERT ARGS INTO INTEGER FORM AFTER BEING PASSED IN
// could be 9
for (String val : args) {
// Parse the string and add it to valArray
int temp = Integer.parseInt(val.trim());
valArr.add(temp);
}
}
System.out.println(valArr);
//CarPark cp = new CarPark(valArr[0], valArr[1], valArr[2], valArr[3]);
}
}
This is just the code for Integer part in similar way you can extend the args array length and include double values, Remeber to use val.trim() to remove whitespaces otherwise it will throw NumberFormatException.
这只是 Integer 部分的代码,您可以以类似的方式扩展 args 数组长度并包含双精度值,请记住使用 val.trim() 删除空格,否则会抛出 NumberFormatException。