Solved

Read a text file, store it in an array and print out the array.

Posted on 2003-11-14
4
1,876 Views
Last Modified: 2010-03-31
Hi all.
I have this code which reads a text file then store the contents of it in an array and print out the array. I have this error message :"java.util.NosuchElementException" Please tell me what wrong.
public class ReadFile
{
      public static void main(String [] args)throws IOException
      {      
            String line, items, infile = "myFile.txt";
            String [] OutArray;      
            OutArray = new String[50];
            StringTokenizer tknz;
            int index = 0;
            BufferedReader stdin = new BufferedReader(new FileReader (infile));
          line = stdin.readLine();
            while (line != null)
            {
               tknz = new StringTokenizer (line);
                                 items = tknz.nextToken();

           while(tknz.nextToken() != null)
           //for(int i=0; i<line.length(); i++)
               {              
                  OutArray[index]= items;
                  index= index + 1;
                 items = tknz.nextToken();

             }
           line = stdin.readLine();  

            }
              for (int j=0; j<=index; j++)
              {
                    System.out.print(OutArray[j] + " ");
              }      
      }
}      Thank for any help.                
0
Comment
Question by:vnhenry
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 9752851
tknz = new StringTokenizer (line);
while(tknz.hasMoreToken())
{            
        OutArray[index]= tknz.nextToken();
         index= index + 1;
}
0
 
LVL 92

Expert Comment

by:objects
ID: 9752866
to print it out should also be:

for (int j=0; j<index; j++)
{
    System.out.print(OutArray[j] + " ");
}    

0
 

Author Comment

by:vnhenry
ID: 9752904
Thank for you help objects.
Sorry about the duplicate, I posted the question and  waited about 5 munites , I did not see it in my questions list, I thought it wasn't work so I posted it again.
To whom I can ask for delete  a duplicate question, may I ask?
Thank for you help
0
 
LVL 92

Expert Comment

by:objects
ID: 9752935
Ask CS and they'll delete it for you
http://www.experts-exchange.com/Community_Support/
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question