Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read a file

Status
Not open for further replies.

mgl70

Programmer
Joined
Sep 10, 2003
Messages
105
Location
US
Hi,
I am learning java.I have a question on this. I have a textfile with 1 to 100 numbers.
I need to read this file upto 1 to 50 numbers and store it in in an 1D array. The code is

File f = new File(MyFile.txt);
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
while()
{
//here I need to read the file . put it in an array
}

can you help me this.
thanks,
 
Code:
import java.io.*;
class ReadFile
      {
       public static void main(String args[])
              {
               int count=1;
               String myLine[] = new String[101];
               float myFloat[] = new float[101];
               File f = new File("MyFile.txt");
               try 
                {
               FileReader fr = new FileReader(f);
               BufferedReader br = new BufferedReader(fr);
               myLine[count] = br.readLine(); //myLine[0] will not be used in my code
               while(count<=50)
                    {
                     count++;
                     myLine[count] = br.readLine();
                     if (myLine[count]==null)
                        break;
                    }
               br.close();
               fr.close();

                }
               catch (Exception e)
                {
                }
               for (int i=1;i<=100;i++)
                   {
                    if (myLine[i]!=null)
                    myFloat[i] = Float.parseFloat(myLine[i]);
                   }
              }
      }
 
thank you for your reply. When I was testing with this code
I am having the error here.

if(myLine!= null)
myFloat = Float.parseFloat(MyLine);

the error is incompatible types found float. required java.lang.Float.

thanks,

 
You should have this line if myFloat is not an array look like in my code.
float myFloat;
or
float myFloat = 0.0f;
 
Thank you.But I need to store these numbers in an array.
The Float should be array.

thanks,
 
Float.parseFloat returns a float (primative) not a Float (object)... Your allright if you want to save them to an array, just not a first class container type. Perhaps you want Float, so that you can add them to an ArrayList? Then try using ValueOf(String).
 
I am sorry . I couldnot do that. can you write me the code.
I need to store it in an array those numbers, but not in array list (what you said adding a primitive to arraylist. I donot know How to do that. In the arrayList I saw add() are there. All add() are taking object arguments or some other types but not primitives.)

and after that I need to sort those numbers by using selection sort. For that I need to send an array to selection sort()and note the time. how long it will take to sort those numbers.

selection sort and time I am able to do that. I created an array manually and send to the selectionsort().
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top