0

Hi guys i have a text file in the following path C:/Users/Marc/Downloads/vector25 which contains comma-separated values in the following format

-6.08,70.93,-9.35,-86.09,-28.41,27.94,75.15,91.03,-84.21,97.84, -51.53,77.95,88.37,26.14,-23.58,-18.4,-4.62,46.52,-19.47,17.54, 85.33,52.53,27.97,10.73,-5.82,

How would i read this text file and store those doubles in an array ?

I'm currently thinking of trying a buffered reader but so far the answer eludes me can anyone point me in the right direction ?

    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Scanner;
    public class subvector {

public static void main(String[] args){

    FileReader file = null;
    try {
        file = new FileReader("C:/Users/Marc/Downloads/vector25");
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    ArrayList<Double> list = new ArrayList<Double>();
    int i=0;
    try {
        Scanner input = new Scanner(file);
        while(input.hasNext())
        {
           list.add(input.nextDouble());
           i++;
        }
        input.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
   for(double k:list){
       System.out.println(k);

   }
}
1
  • What is the problem with your code? Do you have a problem? An error? Commented Sep 23, 2013 at 11:57

3 Answers 3

3

You should use a delimeter

Scanner input = new Scanner(file);
input.useDelimeter(",");
Sign up to request clarification or add additional context in comments.

Comments

3

Scanner.nextDouble() uses whitespace as the delimiter by default. Your input has commas as delimiter. You should use input.useDelimiter(",") to set commas as the delimiter before calling input.hasNext(). Then it should work as expected.

Comments

0

I think your code snippet will not for the specified data i.e. -6.08,70.93,-9.35,-86.09,-28.41,27.94,75.15,91.03,-84.21,97.84, -51.53,77.95,88.37,26.14,-23.58,-18.4,-4.62,46.52,-19.47,17.54, 85.33,52.53,27.97,10.73,-5.82,

But your program will work fine these type of data :

19.60
63.0
635.00
896.63
47.25

I have modified your program and tested with your data also. It's working as expected.

public static void main(String[] args) {
    FileReader file = null;
    try {
        file = new FileReader("D:\\log4j\\vector25.txt");
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    ArrayList<Double> list = new ArrayList<Double>();
    int i=0;
    Double d= null;
    try {
        BufferedReader input = new BufferedReader(file);
        String s=null;
        while((s=input.readLine())!=null) {
            StringTokenizer st = new StringTokenizer(s,",");
            while(st.hasMoreTokens()) {
            try {
                d = Double.parseDouble(st.nextToken());
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            list.add(i, d);
        }
    }
    input.close();
} catch(Exception e) {
    e.printStackTrace();
}
for(double k:list) {
    System.out.println(k);
}
}

Please review it and let me know if you update anything.

This is my first POST on stackoverflow.

Thanks, Prasad

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.