muktoshuvro
muktoshuvro

Reputation: 438

Saving values inside while loop

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?

Upvotes: 3

Views: 5400

Answers (5)

wolfrevo
wolfrevo

Reputation: 7303

split returns a String-array which you assign to your array country. You should assign the result of split to a position in country, i.e. country[i]. If you do not use a List instead of your array, then you must dimensionate the array first! I.e. create it with a given size new String[size]

Upvotes: 1

Satyan Raina
Satyan Raina

Reputation: 1020

You can store them in one of the collection types. (see http://docs.oracle.com/javase/7/docs/api/java/util/Collection.html)

Java collections provide you various data structures to store and retrieve data in a particular order. For example, a List will allow you to retrieve the data by index and will preserve the insertion order (FIFO). Other data structures like trees, queues, stacks, sets have other properties.

Using a List in your code:


    InputStreamReader reader = new InputStreamReader(asset_stream);  
    br = new BufferedReader(reader);
    List <String[]> countries=new LinkedList <String[]>();

    try {
        while ((line = br.readLine()) != null) {
            countries.add(line.split(cvsSplitBy));
        }
    } catch (NumberFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    for(String[] country: countries)
    {
      //Do something useful with your List
      System.Out.println(country[0]);
    }

Upvotes: 3

Duncan Jones
Duncan Jones

Reputation: 69399

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
}

Upvotes: 7

Elliott Frisch
Elliott Frisch

Reputation: 201527

Assuming you're reading multiple countries per line, you may be able to use something like this -

public static List<String> getCountryList(InputStream asset_stream) {
    InputStreamReader reader = new InputStreamReader(asset_stream);
    BufferedReader br = new BufferedReader(reader);
    String[] country = null;
    List<String> al = new ArrayList<String>();
    try {
        String line;
        while ((line = br.readLine()) != null) {
            country = line.split(cvsSplitBy);
            for (String s : country) {
                al.add(s);
            }
        }
    } catch (NumberFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        br.close();
    }
    return al;
}

Upvotes: 4

SebastianH
SebastianH

Reputation: 2182

With every loop your are splitting the current line and saving its parts in the country array. Therefore you can only see the results of your last line after the while loop.

If you want to store the splitted results of every line you should use a collection of String[]. For example like this:

LinkedList<String[]> list = new LinkedList<String[]>();
while ((line = br.readLine()) != null) {
    list.add(line.split(cvsSplitBy));
}
//To read values
for (String[] entry : list) {
    //do something with entry
}

Upvotes: 3

Related Questions