Results 1 to 14 of 14
  1. #1
    Ashwin94 is offline Member
    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0

    Lightbulb ERROR: while convertin char array to int

    output:
    Input a string
    ash
    Error:
    Exception in thread "main" java.lang.NumberFormatException: For input string: "
    "
    at java.lang.NumberFormatException.forInputString(Num berFormatException
    java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at test3.main(test3.java:18)

    program:

    import java.util.*;
    class test3
    {
    public static void main(String ar[])
    {
    char a[]=new char[10];
    String str;
    Scanner in = new Scanner(System.in);
    System.out.println("Input a string");
    str = in.nextLine();
    int length = str.length();
    int i;
    for(i=0;i<=length-1;i++)
    {
    a[i]= str.charAt(i) ;
    }
    int y;
    y = Integer.parseInt(String.valueOf(a[1]));
    System.out.println(y);
    }
    }



    please solve this.......

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default Re: ERROR: while convertin char array to int

    First why use "<= length - 1" when you can simply use "< length"?

    And, what is happening is that "a[1]" (i.e. the second element of the array "a") is, seemingly, a return character. What is the value you actually want? The decimal character value of the character in that position (i.e. 65 for "A") or the numeric value represented by the character value of the character (i.e. 6 for the character '6')?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: ERROR: while convertin char array to int

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: ERROR: while convertin char array to int

    Quite.
    The user has input "ash"...so why are you attempting to turn that into any form of int?
    Please do not ask for code as refusal often offends.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,008
    Blog Entries
    7
    Rep Power
    20

    Default Re: ERROR: while convertin char array to int

    I'm very curious what the OP had in mind when he wrote that code ... to me it doesn't make any sense at all ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Ashwin94 is offline Member
    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0

    Default Re: ERROR: while convertin char array to int

    In a[1]... s will be stored ...i want the ascii value of that.... to be stored in integer....................


    i want a prog.......... To get a string...and store each letter from string in different integer variable.by ascii value.........plz help me
    Last edited by Ashwin94; 08-09-2013 at 02:54 PM.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default Re: ERROR: while convertin char array to int

    Quote Originally Posted by Ashwin94 View Post
    i want the ascii value of that
    so simply cast it to an int.
    i.e. y = (int) a[1]

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: ERROR: while convertin char array to int

    Ashwin94, ask your questions in the forum, not by PM. I've deleted the message you sent me.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    Ashwin94 is offline Member
    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0

    Default Re: ERROR: while convertin char array to int

    not comin.......:(

  10. #10
    Ashwin94 is offline Member
    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0

    Default Re: ERROR: while convertin char array to int

    masijade

    kk its workin...

  11. #11
    Ashwin94 is offline Member
    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0

    Default Re: ERROR: while convertin char array to int

    but i want to store in differnt in diffrent variable....

    for example if i give string in 20 letters then we want to give for ever letter???

  12. #12
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default Re: ERROR: while convertin char array to int

    Okay? So use ANOTHER array and cast each element, not just the second one.

  13. #13
    Ashwin94 is offline Member
    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0

    Default Re: ERROR: while convertin char array to int

    Quote Originally Posted by masijade View Post
    Okay? So use ANOTHER array and cast each element, not just the second one.
    tel some example....

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: ERROR: while convertin char array to int

    Quote Originally Posted by Ashwin94 View Post
    tel some example....
    that's the same as "write the code for me".

    Its quite simple: you want to turn every character in the String into an integer, so you need an array of integers. You already know how to create an array of characters, so the change should be simple for you.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 11:32 AM
  2. Replies: 2
    Last Post: 03-08-2011, 06:20 AM
  3. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 09:01 PM
  4. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 11:41 AM
  5. Convertin a .DAT file into readable format
    By janakiram.attuluri in forum Advanced Java
    Replies: 1
    Last Post: 12-07-2007, 10:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •