Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-22-2007, 12:29 AM
Member
 
Join Date: Nov 2007
Posts: 8
Warren is on a distinguished road
arrays help
I am reading a text file with BufferedReader (FileReader).
I want to put each line of the text (which has always one word per line)
in an array(which size is predefined from the beginning and it is much bigger
than the amount of the words of the .txt file). Whatever i try to solve the problem it always ends with this: 'cannot convert from String to String[]'


so, how can I put these words one by one in the array? help plz

Last edited by Warren : 11-22-2007 at 12:34 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-22-2007, 01:18 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,124
hardwired is on a distinguished road
Code:
import java.io.*; public class ReadWords { public static void main(String[] args) { String[] receiver = new String[100]; int count = 0; String path = "wordsToRead.txt"; try { BufferedReader br = new BufferedReader( new FileReader(path)); String line; while((line = br.readLine()) != null) { receiver[count++] = line; } br.close(); } catch(IOException e) { System.out.println("Read error: " + e.getMessage()); } System.out.println("We read " + count + " lines from " + path); // Print the non-null elements of receiver. for(int j = 0; j < count; j++) { System.out.print(receiver[j]); if(j < count-1) System.out.print(", "); } System.out.println(); } }
wordsToRead.txt
Code:
Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-22-2007, 01:38 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Code:
BufferedReader reader = new BufferedReader(new FileReader("test.txt")); String s; String tmp = ""; while ((s = reader.readLine()) !=null) { tmp += s + "\n"; } String[] arr = tmp.split("\\n");
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-23-2007, 05:27 AM
Member
 
Join Date: Nov 2007
Posts: 8
Warren is on a distinguished road
Why we cant say this? (sorry for my ignorance)

Code:
try { BufferedReader br = new BufferedReader( new FileReader(path)); while(br.readLine() != null) { receiver[count++] = br.readLine() }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-23-2007, 09:48 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,124
hardwired is on a distinguished road
Code:
// The condition in the while loop argument reads // one line but does not save it while(br.readLine() != null) { // The next line is read here and is saved. receiver[count++] = br.readLine() }
So this will read every other line: odd number lines are read but not saved, even number lines are read and saved. Another problem with this is that an exception will be thrown for files which have an even number of lines.
Some like to write the read operation this way:
Code:
String line = br.readLine(); while(line != null) { receiver[count++] = line; line = br.readline(); }
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-23-2007, 07:45 PM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
What if the file is over a 100 lines?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-23-2007, 08:23 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,124
hardwired is on a distinguished road
The easy way is to use a List or StringBuilder, add the lines as you go and convert to an array after the reading operation.
Code:
import java.util.List; import java.util.*; ... List<String list = new ArrayList<String>(); StringBuilder sb = new StringBuilder(); String line; while((line = br.readLine()) != null) { list.add(line); sb.append(line + "\n"); // staykovmarin's suggestion } // convert to an array String[] words = list.toArray(new String[list.size()]); words = sb.toString().split("\n");
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Arrays bunbun New To Java 1 04-09-2008 03:24 AM
new to arrays jimJohnson New To Java 1 04-08-2008 03:45 PM
question about arrays broganm1 New To Java 3 02-13-2008 03:29 AM
2D-Arrays kbyrne New To Java 1 02-07-2008 11:08 PM
Problems with arrays Marcus New To Java 2 07-04-2007 09:10 AM


All times are GMT +3. The time now is 09:19 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org