Hey guys, I'm quite new to Java and I usually try to get things done on my own, but it seems like I'm just sooo lost now.
Would really appreciate any help you guys can give me
So here's the problem:

I need my program to read a text file called "osaka.txt" which basically has a list of 7 Heats, in each heat there are 8 athletes and every Athlete has information such as Lane, Number, Name, Nationality and Time. It is all set out neatly in rows and columns.
So basically what i have tried to do is to use 2 different reading methods, and find out which is the best method to read the file.
The first method is :

import java.io.*;

class Heat 
{
	public static void main(String args[])
	{
	try
		{

			FileInputStream fstream = new FileInputStream("osaka.txt");
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;

			while ((strLine = br.readLine()) != null)   
			{

				System.out.println (strLine);
			}

			in.close();
		}
		catch (Exception exc)
		{
			System.err.println("Error: " + exc.getMessage());
		}
	}
}

and the 2nd method is:

import java.io.*;

public class Heat2 {

	public static void main(String[] args) 
	{
		try 
		{
			File file = new File("osaka.txt");
			FileInputStream fis = new FileInputStream(file);
			BufferedInputStream bis = new BufferedInputStream(fis);
			DataInputStream dis = new DataInputStream(bis);

			while (dis.available() != 0) 
			{

				System.out.println(dis.readLine());
			}

			fis.close();
			bis.close();
			dis.close();

		} 
		catch (FileNotFoundException exc) 
		{
			System.err.println("Error: " + exc.getMessage());		
		} 
		catch (IOException exc) 
		{
			System.err.println("Error: " + exc.getMessage());
		}
	}
}

So now where I am stuck at is, how to create a class which contains the information about 8 athletes in one heat, meaning I would need to create an array for the athletes.
What i think then is to have a method which would read the data file for one heat by itself, and from there pick out the 3 fastest runners.
This is because later on I will need to pick out the 3 fastest 3 runners from each Heat, and the 3 "2nd fastest" runners and put them into the next round.
I think I am Ok with sorting the arrays of the athletes in terms of their running time, but I need some help in creating arrays for the athletes in every heat. Been trying to do it for more than a week now and still stuck, maybe because I am quite new to Java and am looking at it the wrong way.
Lastly, I will need to list the 24 athletes who make it to the next round, but I hope I can do that once I get the arrays done.

Not sure if I'm allowed to upload the text file so you guys can have a look at it or not, but here it is: http://www.mediafire.com/file/dx3qyww0wqy/osaka.txt
Sorry for violating any rules if I am.

Thanks for any help, would really be grateful for it ;-)

I prefer the first method with the BufferedReader, with a small change:

public void readMethod() throws Exception {
BufferedReader br = null;
	try {
		br = new BufferedReader(new Filereader("osaka.txt"));
			String strLine;

			while ((strLine = br.readLine()) != null)   
			{

				System.out.println (strLine);
			}
		}
		catch (Exception exc)
		{
                        throw exc;
		} finally {
                       if (br!=null) br.close();
               }
	}

And call it like this:

try {
 readMethod();
} catch (Exception e) {
 System.out.println("Error: "+e.getMessage());
}

I would suggest, the method to return a Collection of objects that the file contains:

class Athlete {
  // Have as attributes these:
  // Lane    num   Name                    Nat   Time
  private int lane;
  private int num;

  public Athlete() {

  }

// add get,set methods
}
class Heat {
  private Athlete [] athletes = new Athlete[8];

  public Heat() {
  }

// add methods for getting and setting the values of the Athlete array
}

You method should return a Vector of Heat. Check the API for the Vector class.
Now in another class do this:

class Main {
public static Vector readMethod() throws Exception {
.....
}

public static void main(String args[]) {
   try {
     Vector heats = readMethod();
      for (int i=0;i<heats.size();i++) {
           Heat ht = (Heat)heats.get(i);

           // now you have each Heat in an object. Each Heat has 8 athletes with their scores. So you can calculate whatever you want
      }
  } catch (Exception e) {
   System.out.println("Error: "+e.getMessage());
  }
}
}

Your only problem now is reading the file and parse it so you will be able to extract the information. Does the format of the file has to be that way?
You can do this when reading:

Heat ht = null;
Vector heats = new Vector();
while () {

line = line.trim();
if (line.equals("")) continue;

if (line.startsWith("Heat")) {
   // add the previous Heat to the Vector and create a new one for the next Heat
   if (ht!=null) heats.add(ht);
   ht = new Heat();
} else if (line.startsWith("Lane")) {
  // do nothing
} else {
  Athlete at = new Athlete();

  // parse the line and add the Athlete to Heat.
  at.setLane(...);
  .... 
  // put the "at" to the "ht" object as one of the athletes
}

}

Edited 6 Years Ago by javaAddict: n/a

Hey, thanks a lot for your help
Unfortunately you are dealing with a total Java newbie, and I am still unable to get my head around the whole concepts of arrays and vectors (didn't learn about Vectors yet).
Also in terms of the classes you posted, the only one that I believe i have done correct so far is this one:

public class Athlete 
{

	private int _lane;
	private int _num;
	private String _name;
	private String _nat;	
	private double _time;

	public Athlete(int lane, int num, String name, String nat, double time)
	{
		_lane=lane;
		_num=num;
		_name=name;
		_nat=nat;
		_time=time;
	}

	public void setName(String name) {
		_name = name;
	}
	public String getName(){
		return _name;
	}
	public void setLane(int lane)
	{
		_lane = lane; 
	}
	public int getLane()
	{
		return _lane;
	}
	public void setNum (int num)
	{
		_num = num;
	}
	public int getNum ()
	{
		return _num;
	}
	public void setNat(String nat) {
		_nat = nat;
	}
	public String getNat(){
		return _nat;
	}
	public void detTime(double time) {
		_time = time;
	}
	public double getTime(){
		return _time;
	}
}

Otherwise I'm totally stuck with the rest.
When i try to use this:

try 
		{
			readMethod();
		} 
		catch (Exception e) 
		{
			System.out.println("Error: "+e.getMessage());
		}

Eclipse always underlines the "readMethod();" and tells me:"The method readMethod() is undefined for the type Control".
Regarding the last piece of code you posted, I am totally confused with it (Sorry). Probably because I am having trouble understanding the whole vector thing and how it relates to an array.
I know what you are doing is to make the program search for any line that starts with "Heat" and hence create a new array for it, but I am unable to do that.
Any help would be appreciated.
Thanks a lot in advance :)

"The method readMethod() is undefined for the type Control".

It is because you haven't defined the readMethod. You can call it whatever you want. But you need to implement it. Inside it you will read the file and return the results.
You should also define a class Heat with an array of Athletes and have that return a Vector of Heats.

This article has been dead for over six months. Start a new discussion instead.