String to Integer

A

adam

Hi!!

How can I convert an String into an Integer? I'm trying to use
'parseInt' but I only get a "java.lang.NumberFormatException".

String s = '\n';
Integer i = Integer.parseInt(s); // i should be 10

I also tried with Integer.valueof(s) with no success. It works for a
char, but not for a String. How can I solve it??

---
Exception in thread "main" java.lang.NumberFormatException: For input
string: "hello"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
 
A

adam

Lew said:
Integer i = '\n';

is what you want.

Thanks for answering so quickly!

The thing is that I have a String var with characters such as "0", "a",
"\n"... I use String.charAt(0) for getting the first character of the
string. It works nice when I'm using a normal character ('0', 'a'...)
but it doesnt with escape characters (it gets '\' instead of '\n').

So the question is: how can convert a string "\n" into an integer?
Or how can I convert "\n" into a char for then converting it to an integer??

Thanks in advance.
 
M

Mark Space

adam said:
Thanks for answering so quickly!

The thing is that I have a String var with characters such as "0", "a",
"\n"... I use String.charAt(0) for getting the first character of the
string. It works nice when I'm using a normal character ('0', 'a'...)
but it doesnt with escape characters (it gets '\' instead of '\n').

So the question is: how can convert a string "\n" into an integer?
Or how can I convert "\n" into a char for then converting it to an
integer??

\n is an escape sequence for string and character literals. If read
from a file or the keyboard, the IO system does not assume that you
really meant a \ to mean something else, it just reads what is there.
If you want a newline in a file or at the keyboard, hit Enter.

I don't know of Java class that will parse string literal escapes for
you. Considering there's only a few of them, doing it yourself should
not be hard.
 
D

Daniel Pitts

adam said:
Thanks for answering so quickly!

The thing is that I have a String var with characters such as "0", "a",
"\n"... I use String.charAt(0) for getting the first character of the
string. It works nice when I'm using a normal character ('0', 'a'...)
but it doesnt with escape characters (it gets '\' instead of '\n').

So the question is: how can convert a string "\n" into an integer?
Or how can I convert "\n" into a char for then converting it to an
integer??

Thanks in advance.
I disagree:
<sscce>
public class Main {
public static void main(String[] args) {
String string = "Test\n";
for (int i = 0; i < string.length(); ++i) {
char c = string.charAt(i);
System.out.print(c);
System.out.println(" - " + (int)c);
}
}
}
</sscce>
<output>
T - 84
e - 101
s - 115
t - 116

- 10
</output>
 
R

Roedy Green

String s = '\n';
Integer i = Integer.parseInt(s); // i should be 10

if you simply want the 10, there is no need to parse, namely convert a
string a character digits to binary. It is already in binary.

parseInt wants something like this:
String s = "10";
Integer i = Integer.parseInt(s); // i should be 10


Just do

int lf = '\n';
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top