Skip to main content

APIzation: Replication Package

[Q#27372530][A#27372799] How to parse string containing negative number into integer?

https://stackoverflow.com/q/27372530

Part of my code is here! 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.

Answer

https://stackoverflow.com/a/27372799

Works without a problem. Output: 1 -3

APIzation

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);
package com.stackoverflow.api;

public class Human27372799 {

  public static String[] parseNumbers(String message) {
    String[] msg = message.split(",");
    return msg;
  }
}

package com.stackoverflow.api;

/**
 * How to parse string containing negative number into integer?
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/27372799">https://stackoverflow.com/a/27372799</a>
 */
public class APIzator27372799 {

  public static int parseString(String message) throws Exception {
    String[] msg = message.split(",");
    int x = Integer.parseInt(msg[0]);
    int y = Integer.parseInt(msg[1]);
    System.out.println(x);
    return y;
  }
}