This discussion is archived
10 Replies Latest reply: Jan 30, 2007 9:03 PM by 807599 RSS

reading a text file into an array

807599 Newbie
Currently Being Moderated
hi

I'm trying to load to a text file consisting of fixed length records into a one dimensional array but I do not seem to have the requisite knowledge.Can anyone out there help?

PS:Have already read the text file.
  • 1. Re: reading a text file into an array
    807599 Newbie
    Currently Being Moderated
    Are you having trouble with the array then? Please be specific.

    http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html
  • 2. Re: reading a text file into an array
    807599 Newbie
    Currently Being Moderated
    yes, I'm having trouble with the array.
  • 3. Re: reading a text file into an array
    807599 Newbie
    Currently Being Moderated
    No donkey, he meant be specific with your problem. "There's a problem" doesnt help anyone solve it.

    Plus, at least attempt a search before posting. This question has been asked AND ANSWERED thousands of times.

    Ted.
  • 4. Re: reading a text file into an array
    578472 Newbie
    Currently Being Moderated
    Checkout with the code below
    try {
            BufferedReader in = new BufferedReader(new FileReader("infilename"));
    
            String str;
            String buffer = new String();
            String NLS =  System.getProperty("line.separator");
    
            while ((str = in.readLine()) != null) {
    
                   buffer = buffer+NLS+str;            
            }
    
          char buff[] = buffer.toCharArray(); 
    
            in.close();
    
        } catch (IOException e) {
        }
    Hope it helps...

    REGARDS,
    RaHuL
  • 5. Re: reading a text file into an array
    807599 Newbie
    Currently Being Moderated
    sorry, new to the forum. just done the searches and found answers to the question.
    thanks and sorry again.
    Stanley

    Message was edited by:
    standoh
  • 6. Re: reading a text file into an array
    807599 Newbie
    Currently Being Moderated
    Well done RahulSharna.

    In a quest to get the dukes you wrote the most useless piece of non-working code you could think of!!

    Ted.
  • 7. Re: reading a text file into an array
    578472 Newbie
    Currently Being Moderated
    Well done RahulSharna.

    In a quest to get the dukes you wrote the most
    useless piece of non-working code you could think
    of!!

    Ted.
    yes i did.... as i was bit desperate about....

    Thanks for Correcting me anyway Buddy....
    I realized how quest for something leads to ignorance.... :)

    However,i'd appriciate if U can correct the stated code.
    so that i can learn from my mistakes.

    REGARDS,
    RaHuL
  • 8. Re: reading a text file into an array
    807599 Newbie
    Currently Being Moderated
    Well you could start with printing a stacktrace in your catch block. Empty catch blocks ar very evil.

    Other then that i'd close the inputstream in a finally block so it will always be closed.

    Just 2 random shots for improvement.

    Lima
  • 9. Re: reading a text file into an array
    807599 Newbie
    Currently Being Moderated
    I think I saw the solution in the book "absolute java" by savitch
  • 10. Re: reading a text file into an array
    807599 Newbie
    Currently Being Moderated
    import java.io.*;
    import java.util.Scanner;
    import java.io.BufferedReader;
    class ArrayReading
    {
         public static void main(String[] args) throws IOException 
         {
              int[] myArray = new int[5];
              
              Scanner s = null;
    //          Tester2 test = new Tester2();
              try 
              {
                   s = new Scanner(
                        new BufferedReader (new FileReader("usrnames.txt")));
                        while(s.hasNext())
                        {
                             if(s.hasNextInt())
                             {
                                  for(int i=0; i<myArray.length; i++)
                                  {
                                  
                                  myArray[i] = s.nextInt();
                                  System.out.print(" "+myArray);
                                  }
                             }
                        }
              }
              finally
              { s.close();
              }
                   }
    }


    Message was edited by:
    fastmike