Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to add new optional parameter to JSP 1 29
oracle 11g 23 51
tomcat not starting 6 32
Configure a Bean in an XML file 4 14
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…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

863 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

25 Experts available now in Live!

Get 1:1 Help Now