Solved

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

Posted on 2003-11-14
4
1,837 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
  • 3
4 Comments
 
LVL 92

Accepted Solution

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

Expert Comment

by:objects
Comment Utility
to print it out should also be:

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

0
 

Author Comment

by:vnhenry
Comment Utility
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
Comment Utility
Ask CS and they'll delete it for you
http://www.experts-exchange.com/Community_Support/
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now