psoares
psoares

Reputation: 4883

two dimensional array in java - difficulties

I'm used to python and django but I've recently started learning java. Since I don't have much time because of work I missed a lot of classes and I'm a bit confused now that I have to do a work.

EDIT
The program is suppose to attribute points according to the time each athlete made in bike and race. I have 4 extra tables for male and female with points and times.
I have to compare then and find the corresponding points for each time (linear interpolation).

So this was my idea to read the file, and use an arrayList

One of the things I'm having difficulties is creating a two dimensional array.
I have a file similar to this one:

12    M    23:56    62:50
36    F    59:30    20:60

Where the first number is an athlete, the second the gender and next time of different races (which needs to be converted into seconds).

Since I can't make an array mixed (int and char), I have to convert the gender to 0 and 1.

so where is what I've done so far:

    public static void main(String[] args) throws FileNotFoundException {
        Scanner fileTime = new Scanner (new FileReader ("time.txt"));
        while (fileTime.hasNext()) {
            String value = fileTime.next();
            // Modify gender by o and 1, this way I'm able to convert string into integer
            if (value.equals("F"))
                value = "0";
            else if (value.equals("M"))
                value = "1";
            // Verify which values has :
            int index = valor.indexOf(":");
            if (index != -1) {
                String [] temp = value.split(":");
                for (int i=0; i<temp.length; i++) {
                    // convert string to int
                    int num = Integer.parseInt(temp[i]);
                    // I wanted to multiply the first number by 60 to convert into seconds and add the second number to the first
                   num * 60; // but this way I multiplying everything
            }
        }
        }   

I'm aware that there's probably easier ways to do this but honestly I'm a bit confused, any lights are welcome.

Upvotes: 0

Views: 322

Answers (3)

yshavit
yshavit

Reputation: 43436

Python is a dynamically-typed language, which means you can think of each row as a tuple, or even as a list/array if you like. The Java idiom is to be stricter in typing. So, rather than having a list of list of elements, your Java program should define a class that represents a the information in each line, and then instantiate and populate objects of that class. In other words, if you want to program in idiomatic Java, this is not a two-dimensional array problem; it's a List<MyClass> problem.

Upvotes: 1

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

Try reading the file line by line:

while (fileTime.hasNext())

Instead of hasNext use hasNextLine.

Read the next line instead of next token:

String value = fileTime.next();
// can be
String line = fileTime.nextLine();

Split the line into four parts with something as follows:

String[] parts = line.split("\\s+");

Access the parts using parts[0], parts[1], parts[2] and parts[3]. And you already know what's in what. Easily process them.

Upvotes: 0

unholysampler
unholysampler

Reputation: 17341

Just because an array works well to store the data in one language does not mean it is the best way to store the data in another language.

Instead of trying to make a two dimensional array, you can make a single array (or collection) of a custom class.

public class Athlete {
  private int _id;
  private boolean _isMale;
  private int[] _times;
  //...
}

How you intend to use the data may change the way you structure the class. But this is a simple direct representation of the data line you described.

Upvotes: 5

Related Questions