Saving values inside while loop

java, split, while-loop

Solution

Consider using a list to hold the values:

List<String[]> countries = new ArrayList<>();

try {
    while ((line = br.readLine()) != null) {
        countries.add(line.split(cvsSplitBy));    
    }
}

Later you can iterate over this list:

for (String[] country : countries) {
  System.out.println(Arrays.toString(country); // or whatever
}

Problem

I am trying to read values of my country array string which reads csv file. ``` InputStreamReader reader = new InputStreamReader(asset_stream); br = new BufferedReader(reader); String[] country = null; String cvsSplitBy = ";"; try { while ((line = br.readLine()) != null) { country = line.split(cvsSplitBy); } } catch (NumberFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } ``` My code is currently storing the values inside the `country` variable. But when my loop finishes, I only have the last value read in the loop. How can I store all the values so I can print them after finishing the loop?

Original source

Related problems