String to Integer

Discussion in 'Java' started by adam, Jun 21, 2008.

  1. adam

    adam Guest

    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)
    ---

    Thanks in advance!
    adam, Jun 21, 2008
    #1
    1. Advertising

  2. adam

    adam Guest

    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.
    adam, Jun 21, 2008
    #2
    1. Advertising

  3. adam

    Mark Space Guest

    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.
    Mark Space, Jun 21, 2008
    #3
  4. adam

    Daniel Pitts Guest

    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/>
    Daniel Pitts, Jun 21, 2008
    #4
  5. adam

    Roedy Green Guest

    On Sat, 21 Jun 2008 15:41:02 +0200, adam <> 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
    Roedy Green, Jun 21, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    CType(x,Integer) vs. Integer.Parse(x)

    =?Utf-8?B?Sm9l?=, Feb 6, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,937
    =?Utf-8?B?RGF2aWQgQW50b24=?=
    Feb 7, 2006
  2. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    No Math.min(Integer, Integer)?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 29, 2003, in forum: Java
    Replies:
    0
    Views:
    488
    =?ISO-8859-1?Q?Thomas_Gagn=E9?=
    Jul 29, 2003
  3. Sebastian Stelzer

    How do I add an Integer to another Integer?

    Sebastian Stelzer, Oct 14, 2004, in forum: Java
    Replies:
    2
    Views:
    479
    Yu SONG
    Oct 15, 2004
  4. Sebastian Stelzer

    How do I add an Integer to another Integer?

    Sebastian Stelzer, Oct 14, 2004, in forum: Java
    Replies:
    6
    Views:
    45,323
    JavaBean2010
    Apr 7, 2010
  5. Randy Kramer
    Replies:
    12
    Views:
    352
    Robert Klemme
    Oct 25, 2007
Loading...

Share This Page