我正在尝试将带有文本和数字的数组转换为int。
数组separated
:
我想要的是[1]
最终成为一个int。
我试过了:
int logSetting = Integer.parseInt(String.valueOf(separated));
和
int logSetting = Integer.parseInt(String.valueOf(separated[1]));
两者都使应用程序崩溃
09-16 09:57:30.865: E/AndroidRuntime(27938): FATAL EXCEPTION: main 09-16 09:57:30.865: E/AndroidRuntime(27938): Process: my.project, PID: 27938 09-16 09:57:30.865: E/AndroidRuntime(27938): java.lang.NumberFormatException: Invalid int:
“[Ljava.lang.String; @ 42b273f0”
09-16 09:57:30.865: E/AndroidRuntime(27938): at java.lang.Integer.invalidInt(Integer.java:137) 09-16 09:57:30.865: E/AndroidRuntime(27938): at java.lang.Integer.parse(Integer.java:374) 09-16 09:57:30.865: E/AndroidRuntime(27938): at java.lang.Integer.parseInt(Integer.java:365) 09-16 09:57:30.865: E/AndroidRuntime(27938): at java.lang.Integer.parseInt(Integer.java:331) 09-16 09:57:30.865: E/AndroidRuntime(27938): at my.project.MainActivity.fileReader(MainActivity.java:928) 09-16 09:57:30.865: E/AndroidRuntime(27938): at my.project.MainActivity.logCat(MainActivity.java:944) 09-16 09:57:30.865: E/AndroidRuntime(27938): at my.project.MainActivity$1.run(MainActivity.java:854) 09-16 09:57:30.865: E/AndroidRuntime(27938): at android.os.Handler.handleCallback(Handler.java:733) 09-16 09:57:30.865: E/AndroidRuntime(27938): at android.os.Handler.dispatchMessage(Handler.java:95) 09-16 09:57:30.865: E/AndroidRuntime(27938): at android.os.Looper.loop(Looper.java:157) 09-16 09:57:30.865: E/AndroidRuntime(27938): at android.app.ActivityThread.main(ActivityThread.java:5356) 09-16 09:57:30.865: E/AndroidRuntime(27938): at java.lang.reflect.Method.invokeNative(Native Method) 09-16 09:57:30.865: E/AndroidRuntime(27938): at java.lang.reflect.Method.invoke(Method.java:515) 09-16 09:57:30.865: E/AndroidRuntime(27938): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) 09-16 09:57:30.865: E/AndroidRuntime(27938): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 09-16 09:57:30.865: E/AndroidRuntime(27938): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:4)
你可以做这样的事情
String[] separated=new String[]{"some text ","1","some more text"};
int logSetting = 0;
for(String i:separated){
try {
logSetting = Integer.parseInt(i);
}catch (NumberFormatException e){
System.out.println("NumberFormatException \""+i+"\" is not a number");
}
}
System.out.println(logSetting);
Out put:
NumberFormatException "some text " is not a number
NumberFormatException "some more text" is not a number
1
答案 1 :(得分:1)
你快到了,int logSetting = Integer.parseInt(String.valueOf(separated[1]));
应该是int logSetting = Integer.parseInt(separated[1]);
。如果这不起作用,它可能会导致代码崩溃。你得到什么错误?
答案 2 :(得分:0)
基于您的NumberFormatException
异常,当您尝试将String转换为数字值时,如int,float,double,long等,其格式不正确
例如:
public class ConvertStringToNumber
{
public static void main(String[] args)
{
try
{
String s = "FOOBAR";
int i = Integer.parseInt(s); <--this line of code will never be reached
System.out.println("int value = " + i);
}
catch (NumberFormatException nfe)
{
nfe.printStackTrace();
}
}