Waqleh
Waqleh

Reputation: 10161

convert String to int to get a unique number

hi i am trying to convert a sting such as "abc-d1_23QWEwer" to an int number

StringBuilder sb = new StringBuilder();
int intServiceName = 0;
String stringServiceValue;
for (int i = 0; i < serviceName.length(); i++){
    if (DEBUG) Log.i(TAG, "serviceName.length() loop i: " + i);
    sb.append(String.valueOf(Character.getNumericValue(serviceName.charAt(i))));
    if (DEBUG) Log.i(TAG, "serviceName.length() loop i after: " + i);
}
if (DEBUG) Log.i(TAG, "serviceName.length() loop end2");
stringServiceValue = sb.toString();
if (DEBUG) Log.i(TAG, "serviceName.length() loop end: "+stringServiceValue.replaceAll("\\D+",""));
stringServiceValue = stringServiceValue.replaceAll("\\D+","");
if (DEBUG) Log.i(TAG, "serviceName.length() loop endstringServiceValue: "+ stringServiceValue);
intServiceName = Integer.parseInt(stringServiceValue);
if (DEBUG) Log.i(TAG, "serviceName.length() loop end123123: "+ String.valueOf(intServiceName));

i am getting an error when the code reaches this line:

intServiceName = Integer.parseInt(stringServiceValue);

what am i doing wrong? this is the error log

06-21 19:12:54.760: E/AndroidRuntime(11139): FATAL EXCEPTION: AsyncTask #4
06-21 19:12:54.760: E/AndroidRuntime(11139): java.lang.RuntimeException: An error occured while executing doInBackground()
06-21 19:12:54.760: E/AndroidRuntime(11139):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.lang.Thread.run(Thread.java:856)
06-21 19:12:54.760: E/AndroidRuntime(11139): Caused by: java.lang.NumberFormatException: Invalid int: "2914282912918292114"
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.lang.Integer.invalidInt(Integer.java:138)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.lang.Integer.parse(Integer.java:378)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.lang.Integer.parseInt(Integer.java:366)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.lang.Integer.parseInt(Integer.java:332)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at com.wr.noc.AsyncTasks.doInBackground(AsyncTasks.java:113)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at com.wr.noc.AsyncTasks.doInBackground(AsyncTasks.java:1)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-21 19:12:54.760: E/AndroidRuntime(11139):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-21 19:12:54.760: E/AndroidRuntime(11139):    ... 5 more

Upvotes: 0

Views: 1118

Answers (6)

Waqleh
Waqleh

Reputation: 10161

use long instead of integer. your number is too big you could do something like this if you need an int not a long

sb.append(String.valueOf(Character.getNumericValue(serviceName.charAt(i))).substring(0, 1));

Upvotes: 1

Oleksiy
Oleksiy

Reputation: 39810

go through the whole string like this:

// not sure if the syntax is correct

int myInt[] = new int[myString.length()];

for (int i = 0; i < myString.length(); i++)
    myInt.add(myString[i]);

Upvotes: 0

AllTooSir
AllTooSir

Reputation: 49372

Your number 2914282912918292114 is too large to be an int . The largest value int can hold is 2,147,483,647. You need to use longwhich can hold maximum value of 9,223,372,036,854,775,807.

 Long.parseLong(yourString);

If you are dealing with huge numbers , you can use the BigInteger class for integers and BigDecimal for numbers with decimal digits.

Upvotes: 1

Shrike
Shrike

Reputation: 155

The number 2,914,282,912,918,292,114 is too big to fit in an int. It just fits inside a long (the max value for a signed long is 9,223,372,036,854,775,807), so you could do

long l = Long.parseLong(someString);

Although a better solution would be to ask yourself why you need numbers that are so huge. A redesign of your algorithm to avoid having such large numbers would make things easier as you go on.

Upvotes: 2

Thierry
Thierry

Reputation: 5233

Your number is too large for an int. Consider to use a long :

numServiceName = Long.parseLong(stringServiceValue);

Upvotes: 1

Alessandro Massa
Alessandro Massa

Reputation: 93

Observe :

06-21 19:12:54.760: E/AndroidRuntime(11139): Caused by: java.lang.NumberFormatException: Invalid int: "2914282912918292114"

I think it goes overflow, you might use a long instead.

Upvotes: 5

Related Questions