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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using BufferedReader

Status
Not open for further replies.

CaptRage61

Programmer
Feb 17, 2005
50
US
How do you use Buffered reader to read from a file. I have a class set up with a few variables. And I have a file where every line would be a new instance of this class and there are 5 things in each line seperated by tabs. Each thing on each line would be stored in the different variables. How do I do this;

Ex:
class file
variable a, b, c, d, e;

data file:
part1 part2 part3 part4 part5

need to set a=part1; b=part2 ......
 
To parse the line from your data file, check out StringTokenizer class in java.util.* . Set \t as the delimiter. (You might want to set " " (white space) as a delimiter too.)

Make a buffered reader for each file.

BufferedReader fileIn = new BufferedReader(new FileReader("File_Name"));

Here is some more example code:

-Bones
 
I got it set up where it I can grab each peice of data from each line. How do I then take that and create a new Element object ( I already have the class created )
Here is my Element Class:
Code:
public class Element {
    String name;
    String symbol;
    int number;
    double weight, bp;


    public Element() {
    }
    public Element(String n, String s, int num, double w, double b){
        name = n;
        symbol = s;
        number = num;
        weight = w;
        bp = b;
    }
    public String getName(){
        return name;
    }
    public String getSymbol(){
        return symbol;
    }
    public int getNumber(){
        return number;
    }
    public double getWeight(){
        return weight;
    }
    public double getBP(){
        return bp;
    }
    
}

here is where I am reading from the file, the comments are where I am stuck

Code:
public void readFile(File inFile) {
        try {
            StringTokenizer tokenizedString;
            BufferedReader inStream = null;
            int lineNum = 0;
            int tokenCount = 0;
            int elementNumber = 0;
            Object[] element = new String[5];
            String inputLine = null;
            String tokenDelimiters = " \t ";
            inStream = new BufferedReader(new FileReader(inFile));
            do {
                inputLine = inStream.readLine();
                if (inputLine != null && inputLine.length() > 0) {
                    lineNum++;
                    tokenizedString = new StringTokenizer(inputLine,
                            tokenDelimiters);
                    String token = null;
                    while(tokenizedString.hasMoreTokens())   {
                        token = tokenizedString.nextToken();
               //Create a new element
                if(token != null)  {
                   //add each token to the element
                  tokenCount++;
              }
              //add each element to the table
            }

                }
            } while (inputLine != null);

        } catch (IOException e) {
        }
    }

Anyone know how to get this to work?
 
tokenizedString = new StringTokenizer(inputLine,
tokenDelimiters);
while(tokenizedString.hasMoreElement()){
String n =tokenizedString.nextToken();
String s =tokenizedString.nextToken();
int num =Integer.parseInt(tokenizedString.nextToken());
double w =Double.parseDouble(tokenizedString.nextToken());
double b =Double.parseDouble(tokenizedString.nextToken());

Element e=new Element(n,s,num,w,b);
// and do whatever you want to your element.
}
 
Thanks that worked, I did figure out how to do it but my way was really messy.

Now say that there is X lines that I need to read from the file and I need to create X Elements. How would I write a loop that would create all of these? What I was thinking was haveing a counter and name each Element e1, e2, e3, eX. How do I name this? I tried:
Code:
int counter = 1;
Element e + counter = new element(n,s,num,w,b);
counter++;
But this don't work it does not like the e+ counter
 
You can't define variable names at runtime.

I'd store it in an ArrayList.

For each new Element you create, do an ArrayList.add().

Cheers,

Dian
 
would I do:

Element e1=new Element(n,s,nu,,w,b);
ArrayList al = new ArrayList();
al.add(counter,e1);
counter++;

Would this work?
 
That did work and I have the ArrayList created and it contains all of my Elements.

I need to create a loop to get these into an Object array.

how do I get the array list to return my Element so I can call the getName and other methods.

Here is what I tried:

int x = al.size();
for(int y=0; y<= x; y++){
Element w = new Element();
w = al.get(y); <================ Incompatable types found
}

The goal is to get and Object array creates with all the data.
Ex:
Object [][] data = { { e1.getName(), e1.getSymbol().....}
{e2.getName(), e2.getSymbol()..............}
{all other elements......} }

I need this object array so I can pass it into a JTable.
 
Well, you can just add the Elements to the ArrayList without the counter, ArryList will do it for you.

And to recover them in an array try

Code:
Element[] runtimeType = new Element[];
Element[] myElements = myArrayList.toArray(runtimeType);

Take a look at ArrayList API if you don't understand it.

Cheers,

Dian

 
That is not exactly what I want. I already have all of my Elements in an ArrayList, now what I need to do is place all of the information from each and all elements into and 2D Object array : Object[][] so I can pass that array as the data into a JTable.
 
Then iterate through the ArrayList, and fill the new array with the values got from the Elements.

Cheers,

Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top