2

I have the following code already, which goes as far as appending the doubles.

Scanner sc1 = new Scanner(System.in);

try {

    System.out.println("Enter filename:");
    String name = sc1.nextLine();//determines name of file
    File file = new File(name);//creates above file     
    FileReader fileReader = new FileReader(file);//file is read 
    BufferedReader bufferedReader = new BufferedReader(fileReader );//BufferReader reads file, line by line
    StringBuffer stringBuffer = new StringBuffer();//appended to StringBuffer
    String line;//reads each 

    while ((line = bufferedReader.readLine()) != null) 
    {
        stringBuffer.append(line + "\n");           
    }

    fileReader.close();
    System.out.println("Contents of file:");
    System.out.println(stringBuffer.toString());

    } 

catch (IOException e) 
{
    e.printStackTrace();
}

The code reads a file that has a double on each line

ex:

1

2

3

4

5

...

How do I store each of these doubles into an array?

4 Answers 4

2

If you want to stick to using Array only, convert StringBuffer to String[] like this:

    String [] stringArray = stringBuffer.split("\n");  

Then convert string array to double array:

    Double [] doubleArray = new Double[stringArray.size];
    for(int i=0 : i < stringArray.size : i++){  
        doubleArray[i] = Doube.parseDouble(stringArray[i]);  
    }

If you must use StringBuffer, you can divide into array by StringTokenizer class.
Alternatively, you can use String instead of StringBuffer and then split() method will work with de-limiter.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I'd like to stick with Arrays, and I used your code. But it tells me that the method split("\n") is undefined. Also, I cannot directly convert the elements in the String array into the double array dataArray[i] = stringArray[i];
I have updated my answer. String Array should now convert to Double. For split() you have two options as highlighted in main post.
2

You could use an ArrayList:

ArrayList<Double> doubles = new ArrayList<Double>();
while ((line = bufferedReader.readLine()) != null) 
{
    doubles.add(Double.parseDouble(line));        
}

3 Comments

I've never used ArrayList before. I notice that no array is declared above, so I was wondering how I implement the above code into a specific array?
save the final result into a String, use String.split() to split it into an array.Sorry about the answer, misunderstood what you were looking for.
With the ArrayList, you don't need the StringBuffer at all. I instantiated the array in my code, you can copy that exactly. If you need help printing out the values of an ArrayList, just google "printing out values of ArrayList"
1

Basic solution

    ArrayList<Double> doubles = new ArrayList<>();
    while ((line = bufferedReader.readLine()) != null)
    {
        stringBuffer.append(line + "\n");
        try{
            double value = Double.parseDouble(line );
            doubles.add(value);
        } catch(NullPointerException e){
            //null string
        } catch(NumberFormatException e){
            //no parsable on the current line
        }
    }

Comments

0

I don't get the problem...Just make an array that stores the contents of the line as the file is read.

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.