0

I am working with csv file having very large dataset. while reading file i had extracted 3rd place(BALANCE) ';' separated numeric value from each rows with in while loop iteration.i want to store those values(BALANCE) into listOF string.how can i do this.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.*;
public static void main(String[] args)throws IOException {
        String filename = "bank-full.csv";
        File file = new File(filename);
        try {
            Scanner inputStream = new Scanner(file);
            inputStream.next();
            int count=0;
      while (inputStream.hasNext()) 
          {
            String data = inputStream.next();         
            String[] values = data.split(";");
            double BALANCE = Double.parseDouble(values[2]);
            System.out.println(BALANCE);
            inputStream.close();
          } 
      catch (FileNotFoundException ex) {
            Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
   }

5 Answers5

1

store double variable values in to list

Like this:

List<String> l = new ArrayList<String>();
l.add(Double.toString(42.0));

or if you are trying to build a list of doubles, you use the wrapper type, like this:

List<Double> l = new ArrayList<Double>();
l.add(42.0);  // The double will be autoboxed as a Double.
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0
String filename = "bank-full.csv";
            List<Double> list = new ArrayList<Double>();
            File file = new File(filename);
            try {
                Scanner inputStream = new Scanner(file);
                inputStream.next();
                int count = 0;
                while (inputStream.hasNext()) {
                    String data = inputStream.next();
                    String[] values = data.split(";");
                    double BALANCE = Double.parseDouble(values[2]);
                    list.add(BALANCE);
                    System.out.println(BALANCE);
                    inputStream.close();
                }
            }
                catch (FileNotFoundException ex) {
                    Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
anurag gupta
  • 379
  • 1
  • 5
0

You can either initialize your list to hold type 'Double', or you can cast the doubles to strings before adding them to the list. It more or less depends on how you plan to use the list. Will you be storing the information back in as doubles from the list? Or will you be allowing the user to edit the values in the form of a string? Just a couple of examples.

Ryan Shukis
  • 345
  • 1
  • 10
0

If you want to save the balances values in a list of Strings, you can directly add them to it, like this:

List<String> balancesList = new ArrayList<String>();

And in your while loop:

 String data = inputStream.next();         
 String[] values = data.split(";");
 balancesList.add(values[2]); //Just add it to the list here

Because the balances values in values[2] are Strings.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
0

Try using an ArrayList or LinkedList: they both extend AbstractList so they have the method add(T), where T can be any object type (in your example Double).
In your situation you should do something like this:

List<Double> container = new ArrayList<Double>();
while (inputStream.hasNext()) {
    String data = inputStream.next();
    String[] values = data.split(";");
    double BALANCE = Double.parseDouble(values[2]);
    System.out.println(BALANCE);
    container.add(BALANCE);
}
inputStream.close();
//now "container" contains all balance values

ALso, you shouldn't close the input stream while reading, close it after the while loop.

If you want to store them in reverse order, use this add method which allows you to add an element at every position of the list, so you can do:

container.add(container.size(), BALANCE);
Tommaso Bertoni
  • 2,333
  • 1
  • 22
  • 22