Ryada
Ryada

Reputation: 95

how to convert -negative values in strings to double

i need to read the values from a file(.csv) ,which contains both positive and negative values.. i am getting an error while converting string to integer.

    Scanner sc = new Scanner(getAssets().open("Book1.csv")).useDelimiter(",");
    int i = 0;
    while (sc.hasNext()) {
        String str  = sc.next();
        data[i] = new GraphViewData(i, Integer.parseInt(str));
        i++;
    }

Below is my logcat :

05-02 04:05:55.119: E/AndroidRuntime(3003): ' as integer
05-02 04:05:55.119: E/AndroidRuntime(3003):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-02 04:05:55.119: E/AndroidRuntime(3003):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-02 04:05:55.119: E/AndroidRuntime(3003):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-02 04:05:55.119: E/AndroidRuntime(3003):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-02 04:05:55.119: E/AndroidRuntime(3003):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 04:05:55.119: E/AndroidRuntime(3003):     at android.os.Looper.loop(Looper.java:123)
05-02 04:05:55.119: E/AndroidRuntime(3003):     at android.app.ActivityThread.main(ActivityThread.java:3683)
05-02 04:05:55.119: E/AndroidRuntime(3003):     at java.lang.reflect.Method.invokeNative(Native Method)
05-02 04:05:55.119: E/AndroidRuntime(3003):     at java.lang.reflect.Method.invoke(Method.java:507)
05-02 04:05:55.119: E/AndroidRuntime(3003):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-02 04:05:55.119: E/AndroidRuntime(3003):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-02 04:05:55.119: E/AndroidRuntime(3003):     at dalvik.system.NativeStart.main(Native Method)
05-02 04:05:55.119: E/AndroidRuntime(3003): Caused by: java.lang.NumberFormatException: unable to parse '893

Upvotes: 2

Views: 15840

Answers (5)

Grebyn
Grebyn

Reputation: 39

The minus sign imported from the csv file of yours is of another ASCII. A simple and fast way is to:

str = str.replaceAll("−", "-")

Upvotes: 0

Paritosh Agrawal
Paritosh Agrawal

Reputation: 284

You could something like this

String negNum = "-743.347";
Double num = Double.valueOf(negNum);
System.out.println("Negative number: " + num);

Upvotes: 2

Martin Wilson
Martin Wilson

Reputation: 3386

To convert negative values in strings to Integer you can use Integer.parseInt as you show in your code.

I don't think the error you are seeing is to do with the minus sign - it's ok to pass a string that starts with an ASCII minus sign (see http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html#parseInt(java.lang.String))

BTW - are you trying to convert to Integer or Double? your question title mentions Double (Double.parseDouble also accepts the sign)

Upvotes: 2

npinti
npinti

Reputation: 52185

The Integer.parseInt should work for negative numbers. However, note that you could have written the negative number as follows - 3 instead of just -3. The extra space will cause the parsing to fail. You will need to make sure that you are actually trying to parse actual numbers without any other strings in them.

You could use something like so Integer.parseInt(str.replaceAll("[^\\d-]+", "")); to remove all non numeric data.

EDIT: As per your exception, you seem to have an extra ` in your string, which is causing the conversion to fail. Please make sure that you only have numbers when you try and parse strings to numbers.

Upvotes: 1

Krishnakant Dalal
Krishnakant Dalal

Reputation: 3578

You could use this...

Math.abs(x)

Upvotes: 0

Related Questions