Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

List is not displaying more no of records > ihave read data from 2 excel files a

ID: 3638387 • Letter: L

Question

List is not displaying more no of records > ihave read data from 2 excel files and when i run the follwing program it is showing only 1 row when there are 50 rows.

Scanner s= new Scanner(fil); // reading from file
String array1[]=new String[50];
s.next(); s.next(); s.next(); s.next();
int i=0;
String[] values= new String[55];
while(s.hasNext())
{
String field=s.next();
values=field.split(",");
}
List<String> list1=new ArrayList<String>();

for(String x:values)
{
list1.add(x);
}
System.out.println(list1);

Explanation / Answer

There is a proper reason for that: You iterate the rows with the scanner all right, but then you replacethe values array in each iteration. Thus you will get only the last row in values eventually. You might do it like that: List values = new ArrayList(); while(s.hasNext()) { String field=s.next(); for (String value : field.split(",")) { values.add(value); } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote