0

I'm using Eclipse for this program and I'm trying to do is get the program to read the text file "ClimateDataA" and then find the date of the first time the temperature is under 80 degrees and I have to use for & while loops.

This is what the text file "ClimateDataA" contains:

14939 20140801 86

14939 20140802 90

14939 20140803 93

14939 20140804 87

14939 20140805 93

14939 20140806 83

14939 20140807 81

14939 20140808 83

14939 20140809 85

14939 20140810 85

14939 20140811 80

14939 20140812 81

14939 20140813 87

14939 20140814 89

14939 20140815 76

14939 20140816 87

14939 20140817 92

14939 20140818 91

14939 20140819 93

14939 20140820 96

14939 20140821 90

14939 20140822 93

14939 20140823 95

14939 20140824 91

14939 20140825 83

14939 20140826 81

14939 20140827 77

14939 20140828 82

14939 20140829 75

14939 20140830 87

14939 20140831 92

Column 1 is an ID number. Column 2 is the date. Column 3 is the temperature.

This is my code so far:

import java.util.Scanner;
import java.io.*;


public class ClimateSummary
{   
    public static void main (String [] args) throws FileNotFoundException
    {

    Scanner userInfo = new Scanner (new File("MyInfo.txt"));
    Scanner weatherData = new Scanner (new File("ClimateDataA.txt"));

    String userName = userInfo.nextLine();
    String userBirthPlace = userInfo.nextLine();
    int userBirthDay = userInfo.nextInt();
    String userBirthMonth = userInfo.next();
    int userBirthYear = userInfo.nextInt();
    String dataLocation = userInfo.next();

    int [][] myArray = new int[31][3];

    for (int i = 0; i < myArray.length; i++) {

    }


    System.out.println("Welcome " + userName + "! This is the Super Computing Weathernator 3000!");
    System.out.println();
    System.out.println("Name: " + userName);
    System.out.println("Date of birth: " + userBirthDay + " " + userBirthMonth + " " + userBirthYear );
    System.out.println("Place of birth: " + userBirthPlace);
    System.out.println("Data collected at: " + dataLocation);
    System.out.println();
    System.out.println("The first day under 80 degrees is ");


    userInfo.close();

 }
}

Without these lines of code the program can run.

Scanner weatherData = new Scanner (new File("ClimateDataA.txt"));

 int [][] myArray = new int[31][3];

    for (int i = 0; i < myArray.length; i++) {

}

This is the output I'm trying to get:

Welcome Eric Hornberger! This is the Super Computing Weathernator 3000!

Name: Eric Hornberger

Date of birth: 18 February 1960

Place of birth: Columbus, Nebraska

Data collected at: Columbus

The first day under 80 degrees is 15/08/2014

The first five lines are using data from the "MyInfo" text file.

I'm going to have to parse to get the date exactly like that in the output but I only need help with getting the data I'm looking for.

Can anyone help me do this properly? Thanks for your time!

AirCode
  • 3
  • 3
  • Well, first of all, looking at the code you have, you have to populate your weatherArray with entries. Have you tried to call `weatherData.nextLine()` and see what it returns? (use `System.out.println()` for example) – Serhiy Oct 08 '14 at 06:39
  • what have you tried, what did you see and how did this differ from what you expect? – Romski Oct 08 '14 at 06:40
  • @Serhiy It gave me an error (I also changed the `weatherData.nextLine()` to `weatherData.nextInt()` because I declared the variable as an int) _Exception in thread "main" java.io.FileNotFoundException: ClimateDataA.txt (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(Unknown Source) at java.util.Scanner.(Unknown Source) at ClimateSummary.main(ClimateSummary.java:11)_ – AirCode Oct 08 '14 at 06:54
  • @Romski Beyond what I have in the code that you've seen I haven't tried much of anything else because I literally have no clue as to what I need to do. I'm pretty knew to coding. – AirCode Oct 08 '14 at 06:56
  • @AirCode the exception is prettya explanatory, the problem is that your file is not present in the working directory, either create a file or copy it from other location to location where `MyInfo.txt` is located (should be in your workspace\projectName directory) – Serhiy Oct 08 '14 at 06:59
  • On tip: Look at the class java.text.SimpleDateFormat, that can help you formatting the dates as you want: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – AndersG Oct 08 '14 at 07:00
  • @aircode this site is for helping with specific problems, not for doing it for you. As Serhiy has said, your first problem is locating the input file. – Romski Oct 08 '14 at 07:05
  • @Serhiy @Romski I'm sorry if it seemed like I was asking for too much, locating the input `ClimatDataA.txt` isn't my problem here, I already know how that works which is why the `MyInfo.txt` parts worked. I'm asking how I go about setting up the 2D array properly if I haven't already and how I'm to call the specific piece of data (in this case located at [14][2] of the array). – AirCode Oct 08 '14 at 07:18

1 Answers1

0

You should think of each line in your file as a separate string. Then you can split those strings into parts (id, date, temperature) and get the data from each column. Here is an older question about splitting up strings: How to split a string in Java

And here is info on how to read the lines from your file: Java read line from file

So putting it together:

  • Loop thorugh the lines in your file
  • Treat each line as a String that you split (on blank space in your case) to get the data
  • If the temperature value is lower than the previously found minimum, save the values in some variables

Hope that will at least help you get started!

Another thing also: Since you are using Java, you might want to consider using an array of objects for you data instead of a 2D array, something along the line of:

public Class WeatherData {
   private int id;
   private Date date;
   private int temperature;

   /** Getters, setter, contructor etc */ 
}

That will make your program easier to read and modify when needed.

Community
  • 1
  • 1
AndersG
  • 365
  • 4
  • 12