Results 1 to 11 of 11
- 03-13-2011, 07:22 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 25
- Rep Power
- 0
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
- 03-13-2011, 07:38 PM #2
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); }
- 03-13-2011, 08:22 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 25
- Rep Power
- 0
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); }
-
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.
- 03-13-2011, 08:45 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 25
- Rep Power
- 0
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!
-
You're welcome. Best of luck.
- 03-13-2011, 09:05 PM #7
so should they cast the character that .charAt(i) returns as a string?
Last edited by sehudson; 03-13-2011 at 09:07 PM.
-
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[].
- 03-13-2011, 09:26 PM #9
Member
- Join Date
- Mar 2011
- Posts
- 25
- Rep Power
- 0
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]; } } }
-
Last edited by Fubarable; 03-13-2011 at 10:09 PM.
- 03-13-2011, 10:08 PM #11
Member
- Join Date
- Mar 2011
- Posts
- 25
- Rep Power
- 0
Similar Threads
-
Need help copy array into new array
By Get_tanked in forum New To JavaReplies: 2Last Post: 02-07-2011, 03:45 AM -
ISSUE: Array as class constructor will not copy elements, only reference
By Lucificate in forum New To JavaReplies: 16Last Post: 07-08-2010, 09:13 PM -
Copy a specified element from one array list to another arraylist
By TaxpayersMoney in forum New To JavaReplies: 1Last Post: 05-20-2010, 10:17 PM -
Copy string to string.
By limp in forum New To JavaReplies: 4Last Post: 03-17-2009, 06:06 PM -
Problem with array Copy
By coco in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks