1

I am trying to convert a array with text and numbers to an int.

the array separated:

  • separated[0] some text
  • separated[1] 1
  • separated[2] some more text

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)
5
  • put your logcat error Commented Sep 16, 2014 at 7:54
  • What kind of "crash" happened in second case? Please post the stack trace. Commented Sep 16, 2014 at 7:54
  • separated[1] 1. what does 1 without bracket mean here? Commented Sep 16, 2014 at 7:54
  • 1
    What exception you get? Commented Sep 16, 2014 at 7:55
  • 2
    did you try Integer.parseInt(separated[1]);? if you do Integer.parseInt(separated[0]); it should throw error, because text can't convert to int Commented Sep 16, 2014 at 7:55

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

Comments

1

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?

2 Comments

are you sure about your answer? the op got numberformatexcepion
If all he has in the index [1] is "1" this should not throw the exception. If it does I have no idea what is wrong.
0

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();
    }
  }
  1. Source for the Code
  2. Read more and Another Source

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.