-1

I have to produce a program that can read a file I have been given. The file contains 365 lines of data that is supposed to resemble temperature values. The first value on each line is the low temp, and the second is the high temp. From these values, I have to determine the number of days, the lowest of the low temps, highest of the max temps, and then the average of the lows and the average of the highs.

Here is what I have so far. The problem I am having is splitting the two values on each line to deal with them individually so I can turn them into a double so I can do computations on them

import TextIO.*;

public class Temperature {

   public static void main(String[] args) {


   TextIO.readFile("temperatures.dat");


   //double salesTotal;  // Total of all sales figures seen so far.
   int dayCount, string1, a;
   double tempTotal=0;
   String dataString; // Number of cities for which data is missing.

   dayCount = 0;

   while ( ! TextIO.eof() ) {  // process one line of data.
        dayCount++;
        dataString = TextIO.getln();  // Get the rest of the line.
        a = dataString.indexOf("   ");
        TextIO.put("\n" + a);

     }
  TextIO.put(dayCount);

  }



} 
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • What format are the values in the file in? – Yanki Twizzy Feb 09 '14 at 06:32
  • The file is a .dat, but the values are mostly double, some ints. Here is an example: -1.5 13.1 -13.3 -5.6 22.1 28.6 EDIT* it isn't holding it's format when I post it here, but the numbers are in a chart style.. It looks like there are several spaces between them, but there are none. – ImprovingPants Feb 09 '14 at 06:38
  • possible duplicate of [How to split a String in Java](http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – Brian Roach Feb 09 '14 at 06:50

1 Answers1

0

I get the feeling your question comes from a homework assignment so I wont take all the fun out of it but... What you are looking at is most likely a tab represented by\t in java.

You have so far:

dataString = TextIO.getln();

You will then need to separate those 2 numbers, there are several ways to do this but Ill take your lead and use the indexOf method.

String low = dataString.substring(0, dataString.indexOf("\t"));
String high= dataString.substring(dataString.indexOf("\t")+1);

you then need to convert those strings to numbers, but im sure you can figure that out :).


If you ever find yourself wonder what is that character you can do something like this
public static void printStringChars(String str) {
    for(int i=0;i<str.length();i++)
        System.out.println("(" + str.charAt(i) + ") " + (int)str.charAt(i));
}

Then go look up the number it prints out in an ASCII table such as this one http://www.asciitable.com/

ug_
  • 11,267
  • 2
  • 35
  • 52