Results 1 to 10 of 10
  1. #1
    wizard_oz is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Exclamation convert number to string

    Hi, I would like to insert by the user some numbers and send those numbers to a function that would check the numbers.
    my code is like that:
    Java Code:
    chooseNum = scan.next(); // the user type a numbers
    String [] chooseNun1 = chooseNum.split("");
    for (int j = 0; j < chooseNun1.length; j++)
        { 		
    	System.out.println("choose:" + chooseNun1[j]);
        }
    but for some reseaon the first character is empry
    Can anyone help me with that?

    Thanks,Elen

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

    Default

    Use the length and charAt methods of String.

    Edit: If what you really wanted was to split the string on "nothing", as it looks like in that code. If that code actually contains a space, rather than an empty String, than remember that if the String starts with a space (i.e. the separator) then the first element of the String array will be an empty String, which is also the case if you really did attempt to split on the empty String. This is because the String starts with the separator character, but the separator character should only come after the "field, so the first field must be an empty String.
    Last edited by masijade; 11-16-2009 at 06:18 PM.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    You are splitting a String on an empty String; formally every String starts with an empty String hence the empty Strings in the result.

    kind regards,

    Jos

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm not sure I'd use split("") here. Why not convert the String to a char array:
    Java Code:
        char[] chars = chooseNum.toCharArray();
        for (char c : chars) {
          System.out.println("char: " + c);
        }
    edit: I seem to be a bit slow on the uptake today!

  5. #5
    wizard_oz is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default

    that is my problem, my string doesn't contain any empty character but still when I'm printing the result it gives me the first character empty.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by wizard_oz View Post
    that is my problem, my string doesn't contain any empty character but still when I'm printing the result it gives me the first character empty.
    Then again don't use split. You've been given several good options above. Why not try and use one of them?

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

    Default

    Quote Originally Posted by wizard_oz View Post
    that is my problem, my string doesn't contain any empty character but still when I'm printing the result it gives me the first character empty.
    There are no empty characters; there are empty Strings i.e. Strings that don't contain any characters at all but there are no empty characters. A character may not be printable using a certain Font but that is just a matter of display; remember: there are no empty characters, never.

    kind regards,

    Jos

  8. #8
    wizard_oz is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default

    thanks I'll try it

  9. #9
    wizard_oz is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Default

    ok now I would like to send some parameters to a function that will check the numbers
    char[] chars = chooseNum.toCharArray();
    for (char c : chars)
    {
    // System.out.println("char: " + c);
    p.check(findArr[i], i, c);
    i++;
    }
    when i run it I get an error messeage like:
    "Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method check(int, int, String) in the type Mmn11q2 is not applicable for the arguments (int, int, char)

    "
    Last edited by wizard_oz; 11-16-2009 at 07:01 PM.

  10. #10
    wizard_oz is offline Member
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

Similar Threads

  1. how to convert String number to int
    By gabriel in forum New To Java
    Replies: 5
    Last Post: 08-02-2009, 03:46 PM
  2. Convert bar code to number
    By samatha_kudi in forum Advanced Java
    Replies: 1
    Last Post: 07-17-2009, 02:56 PM
  3. Replies: 0
    Last Post: 04-06-2008, 07:40 PM
  4. Replies: 0
    Last Post: 03-01-2008, 10:01 PM
  5. Help with convert a double type number
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:48 AM

Posting Permissions

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