0

I am trying to convert the import file String data type to Double and do the calculations, but some errors found. Any mistakes when making an assumption?

import java.io.*;
import java.util.*;
import java.lang.*;

public class Q4
{
    public static void main (String [] args) throws IOException, NumberFormatException
    {
        File file = new File("SalesData.txt");
        Scanner inputFile = new Scanner (file);

        while (inputFile.hasNext())
        {
            String weekly_Sale = inputFile.nextLine();
            String sale = weekly_Sale;
            String list = sale;
            Double value = Double.parseDouble(list);
            Double[]data = value.split(",");
            System.out.println(data);
        }
        inputFile.close();
    }
}

The file SalesData.txt contains the dollar amount of sales that a retail store made each day for a number of weeks. Each line in the file contains seven numbers, which are the daily sales for one week. The numbers are separated by a comma. The following is an example from the file:

2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36

Write a program that opens the file and processes its contents. The program should display the following:

• The total sales for each week • The average daily sales for each week • The total sales for all the weeks • The average weekly sales • The week number that had the highest amount of sales • The week number that had the lowest amount of sales

Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50

3 Answers3

2

Various things to point out:

  • As I commented before, Double doesn't have a split method and you can't directly parse to double a String like 2541.36,2965.88,1965.32...

  • It's better to use BufferedReader than Scanner to read a file because is a bit faster

  • You can use the try-with-resources statement to automatically close the io resources.
  • Your code can be shortened like this:

    try (BufferedReader br = new BufferedReader(new FileReader("SalesData.txt"))){
    
        String currentLine;
    
        // read each line until the end of the file
        while ((currentLine = br.readLine()) != null){
            // split each token to a double-parseable string value, then parse it to a double and finally 
            // collect those values into an array of doubles
            double[] data = Arrays.stream(currentLine.split(",")).mapToDouble(Double::parseDouble).toArray();
            System.out.println(Arrays.toString(data));
        }
    } catch (java.io.IOException e) {
        e.printStackTrace();
    }
    

For this I'm using Java 8 Stream API.

Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
1
    Double value = Double.parseDouble(list);
    Double[]data = value.split(",");

The order of your operations is incorrect. At first you need to split the line into individual string values and then parse them to double.

Try something like this

    String[] data = value.split(",");
    for (String item : data) {
         Double result = Double.parseDouble(item)
         // Do Stuff

    }
Iven
  • 26
  • 5
  • I am trying to change the code as below, but still have error msg: while(inputFile.hasNext()){ String weekly_Sale = inputFile.nextLine(); String sale = weekly_Sale; String[]list = sale.split(","); Double data = Double.parseDouble(list); System.out.println(data); } – Bonnie Wong Nov 07 '17 at 13:31
  • And you need to iterate over the array. Double data = Double.parseDouble(list) will not work if list is an array. – Iven Nov 07 '17 at 13:37
  • I am doing the same thing as you suggested, and add a syntax of- System.out.println(result). May I know if the 'result' can be print on screen? error: cannot find symbol – Bonnie Wong Nov 07 '17 at 13:39
0

Try this

String inputFile = new Scanner(new File("SalesData.txt"))
        .useDelimiter("\\A").next();

String[] s = inputFile.split(",");
double[] dValue = new double[s.length];
for (int i = 0; i < s.length; i++) {
    dValue[i] = Double.parseDouble(s[i]);
}
for (double d : dValue) {
    System.out.println(d);
}
Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
Anish
  • 1
  • 1