0

I'm trying to figure this out: I'm asked to find the sum of the temperatures, but first i have to get them into an array list. My code so far:

    public static void main(String[] args) {

    String fileName = "weather.txt";
    File file = new File(fileName);

    Scanner scanner = null;
    try {
        scanner = new Scanner(file); 
    } catch (FileNotFoundException e) {
        System.out.println("The File can't be found!");
        return;
    }

    ArrayList<String> weather = new ArrayList<String>();
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        String[] splittedString = line.split("|");
        String[] split = line.split("\\|");
        System.out.print("{");
        System.out.print("\"Tempreture\" = " + splittedString[0]);
        System.out.print(", \"Humidity\" = " + splittedString[1]);
        String[] date = split[2].split("/");
        System.out.print(", \"Day = " + date[0]);
        System.out.print(", \"Month\" = " + date[1]);
        System.out.print(", \"Year\" = " + date[2]);
        System.out.print("}");

        System.out.println("\n");
        weather.add(splittedString[0]);
        for (int i = 1; i < splittedString.length; i++) {
            System.out.print(splittedString[i]);
        }
        weather.add(splittedString[0]);
        for (int i = 0; i < splittedString.length; i++) {
            System.out.print(splittedString[i]);

        }

I couldn't find the proper way to get the temperature data, convert to double and find the sum.

The data from txt is:

11.4|12|5/1/1995

13.0|10|6/1/1995

13.5|11|7/1/1995

11.2|11|8/1/1995

14.6|11|9/1/1995

timgeb
  • 76,762
  • 20
  • 123
  • 145
DoeJ
  • 29
  • 3

4 Answers4

1

Your code looks complicated. You only need to do two things.

while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    String[] weatherParameters = line.split("\\|");
    weather.add(weatherParameters[0]); // adds the first split of the string
    // in this case the temperature.
    // weather.add(weatherParameters[1]); // would add the humidity
}

That's all. You only have to add the first parameter of the splitted String. You can access the different parameters by simply changing the index. weatherParameters[2] contains the date.

LordAnomander
  • 1,103
  • 6
  • 16
1

If your split is OK then you can get the average without using ArrayList by using sumTemp and countTemp variable like bellow:

    double sumTemp = 0;
    int countTemp = 0;
    while() {
     ...............
     ...............

     sumTemp += Double.valueOf(splittedString[0]);
     countTemp++;
     .....................
     .....................
    }//end while

    double avg = sumTemp / countTemp;
mmuzahid
  • 2,252
  • 23
  • 42
  • `Double.valueOf("splittedString[0]")` will result in an exception, because you certainly cannot parse this `String` to a `double` value. Furthermore, you do not answer the OP's question, as he was wondering on how he could get the input in an `ArrayList`. – LordAnomander Feb 01 '16 at 09:48
  • Try your code and you will see that it is not working properly. `splittedString[0]` is `1` if the input was `11.4|12|5/1/1995`. – LordAnomander Feb 01 '16 at 09:51
  • {"Tempreture" = 1, "Humidity" = 6, "Day = 30, "Month" = 12, "Year" = 1995} 6.6|12|30/12/1995 {"Tempreture" = 1, "Humidity" = 8, "Day = 31, "Month" = 12, "Year" = 1995} 8.4|12|31/12/1995 <<<<< this is my output! – DoeJ Feb 01 '16 at 09:56
  • @LordAnomander how! I did not write any splitting code! then how ``11.4|12|5/1/1995`` is producing ``1`` – mmuzahid Feb 01 '16 at 09:56
  • @mmuzahid You did not correct the OP's code where he made a mistake that causes the input to be splitted in the wrong way. I don't see the need for a discussion here anyways, as I said you didn't try to solve the OP's *real* problem. – LordAnomander Feb 01 '16 at 09:58
0

Try this.

while (scanner.hasNextLine()) {
        String line = scanner.nextLine();

        //Split method takes a regular expression and you have to escape the pipe sign.
        weather.add(line.split("\\|")[0]);
}
Kashif Nazar
  • 20,775
  • 5
  • 29
  • 46
0

You can also create a list of double values to hold temperatures and use Double.valueOf(string)on the values returned by the splitting method.

By the way it's good practice to use the interface when declaring a list, in your case the LinkedList might be actually more effective and you can use type inference during instantiation which would result in

List<Double> temperatures = new LinkedList<>();
while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    String[] weatherParameters = line.split("\\|");
    temperatures.add(Double.valueOf(weatherParameters[0]));
}    
Community
  • 1
  • 1
Maciej
  • 546
  • 4
  • 15