Converting a String to an int ...

hi all,

I have a String s1 = "0123456789" and I want to deal with it as an int. That is, I want to multiply it by some numbers. I tried to cast it but it didn't work.

please don't tell me to difine it as int num1 = 0123456789; because the varialble I want to deal with is a string that comes to my application from another application as a string and I can't do anything about it.

regards

[416 byte] By [Raieda] at [2007-11-27 10:57:31]
# 1

Integer.parseInt()

~

yawmarka at 2007-7-29 12:09:38 > top of Java-index,Java Essentials,Java Programming...
# 2

Check what the Integer class can do for you:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html

TimTheEnchantora at 2007-7-29 12:09:38 > top of Java-index,Java Essentials,Java Programming...
# 3

Typing your subject line into the "Search Forums" text field yields:

http://onesearch.sun.com/search/onesearch/index.jsp?qt=Converting+a+String+to+an+int+&subCat=siteforumid%3Ajava31&site=dev&dftab=siteforumid%3Ajava31&chooseCat=javaall&col=developer-forums

Are any of those posts helpful?

Good luck

Lee

tsitha at 2007-7-29 12:09:38 > top of Java-index,Java Essentials,Java Programming...
# 4

> please don't tell me to define it as > int num1 = 0123456789;

> because the variable I want to deal with

> is a string that comes to my application from another

> application as a string and I can't do anything about it.

That would not compile anyway.

The leading zero makes it an octal literal and only digits [0-7] are allowed.

tschodta at 2007-7-29 12:09:38 > top of Java-index,Java Essentials,Java Programming...
# 5

tschodt, good catch. But the number 0123456789 is just an example.

Raieda at 2007-7-29 12:09:38 > top of Java-index,Java Essentials,Java Programming...
# 6

TimTheEnchantor, thanks for your response,

I tried to do the following:

String s1 = "23";

int a = 0;

Integer e = new Integer(s1);

a = e * 5;

it says cannot convert from Integer to int and it also says that the operators (*,/,<,>,......) are not defined for the type integer.

any ideas ?

Raieda at 2007-7-29 12:09:38 > top of Java-index,Java Essentials,Java Programming...
# 7

You're almost there. Don't use the constructor, search for a static method that takes a string and returns the parsed int.

JoachimSauera at 2007-7-29 12:09:38 > top of Java-index,Java Essentials,Java Programming...
# 8

> You're almost there. Don't use the constructor,

> search for a static method that takes a string and

> returns the parsed int.

Just to add to JoachimSauer's suggestion: read reply #1.

~

yawmarka at 2007-7-29 12:09:38 > top of Java-index,Java Essentials,Java Programming...
# 9

public class StringToInt {

public static void main (String[] args) {

String s1 = "0123456789" ;

try {

int i = Integer.parseInt(s1.trim());

System.out.println("int i = " + i);

} catch (NumberFormatException nfe) {

System.out.println("NumberFormatException: " + nfe.getMessage());

}

}

}

Prem_Sa at 2007-7-29 12:09:38 > top of Java-index,Java Essentials,Java Programming...
# 10

Is there an advantage to using Integer.parseInt() over Integer.decode()? It seems as if decode() calls parseInt() anyway.

Djaunla at 2007-7-29 12:09:38 > top of Java-index,Java Essentials,Java Programming...
# 11

> Is there an advantage to using Integer.parseInt()

> over Integer.decode()? It seems as if decode() calls

> parseInt() anyway.

They have different specs. decode accepts hex, decimal and octal formats, while parseInt accepts only decimal.

Hippolytea at 2007-7-29 12:09:38 > top of Java-index,Java Essentials,Java Programming...
# 12

thanks guys, I did the following and it worked:

String s = "123";

Integer i = new Integer(s);

int a = s.intValue();

thanks :)

Raieda at 2007-7-29 12:09:38 > top of Java-index,Java Essentials,Java Programming...
# 13

> They have different specs. decode accepts hex,

> decimal and octal formats, while parseInt accepts

> only decimal.

Not quite what you were talking about, but parseInt() is overloaded to handle more than just decimal...

http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#parseInt(java.lang.String,%20int)

~

yawmarka at 2007-7-29 12:09:38 > top of Java-index,Java Essentials,Java Programming...
# 14

> String s = "123";

> Integer i = new Integer(s);

> int a = s.intValue();

If you feel like doing that more simply, follow the suggestions given:

String s = "123";

int a = Integer.parseInt(s);

Hippolytea at 2007-7-29 12:09:38 > top of Java-index,Java Essentials,Java Programming...
# 15

String s = "123";

Integer i = new Integer(s);

You can do that?

jGardnera at 2007-7-29 12:09:43 > top of Java-index,Java Essentials,Java Programming...
# 16

> String s = "123";

> Integer i = new Integer(s);

>

> You can do that?

Yes, for as long as the Integer class has existed.

You can also do...

int i = new Integer(s);

...with Java 5 and later.

~

yawmarka at 2007-7-29 12:09:43 > top of Java-index,Java Essentials,Java Programming...