Results 1 to 2 of 2
Thread: adding string to a string array
- 08-31-2012, 02:13 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 1
- Rep Power
- 0
adding string to a string array
Hi, I am tring to add strings from reading a file into a string array:
the returned error is :Java Code:import java.io.*; public class index { public static void main(String[] args) throws IOException { File file = new File("index.txt"); FileReader fr = new FileReader(file); int i = 0; String tstring = ""; String[] links; while ((i=fr.read()) != -1) tstring += (char)i; String astrings[] = tstring.split("\n"); for (String astring:astrings) { if (astring.length() > 0) { links[] = astring;//Type mismatch: cannot convert from String to String[] } } //FileWriter fw = new FileWriter(file); //fw.write(); //fw.close(); } }
Type mismatch: cannot convert from String to String[]
Thanks,
Ted
- 08-31-2012, 02:40 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: adding string to a string array
Is that your actual code?
Just asking because:
is just plain wrong and will generate a completely different compilation error.Java Code:links[] = astring;
Assuming that '[]' is a typo here then the answer is that you are trying to assing a String (astring) to a String[] (links).
You need to tell it which element in the array you are assigning astring to.
Since links is not initialised to anything then this will still not compile, so you need to give links a size.Please do not ask for code as refusal often offends.
Similar Threads
-
Code review on adding sentences to String array
By nn12 in forum New To JavaReplies: 6Last Post: 03-21-2011, 02:27 PM -
Intaking String and splitting into chars, returning individual chars as string array
By Gokul138 in forum New To JavaReplies: 1Last Post: 02-07-2011, 08:22 PM -
Changing a String array into a String?
By BennyJass in forum New To JavaReplies: 6Last Post: 01-16-2011, 02:42 PM -
adding in array String
By Mekonom in forum New To JavaReplies: 4Last Post: 12-10-2009, 04:28 PM -
I can't seem to pass the value of a string variable into a string array
By mathias in forum Java AppletsReplies: 1Last Post: 08-03-2007, 10:52 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks