Reputation: 714
I am trying to convert a array with text and numbers to an int.
the array separated
:
What I want is that the [1]
ends up being a int.
I tried:
int logSetting = Integer.parseInt(String.valueOf(separated));
and
int logSetting = Integer.parseInt(String.valueOf(separated[1]));
Both made the app crash
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)
Upvotes: 1
Views: 141
Reputation: 6739
Based on your exception which is NumberFormatException
, when you try to convert a String to a numeric value, like an int, float, double, long, etc which does not have right format
For example:
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();
}
}
Upvotes: 0
Reputation: 461
You're almost there, int logSetting = Integer.parseInt(String.valueOf(separated[1]));
should just be int logSetting = Integer.parseInt(separated[1]);
. If this doesn't work its probably something else crashing your code. What error do you get?
Upvotes: 1
Reputation: 35587
You can do something like this
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
Upvotes: 4