-2

i wrote a code that takes a line from a txt file, splits it into different strings and integers and then stores it into an array list as an object called professor. The code of the main class its this:

public class Main {

    public static void main(String[] args) throws IOException {
        FileReader file = new FileReader("text.txt");
        BufferedReader reader = new BufferedReader(file);
        ArrayList<Profesor>professors = new ArrayList<Profesor>();
        String line = reader.readLine();
        String[] lineSplit = new String[11];

        while(line != null){
            lineSplit = line.split("\\s+");
            professors.add(new Profesor(lineSplit[0], lineSplit[1], Integer.parseInt(lineSplit[2]), Integer.parseInt(lineSplit[3]), Integer.parseInt(lineSplit[4]), Integer.parseInt(lineSplit[5]), Integer.parseInt(lineSplit[6]), Integer.parseInt(lineSplit[7]), Integer.parseInt(lineSplit[8]), Integer.parseInt(lineSplit[9]), Integer.parseInt(lineSplit[10]), Integer.parseInt(lineSplit[11])));                     
        }

    }

}

And the Profesor class its:

public class Profesor {
    private String name;
    private String subject;
    private int wh0;
    private int wh1;
    private int wh2;
    private int wh3;
    private int wh4;
    private int wh5;
    private int wh6;
    private int wh7;
    private int wh8;
    private int wh9;    
    public Profesor(String n, String s, int w0, int w1, int w2, int w3, int w4, int w5, int w6, int w7, int w8, int w9){
        name = n;
        subject = s;
        wh0 = w0;
        wh1 = w1;
        wh2 = w2;
        wh3 = w3;
        wh4 = w4;
        wh5 = w5;       
        wh6 = w6;
        wh7 = w7;
        wh8 = w8;
        wh9 = w9;               
    }
}

And the txt file its something like:

Jhon Maths 173 486 789 954 684 235 446 168 749 851   
Robert MathsII 283 686 948 978 144 224 473 468 778 845

The question its how can I display the arraylist into the console? and how do I access a string inside one of the objects inside of the arraylist?
Thanks in advance

fabian
  • 80,457
  • 12
  • 86
  • 114

2 Answers2

1

You can use get() method for each ArrayList. So typing professors.get(0) will return the first Professor object.

Not only that once you get that object, you need to create something called getName, or getObject. It's because your variables are private and a class outside of Professor is trying to access the private variable.

It looks something like this:

public String getName() {
    return name;
}

And once you have that method inside Professor class, you can call it within your Main class by calling

Profesor p = professors.get(0);   // returns the first Profesor inside professors ArrayList
String professorName = p.getName();   // returns the name variable of the above professor

More about ArrayList can be found here:

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#get(int)

sparkhee93
  • 1,381
  • 3
  • 21
  • 30
0

Here’s the thing: when you try to print an object, Java figures out what to print by calling the object’s toString() function. So you need to tell Java that by implementing that particular function in your Professor object:

public String toString()
{
    return name + subject + Arrays.toString(wh); 
    // put all those wh numbers into an array!

}

This function returns a String of the form:

Jhon Maths 173 [486, 789, 954, 684, 235, 446, 168, 749, 851] which is what Java prints out.

Now, you can iterate through your ArrayList of Professors and simply System.out.println them one by one.

xrisk
  • 3,790
  • 22
  • 45