Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > String to Integer

Reply
Thread Tools

String to Integer

 
 
adam
Guest
Posts: n/a
 
      06-21-2008
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(Unk nown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
---

Thanks in advance!
 
Reply With Quote
 
 
 
 
adam
Guest
Posts: n/a
 
      06-21-2008
Lew wrote:

> 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.
 
Reply With Quote
 
 
 
 
Mark Space
Guest
Posts: n/a
 
      06-21-2008
adam wrote:
> Lew wrote:
>
>> 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??


\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.
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      06-21-2008
adam wrote:
> Lew wrote:
>
>> 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.

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>

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-21-2008
On Sat, 21 Jun 2008 15:41:02 +0200, adam <(E-Mail Removed)> wrote,
quoted or indirectly quoted someone who said :

>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';
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is there a way to AutoParse a string to another type - e.g. if aDate format then date, else if integer than Integer etc ????? Greg Hauptmann Ruby 6 08-06-2008 04:52 PM
Change a string to an integer, report an error if the string does not represent an integer? Randy Kramer Ruby 12 10-25-2007 09:56 PM
CType(x,Integer) vs. Integer.Parse(x) =?Utf-8?B?Sm9l?= ASP .Net 7 02-07-2006 02:30 AM
How do I add an Integer to another Integer? Sebastian Stelzer Java 2 10-15-2004 01:17 PM
No Math.min(Integer, Integer)? =?ISO-8859-1?Q?Thomas_Gagn=E9?= Java 0 07-29-2003 07:46 PM



Advertisments