Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8896825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:10:35+00:00 2026-06-15T00:10:35+00:00

I’m trying to read the lines from a file and store each value individually

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:10:36+00:00Added an answer on June 15, 2026 at 12:10 am

    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]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to render a haml file in a javascript response like so:
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.