How to parse string containing negative number into integer?

37,854
String message = "1,-3";
String[] msg = message.split(",");
int x = Integer.parseInt(msg[0]);
int y = Integer.parseInt(msg[1]);

System.out.println(x);
System.out.println(y);

Works without a problem. Output:

1

-3

Share:
37,854
Suroor Ahmmad
Author by

Suroor Ahmmad

Student of Computer Science & Engineering from India. Beginner in every coding languages. Here to gain coding knowledge to answer what I know to needies. (:

Updated on December 09, 2020

Comments

  • Suroor Ahmmad
    Suroor Ahmmad over 3 years

    Part of my code is here!

    bufferedReader=new BufferedReader (inputstreamreader);
    message=bufferedReader.readLine ();// ex: message has (1,-3)
    String[] msg=message.split (",") //I use comma (,) as deliminator
    int x=Integer.parseInt (msg [0]);
    int y=Integer.parseInt (msg [1]);
    

    This clearly parses but the problem is it looses negative sign. That is the "message" contains (1,-3). Pls help me to parse without loosing -ve sign.