Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

It is necessary that I create a String[] with 5 strings, each of these strings carries multiple substrings with data separated by commas, I must convert this array of int into ArrayList to section the sub strings in this order: the numbers '1, 2, 3, 4, 5' are student ID numbers. 'john, suzan, jack, erin, adan' are first names, etc.. However, the only issue I ran into is when I'm trying to print the array of grades, ex: '88, 79, 59' into an ArrayList object that prints: 887959(1 arraylist object), NOT 88 (1 arraylist object), 79(another arraylist object),...

What am I doing wrong? How do I fix this?

Code:

String[] kaizer = 
        {

            "1,John,Smith,[email protected],20,88,79,59",
            "2,Suzan,Erickson,Erickson_1990@gmailcom,19,91,72,85",
            "3,Jack,Napoli,The_lawyer99yahoo.com,19,85,84,87",
            "4,Erin,Black,[email protected],22,91,98,82",
            "5,Adan,Ramirez,[email protected],23,100,100,100"
        };
ArrayList<Integer> grades = new ArrayList<Integer>(); 
for (int i = 0; i < kaizer.length; i++) {
    String s = kaizer[i]; //volatile reference
    String[] parts = s.split(","); 
    //volatile reference
    String id = parts[0];
    String name = parts[1];
    String lastname = parts[2];
    String email = parts[3];
    String age = parts[4];
    grades.add(Integer.parseInt(parts[5])); 
    grades.add(Integer.parseInt(parts[6]));
    grades.add(Integer.parseInt(parts[7]));
    //Widget w = new Widget(id, name, num);
    //mywidgets.add(w);
}

This is supposed to print the set of substrings like this:

887959 
917285
858487
919882
100100100

System.out.println(grades.get(0)); 
System.out.println(grades.get(1));
System.out.println(grades.get(2));
System.out.println(grades.get(3));
System.out.println(grades.get(4));

Instead... it prints:

8879599172

The problem lies in the output: '8879599172'

share|improve this question
    
Since you're working with Java, I think you should try to solve this problem using OOP. – Luiggi Mendoza Aug 31 '15 at 14:28
    
You need 1 grades array per for loop. Maybe a hashmap<int, ArrayList<Integer>>, where the key is id and value the grades for this id. – Danilo Silva Aug 31 '15 at 14:29
1  
@DaniloSilva or just use a List<Student> where the Student data is obtained after parsing each String. – Luiggi Mendoza Aug 31 '15 at 14:30
    
@LuiggiMendoza - Unfortunately OP might not know what it means. Many courses teach procedural programming in Java, sometimes using main() as the only method. – PM 77-1 Aug 31 '15 at 14:31
    
@LuiggiMendoza Yes, I think this is the better solution – Danilo Silva Aug 31 '15 at 14:31

grades is an ArrayList and as far as it's concerned you are just adding Integers to it. It doesn't know when it's supposed to break. Index 3 is just the Integer after index 2. If you want to combine each one into combined values you can do grades.add(Integer.parseInt(parts[5]+parts[6]+parts[7])) or you can insert a line break after each 3 values added to grades. You can also make a grades a List with sublists that are each a List of type Integer.

share|improve this answer
    
BINGO! you got it! in just a few minutes you saved me from killing myself, gj! – Adan Ramirez Aug 31 '15 at 14:30

You are storing all the values in a 1-dimensional ArrayList, in your scenario, you should consider using a 2-dimensional ArrayList.

ArrayList<ArrayList<Integer>> grades = new ArrayList<ArrayList<Integer>>(); 
for (int i = 0; i < kaizer.length; i++) {
    String s = kaizer[i]; //volatile reference
    String[] parts = s.split(","); 
    //volatile reference
    String id = parts[0];
    String name = parts[1];
    String lastname = parts[2];
    String email = parts[3];
    String age = parts[4];

    ArrayList<Integer> subList = new ArrayList<Integer>();
    subList.add(Integer.parseInt(parts[5])); 
    subList.add(Integer.parseInt(parts[6]));
    subList.add(Integer.parseInt(parts[7]));
    grades.add(i, subList);

    //Widget w = new Widget(id, name, num);
    //mywidgets.add(w);
}

Then, you can loop through the grades for each student:

//for each student
for(ArrayList<Integer> list : grades)
{
    for(int grade : list)
    {
        //do something with each grade
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.