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
Solved

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

Posted on 2003-11-14
4
1,865 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
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
arguments to jar 5 33
Java Restore security prompts not working 10 56
Crystal Reports Licensing Questions 4 34
Selenium docs api java index 3 48
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

839 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