Kynian
Kynian

Reputation: 670

Reading from a file, getting all nulls

I'm trying to read the lines from a file and store each value individually in an array. It seems to read the first two header lines just fine, but then just returns null for everything. I use methods to convert some of the variables, but even when I read the pure int from the file it just returns "0" which doesn't make sense, since it seems to be reading through the file without an issue using the same basic method (but without the arrays) in a previous project. Here is my code:

package prog9;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.Scanner;

public class Main {

private static String firstline;
private static String secondline;

public static void main(String[] args) throws IOException {
    File file = new File("files/PortlandWeather2011.txt");

    int count = 0;

    BufferedReader reader2 = new BufferedReader(new FileReader(file));
    while (reader2.readLine() != null)
        count++;
    reader2.close();

    Scanner reader = new Scanner(file);

    String prcp[] = new String[count];
    String snow[] = new String[count];
    String snwd[] = new String[count];
    String tmin[] = new String[count];
    String tmax[] = new String[count];
    String station[] = new String[count];
    String date[] = new String[count];
    int test[] = new int[count];

    firstline = reader.nextLine();
    secondline = reader.nextLine();
    System.out.println(firstline);
    System.out.println(secondline);

    int i = 0;

    while (reader.hasNextLine()) {
        station[i] = reader.next();
        // test[i] = reader.nextInt();
        date[i] = convertDate(reader.nextInt());
        prcp[i] = convertTenthsToInches(reader.nextInt());
        snow[i] = convertToInches(reader.nextInt());
        snwd[i] = convertToInches(reader.nextInt());
        tmax[i] = convertToDegrees(reader.nextInt());
        tmin[i] = convertToDegrees(reader.nextInt());
        // System.out.println(reader.nextLine());
        i++;
        if (reader.hasNextLine()) {
            reader.nextLine();
        }
    }
    reader.close();

    for (int x = 0; x < count; x++) {
        System.out.printf("%s %s %s %s %s %s %s\n", station[i], test[i], prcp[i], snow[i], snwd[i], tmax[i], tmin[i]);
    }

}

// our first helper method, takes the date and converts it into the proper
// format as a string.
private static String convertDate(int d) {
    // first make it into a date so we can strip the proper sections out of
    // it
    String date = "" + d;
    // store the year, month, and day seperately so we can rearange them
    String year = date.substring(0, 4);
    String month = date.substring(4, 6);
    String day = date.substring(6, 8);
    // add the slashes and the day/month/year in the correct order.
    String translated_date = month + "/" + day + "/" + year;
    // spit the translated date out.
    return translated_date;

}

// first conversion for inches, since some are in tenths and some are not.
private static String convertTenthsToInches(int i) {
    // create a number format so we can limit the amount of decimal places
    // cleanly.
    String converted = null;
    NumberFormat nf = NumberFormat.getNumberInstance();
    // devide the input by 10 so its not in tenths anymore.
    double input = i / 10.0;
    // if its 0 we just want 0.0, so lets test that now.
    if (input == 0) {
        converted = "0.0";
    } else {
        // if its not 0 we want to set the maximum and minimum decimal
        // places allowed, in this it would just be 1.
        nf.setMaximumFractionDigits(1);
        nf.setMinimumFractionDigits(1);
        // and finally format it using our numberformat and devide by 25.4
        // so we get inches instead of millimeters.
        converted = nf.format((input / 25.4));
    }
    // returns our new number, and formats it so its padded to 8 characters.
    return String.format("%8s", converted);

}

// our second inches helper method. for anything that isnt in tenths.
private static String convertToInches(int input) {
    String converted = null;

    NumberFormat nf = NumberFormat.getNumberInstance();

    if (input == 0) {
        converted = "0.0";
    } else if (input == 9999) { 
        converted = "----";
    } else {// exactly the same as before
        nf.setMaximumFractionDigits(1);
        nf.setMinimumFractionDigits(1);
        converted = nf.format((input / 25.4));
    }

    return String.format("%8s", converted);
}

// and finally converting the tenths of C to F
private static String convertToDegrees(int i) {
    String converted = null;
    NumberFormat nf = NumberFormat.getNumberInstance();
    double input = i / 10.0;

    if (input == 0) {
        converted = "0.0";
    } else if (input == 9999) {
        converted = "----";
    } else {
        nf.setMaximumFractionDigits(1);
        nf.setMinimumFractionDigits(1);
        // we convert the celsius to degrees using our formula
        converted = nf.format((input * 9 / 5 + 32));
    }
    // and then format it so its padded to 8 characters and return it.
    return String.format("%8s", converted);
}
}

The file looks like this:

STATION           DATE     PRCP     SNOW     SNWD     TMAX     TMIN     
----------------- ---------- -------- -------- -------- -------- -------- 
GHCND:USW00014764 20110101        0        0      127      122      -17 
GHCND:USW00014764 20110102        5        0      102       67       28 
GHCND:USW00014764 20110103       13        0       76       44      -56 
GHCND:USW00014764 20110104        0        0       76        6      -83 
GHCND:USW00014764 20110105        0        0       76       17      -83 
GHCND:USW00014764 20110106        0        0       76      -11     -106 
GHCND:USW00014764 20110107        0        0       76      -17     -122 
GHCND:USW00014764 20110108        0        0       51        6      -78 
GHCND:USW00014764 20110109        3        3       76       39      -44 
GHCND:USW00014764 20110110        0        0       51       28      -67 
GHCND:USW00014764 20110111        0        0       51       -6     -122 
GHCND:USW00014764 20110112      185      330       76        0      -44 

and the output always comes out like this:

STATION           DATE     PRCP     SNOW     SNWD     TMAX     TMIN     
----------------- ---------- -------- -------- -------- -------- -------- 
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null
null null null null null null null

Can anyone explain why this could be happening?

Upvotes: 0

Views: 158

Answers (3)

Debobroto Das
Debobroto Das

Reputation: 862

would you please change the following

for (int x = 0; x < count; x++) {
    System.out.printf("%s %s %s %s %s %s %s\n", station[i], test[i], prcp[i], snow[i],     snwd[i], tmax[i], tmin[i]);
}

into---

for (int x = 0; x < count; x++) {
    System.out.printf("%s %s %s %s %s %s %s\n", station[x], test[x], prcp[x], snow[x], snwd[x], tmax[x], tmin[x]);
}

Upvotes: 3

Vishal
Vishal

Reputation: 3279

Minor error in the code I think... Replace these lines for (int x = 0; x < count; x++) { System.out.printf("%s %s %s %s %s %s %s\n", station[i], test[i], prcp[i], snow[i], snwd[i], tmax[i], tmin[i]); }

with following lines

for (int x = 0; x < count; x++) {
    System.out.printf("%s %s %s %s %s %s %s\n", station[x], test[x], prcp[x], snow[x], snwd[x], tmax[x], tmin[x]);
}

Upvotes: 5

Gili
Gili

Reputation: 90023

Your code checks Scanner.hasNextLine() but neglect to invoke Scanner.nextLine() to advance to the next line.

Upvotes: 0

Related Questions