java equivalent function of c atoi() function

L

lonelyplanet999

Hi,

I would like to ask any java standard library function behaves the
same as c function atoi() which converts strings to integer equivalent
?

Tks!
 
D

David Zimmerman

lonelyplanet999 said:
Hi,

I would like to ask any java standard library function behaves the
same as c function atoi() which converts strings to integer equivalent
?

Tks!

Short.parseInt(String)
Inetger.parseInt(String)

depending on what an 'int' was in that particular c compiler

see also
Byte.parseByte(String)
Long.parseLong(String)
Float.parseFloat(String)
Double.parseDouble(String)

and if you aren't thinking in base 10

Integer.parseInt(String val, int radix)
 
L

lonelyplanet999

David Zimmerman said:
Short.parseInt(String)
Inetger.parseInt(String)

I tried applied parseInt to grab and decode numeric program arguments
supplied from command line. Below is the implementation code.

import java.lang.Integer;
import java.lang.String;

public class Argu {
public static void main (String[] args) {
for (int i=0; i<2; i++)
System.out.println(parseInt(args));
}
}

However, I got below compiler error as I compiled the code.

Argu.java:7: cannot resolve symbol
Symbol : method parseInt (java.lang.String)
location : class Argu
System.out.println(parseInt(args));
^
1 error

What's wrong with the code ? :)
 
S

Steve Horsley

lonelyplanet999 said:
I tried applied parseInt to grab and decode numeric program arguments
supplied from command line. Below is the implementation code.

import java.lang.Integer;
import java.lang.String;

There is no need to import java.lang stuff. It is imported implicitly.
public class Argu {
public static void main (String[] args) {
for (int i=0; i<2; i++)
System.out.println(parseInt(args));

Should be:
System.out.println(Integer.parseInt(args));

Also, you will need to catch the possible NumberFormatException that
Integer.parseInt might throw, so the call to Integer.parseInt will need
to be in a try block. Something like this:

public static void main(String[] args) {
for(int i = 0 ; i < args.length ; i++) {
try {
int x = Intger.parseInt(args);
System.out.println("Number: " + x);
}
catch(NumberFormatException nfe) {
System.out.println("Text: " + args);
}
}
}

Steve


Steve
 
N

nos

you need Integer.parseInt() not just parseInt()

lonelyplanet999 said:
David Zimmerman <[email protected]> wrote in message
Short.parseInt(String)
Inetger.parseInt(String)

I tried applied parseInt to grab and decode numeric program arguments
supplied from command line. Below is the implementation code.

import java.lang.Integer;
import java.lang.String;

public class Argu {
public static void main (String[] args) {
for (int i=0; i<2; i++)
System.out.println(parseInt(args));
}
}

However, I got below compiler error as I compiled the code.

Argu.java:7: cannot resolve symbol
Symbol : method parseInt (java.lang.String)
location : class Argu
System.out.println(parseInt(args));
^
1 error

What's wrong with the code ? :)
depending on what an 'int' was in that particular c compiler

see also
Byte.parseByte(String)
Long.parseLong(String)
Float.parseFloat(String)
Double.parseDouble(String)

and if you aren't thinking in base 10

Integer.parseInt(String val, int radix)
 
A

ak

See http://gojava.imagero.com/


lonelyplanet999 said:
David Zimmerman <[email protected]> wrote in message
Short.parseInt(String)
Inetger.parseInt(String)

I tried applied parseInt to grab and decode numeric program arguments
supplied from command line. Below is the implementation code.

import java.lang.Integer;
import java.lang.String;

public class Argu {
public static void main (String[] args) {
for (int i=0; i<2; i++)
System.out.println(parseInt(args));
}
}

However, I got below compiler error as I compiled the code.

Argu.java:7: cannot resolve symbol
Symbol : method parseInt (java.lang.String)
location : class Argu
System.out.println(parseInt(args));
^
1 error

What's wrong with the code ? :)
depending on what an 'int' was in that particular c compiler

see also
Byte.parseByte(String)
Long.parseLong(String)
Float.parseFloat(String)
Double.parseDouble(String)

and if you aren't thinking in base 10

Integer.parseInt(String val, int radix)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top