Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2003-11-14
4
Medium Priority
?
1,888 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

618 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