Note that there are some explanatory texts on larger screens.

plurals
  1. POMy ArrayList only contains the last item on my array
    text
    copied!<pre><code>public static void main(String args[]){ new Converter(); //the converter() method reads a csv file that pass it to an array of String // called stringList; where in stringList is a static variable; ArrayList&lt;Student&gt; list = new ArrayList&lt;Student&gt;(5); String p[] = null; Student stud = null; for(int z = 0; z &lt; stringList.length; z++){ p = stringList[z].split(","); id = Integer.parseInt(p[0]); yr = Integer.parseInt(p[5]); fname = p[1]; gname = p[2]; mname = p[3].charAt(0); course = p[4]; stud = new Student(id,yr,fname,gname,mname,course); studs = stud; </code></pre> <p>I tried to display the current values of the variables above and compare them to the student object</p> <pre><code> System.out.println(id +" "+ yr +" "+ fname + " "+gname + " "+mname + " "+course +" should be the same with : " +stud.toString()); list.add(studs); // add the student object to the list } //end of the for loop </code></pre> <p>Then I noticed my Arraylist only displays only one value:</p> <pre><code> for (int c = 0; c&lt;list.size(); c++){ System.out.println(" @list "+c+": "+list.get(c)); } } // end of main method </code></pre> <p>Normally I would read 100+ items but with this example I made it only 5; this is the out put of the program;</p> <pre><code> 2123259 1 AVILA JEREMY RAYMUND T BSIT should be the same with : 2123259,AVILA,JEREMY RAYMUND,T,BSIT,1 2124919 1 BEROÑA MAYNARD W BSIT should be the same with : 2124919,BEROÑA,MAYNARD,W,BSIT,1 2124679 1 CABRERA JERSON RHOD D BSIT should be the same with : 2124679,CABRERA,JERSON RHOD,D,BSIT,1 2124905 1 CABRILLAS ARMANDO JR. B BSIT should be the same with : 2124905,CABRILLAS,ARMANDO JR.,B,BSIT,1 2123400 1 CARIÑO JVANZ S BSIT should be the same with : 2123400,CARIÑO,JVANZ,S,BSIT,1 @list 0: 2123400,CARIÑO,JVANZ,S,BSIT,1 @list 1: 2123400,CARIÑO,JVANZ,S,BSIT,1 @list 2: 2123400,CARIÑO,JVANZ,S,BSIT,1 @list 3: 2123400,CARIÑO,JVANZ,S,BSIT,1 @list 4: 2123400,CARIÑO,JVANZ,S,BSIT,1 </code></pre> <p>Now my problem here ladies and gentlemen is that I tried to display the Stud object inorder to check if its reading the correct values and indeed it displays it correctly, and then after assigning values to it , I then add it to the list. but something confuses me , and I couldnt figure out why my list only contains the last item in my array? please help me, I've been stuck for 2 nights.</p> <p>this my whole code , I focus on the main</p> <p>public class Converter {</p> <pre><code> private static ArrayList&lt;Student&gt; students; private static String[] stringList; private static Scanner fr; private static int size; private static int id; private static int yr; private static String fname; private static String gname; private static char mname; private static String course; public static void main(String args[]){ ArrayList&lt;Student&gt; list = new ArrayList&lt;Student&gt;(5); new Converter(); String p[] = null; Student stud = null; students = new ArrayList&lt;Student&gt;(stringList.length); for(int z = 0; z &lt; stringList.length; z++){ p = stringList[z].split(","); id = Integer.parseInt(p[0]); yr = Integer.parseInt(p[5]); fname = p[1]; gname = p[2]; mname = p[3].charAt(0); course = p[4]; stud = new Student(id,yr,fname,gname,mname,course); System.out.println(id +" "+ yr +" "+ fname + " "+gname + " "+mname + " "+course +" should be the same with : " +stud.toString()); list.add(stud); } // end of for loop for (int c = 0; c&lt;list.size(); c++){ System.out.println(" @list "+c+": "+list.get(c)); } // end of second loop }// end of main public Converter(){ readStudtentsCSV(); id = 0; yr = 0; fname = ""; gname = ""; mname = ' '; course = ""; } public static void readStudtentsCSV() { int s = 0; try { size = countLines("test.csv"); stringList = new String[size]; fr = new Scanner(new File("test.csv")); } catch(Exception e){ e.printStackTrace(); } while (fr.hasNextLine()){ stringList[s] = fr.nextLine(); //System.out.println(stringList[s]); s++; } } public static int countLines(String eFile) throws Exception{ Scanner fScan = new Scanner(new File(eFile)); int count =0; while (fScan.hasNextLine()){ count += 1; String line = fScan.nextLine(); } fScan.close(); return count; } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload