Results 1 to 11 of 11
  1. #1
    louboulos is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

    Exclamation Java Copy String to Array Urgent!

    Hello everyone!

    I'm new to java obviously.. :p
    I have a question that troubles me 2 days.. :mad:

    I have a String s = "Hello".

    and an array: String c[] = new String[5];

    how can i copy the "H" from String s.charAt(0) to the array c in position c[0] etc etc etc.... ? :D


    Thanks in advance masterminds!!! :D :D

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    354
    Rep Power
    5

    Default

    unless you know for sure that your string(in this case 'Hello') is going to be 5 characters long, you shouldnt hard code the size of the string array. Here is what you can do.

    Java Code:
    String x  =  "hello"
    
    int size = x.length();
    
    String[] charArray = new String[size];
    
    for (int i=0; i<size; i++){
    String[i] = x.CharAt(i);
    }

  3. #3
    louboulos is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

    Default

    Thx for your reply mate! :D


    I did the following but i have a red line under "x.charAt(i);"


    Java Code:
    import java.util.*;
    
    public class Class1 {
        public static void main(String[] args) {
            
        Scanner keyb = new Scanner( System.in );
        
            String x  =  keyb.next();
    
            int size = x.length();
    
            String[] charArray = new String[size];
    
            for (int i=0; i<size; i++){
                
            charArray[i] = x.charAt(i);
                
            }

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

    Default

    The error message is telling you exactly what's wrong. You call your variable charArray, but it isn't in fact a charArray, but rather a String array. charAt(...) returns a char not a String. So change the array type.

    One other thing, please don't mark your threads as urgent as it implies that you feel your question is more important than any other question in the forum, and many will refuse to answer such questions. I almost didn't.

  5. #5
    louboulos is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

    Default

    you have a point there... but I didn't mean that it was more important than others! It is urgent for me only.. :)

    Anyway I'm sorry and thanks for your help!

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

    Default

    You're welcome. Best of luck.

  7. #7
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    354
    Rep Power
    5

    Default

    so should they cast the character that .charAt(i) returns as a string?
    Last edited by sehudson; 03-13-2011 at 09:07 PM.

  8. #8
    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 sehudson View Post
    so should they cast the character that .charAt(i) returns as a string?
    No, you can't cast a primitive char as a String, and while you could change it to a String via the String.valueOf(...) method you certainly don't want to do this. Again, the simplest and correct thing to do is to change the array type of charArray from String[] to char[].

  9. #9
    louboulos is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

    Default

    That doesn't help.... I don't know what is wrong but although the red line is gone, the mistake is the same: "Unexpected Type"

    Java Code:
    import java.util.*;
    
    
    
    public class Class1 {
        public static void main(String[] args) {
            
        Scanner keyb = new Scanner( System.in );
        
            String x  =  keyb.next();
    
            int size = x.length();
    
            char[] z = new char[size];
    
            System.out.println(x.length());
            System.out.println(z.length);
            
              for (int i=0; i < size; i++){
                  
               x.charAt(i) = z[i];
                
            }
              }
        }

  10. #10
    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 louboulos View Post
    That doesn't help....
    How doesn't it help? It fixed your original problem, but of course it won't stop you from creating a new problem, which is what has happened here.


    Java Code:
    x.charAt(i) = z[i];

    Isn't this a little backwards? Think logically what should be happening here.
    Last edited by Fubarable; 03-13-2011 at 10:09 PM.

  11. #11
    louboulos is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

Similar Threads

  1. Need help copy array into new array
    By Get_tanked in forum New To Java
    Replies: 2
    Last Post: 02-07-2011, 03:45 AM
  2. Replies: 16
    Last Post: 07-08-2010, 09:13 PM
  3. Replies: 1
    Last Post: 05-20-2010, 10:17 PM
  4. Copy string to string.
    By limp in forum New To Java
    Replies: 4
    Last Post: 03-17-2009, 06:06 PM
  5. Problem with array Copy
    By coco in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:46 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
  •