The Artima Developer Community
Sponsored Link

Java Answers Forum
Why is the variable intializer incomplete???

7 replies on 1 page. Most recent reply: Jul 13, 2002 7:13 PM by Leslie Jo

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
Leslie Jo

Posts: 22
Nickname: leeloo
Registered: Jul, 2002

Why is the variable intializer incomplete??? Posted: Jul 11, 2002 9:34 PM
Reply to this message Reply
Advertisement
/*
* Reads a text file and return the contents of the file
* as an array of strings. Each element in the array contains
* one line from the text file.
*
* "str" is each line in the file
*/
private static readFileToArray (String filename) throws Exception
{
filename = ("a:\\JavadocPart1.txt");
BufferedReader inFile = new BufferedReader
(new FileReader (filename);
String str = null; // set initial
int i = 0; // declare counter variable

// Read text one line at a time and assign each to a string element
while ((str = inFile.readLine ()) != null)
{
i++;
String [] anArray = new String [1000];
anArray = str;
} // end while

return (anArray );
} // end readFileToArray


Leslie Jo

Posts: 22
Nickname: leeloo
Registered: Jul, 2002

Re: Why is the variable intializer incomplete??? Posted: Jul 11, 2002 10:59 PM
Reply to this message Reply
what i cant do is, the method has to be private, and also, ,the parameter must be the filename

Leslie Jo

Posts: 22
Nickname: leeloo
Registered: Jul, 2002

Re: Why is the variable intializer incomplete??? Posted: Jul 12, 2002 10:22 AM
Reply to this message Reply
/*
* Reads a text file into an array of strings.
* Each element in the array contains
* one line from the text file.
*
* parameter filename is the file path
* returns the contents of the file as an array of strings
*
*/
private static String[] readFileToArray (String filename) throws Exception
{
filename = ("a:\\JavadocPart1.txt");
BufferedReader inputStream = new BufferedReader
(new FileReader (filename));
String str = null; // end of file is null
int i = 0; // declare counter variable


// Read text one line at a time and assign each to a string element
while ((str = inputStream.readLine ()) != null)
{
i++;
String [] anArray = new String [1000];
anArray = str;
} // end while

return anArray;
} // end readFileToArray

I dont understand how to get this to return an array containing the lines from the text file.

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Why is the variable intializer incomplete??? Posted: Jul 12, 2002 2:40 PM
Reply to this message Reply
I don't get yr problem :
   /*
  * Reads a text file and return the contents of the file
  * as an array of strings. Each element in the array contains
  * one line from the text file.
  *
  * "str" is each line in the file
  */
  private static readFileToArray (String filename)
     throws Exception  
  {
    filename = ("a:\\JavadocPart1.txt");
    BufferedReader inFile = new BufferedReader (new  FileReader (filename);
    String str = null; // set initial
    int i = 0; // declare counter variable
 
    // Read text one line at a time and assign each to a  string element
    while ((str = inFile.readLine ()) != null)
    {
      i++;                                    // <-- what do U do with this ? 
      String [] anArray = new String [1000];  // <-- This is really pointless !
      anArray = str;
    } // end while
 
    return (anArray );
  } // end readFileToArray 
I would advise you to do this like here under.
See the code below it should be more efficient, doesn't declare unneeded variables & id safe event for files greater than "1000" lines ...
   /*
  * Reads a text file and return the contents of the file
  * as an array of strings. Each element in the array contains
  * one line from the text file.
  *
  * "str" is each line in the file
  */
  private static readFileToArray (String filename)
     throws Exception  
  {
    filename = ("a:\\JavadocPart1.txt");
    BufferedReader inFile = new BufferedReader (new  FileReader (filename);
    String str = null; // set initial
    int i = 0; // declare counter variable
 
    // Read text one line at a time and assign each to a  string element
    ArrayList array = new ArrayList ();
    while ((str = inFile.readLine ()) != null)      
      array.addElement ( str );
    // end while
 
    return (array.toArray ( new String[array.size()]) );
  } // end readFileToArray 


Thomas SMETS,
SCJP2 - Brussels

Leslie Jo

Posts: 22
Nickname: leeloo
Registered: Jul, 2002

Re: Why is the variable intializer incomplete??? Posted: Jul 12, 2002 4:57 PM
Reply to this message Reply
so the line:

return (array.toArray ( new String[array.size()]) );

returns the array?? of size whatever the list is??
how do I put use this array and print it line by line into a new text file? so "array" is the name of the arraylist?

Jay Kandy

Posts: 77
Nickname: jay
Registered: Mar, 2002

Re: Why is the variable intializer incomplete??? Posted: Jul 12, 2002 6:14 PM
Reply to this message Reply
Hey Tom, probably didnt notice, the method signature isnt proper! I guess a class with main is the best bet to teach.

Sincerely,
Jay

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Why is the variable intializer incomplete??? Posted: Jul 13, 2002 3:34 AM
Reply to this message Reply
Yep,
I didn't grasp it at first from the API's & some one had to show me it !
return (array.toArray ( new String[array.size()]) );

This code transforms the ArrayList in an array of Object of the type given as parameter. If the array has the right size, the provided array is used!

This is a quick snipletlet unchecked to show you how it works (typed directly in the TextArea):
// No imports in the code (some needed)
// Assuming that fis is a BuffferedReader
ArrayList strings = new ArrayList();
String readLine;
while ((readLine = fis.readLine ()) != null) 
  strings = readLine;
 
Object[] o = strings.toArray(new String[string.size()]);
 
// As the toArray returns an Object[]
// we need to cast
String[] s = (String[])o;
 
// At this point the Variable "s" 
// is properly initialized...


Hope it allows you to make better code ?



Thomas SMETS,
SCJP2 - Brussels

Leslie Jo

Posts: 22
Nickname: leeloo
Registered: Jul, 2002

Re: Why is the variable intializer incomplete??? Posted: Jul 13, 2002 7:13 PM
Reply to this message Reply
thank you very much thomas smets

Flat View: This topic has 7 replies on 1 page
Topic: IE6 does not allow javascript links to work Previous Topic   Next Topic Topic: data type problemo

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use