Results 1 to 2 of 2
  1. #1
    ted_chou12 is offline Member
    Join Date
    Aug 2012
    Posts
    1
    Rep Power
    0

    Default adding string to a string array

    Hi, I am tring to add strings from reading a file into a string array:
    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();
    	}
    }
    the returned error is :
    Type mismatch: cannot convert from String to String[]
    Thanks,
    Ted

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: adding string to a string array

    Is that your actual code?
    Just asking because:
    Java Code:
    links[] = astring;
    is just plain wrong and will generate a completely different compilation error.

    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.

    ** This space for rent **

Similar Threads

  1. Replies: 6
    Last Post: 03-21-2011, 02:27 PM
  2. Replies: 1
    Last Post: 02-07-2011, 08:22 PM
  3. Changing a String array into a String?
    By BennyJass in forum New To Java
    Replies: 6
    Last Post: 01-16-2011, 02:42 PM
  4. adding in array String
    By Mekonom in forum New To Java
    Replies: 4
    Last Post: 12-10-2009, 04:28 PM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 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
  •