0

My problem is probably ridiculously easy and I'm just missing something. My program crashes due to a null value of cell 1 during its first iteration. i troubleshot a bit myself and realized on iteration 1 the array length is 1 then after all other iterations the length is 2. this initial improper length causes a complete crash. Any ideas?

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

/* Takes in all of users personal information, and weather data. Then      proceeds to determine status of day + averages of the data values provided, then    reports to user*/
public class ClimateSummary
{
public static void main (String [] args) throws FileNotFoundException
{

Scanner sc = new Scanner (new File(args[0]));
    String name = sc.nextLine();
    String birthCity = sc.next();
    String birthState = sc.next();
    String loc = sc.next();
    int birthDay = sc.nextInt();
    String birthMonth = sc.next();
    int birthYear = sc.nextInt();
    int highTemp = 0;
    double avgTemp;
    double avgPrecip;
    int coldDays = 0;
    int hotDays = 0;
    int rainyDays = 0;
    int niceDays = 0;
    int miserableDays = 0;
    double totalTemp = 0;
    double totalPrecip = 0;


    int i = 0;
    while(i <= 5)
    {
        String storage = sc.nextLine();
        String[] inputStorage = storage.split(" "); //couldnt find scanf     equiv in c for java so using array to store multiple values.
            System.out.println(inputStorage[0]);
        int tempTemp = Integer.parseInt(inputStorage[0]);
            double tempPrecip = Double.parseDouble(inputStorage[1]);

            totalTemp = totalTemp + tempTemp;
            totalPrecip = totalPrecip + tempPrecip;

            if(highTemp < tempTemp)
            {
                highTemp = tempTemp;
            }

            if(tempTemp >= 60.0)
            {
                hotDays++;
            }else{
                coldDays++;
            }

            if(tempPrecip > 0.1)
            {
                rainyDays++;
            }

            if(tempTemp >= 60.0 || tempTemp <= 80.0 || tempPrecip == 0.0)
            {
                niceDays++;
            }else if(tempTemp < 32.0 || tempTemp > 90.0 || tempPrecip > 2.0)
            {
                miserableDays++;                
            }else{

            }

        i++;

}

    avgTemp = totalTemp/5;
    avgPrecip = totalPrecip/5;



    System.out.println("Name: " + name);
    System.out.println("Place of birth: " + birthCity + "," + birthState);
    System.out.println("Data collected at: " + loc);
    System.out.println("Date of birth: " + birthMonth + " " + birthDay +", "      + birthYear);
    System.out.println("");
    System.out.println("The highest temperature during this tine was " + highTemp + " degrees Farenheit");
    System.out.println("The average temperature was " + avgTemp + " degrees Farenheit");
    System.out.println("The average amount of precipitation was " + avgPrecip + " inches");
    System.out.println("Number of hots days = " + hotDays);
    System.out.println("Number of cold days = " + coldDays);
    System.out.println("Number of rainy days = " + rainyDays);
    System.out.println("Number of nice days = " + niceDays);
    System.out.println("Number of miserable days = " + miserableDays);
    System.out.println("Goodbye and have a nice day!");

}

Eric Thomas Columbus Nebraska Columbus 18 February 1990 54 0 44 2.2 64 0.06 26 0.5 34 0.02

LimeRain
  • 23
  • 3
  • Suppose that your file only had one word. What should you be doing with that information? – Makoto Sep 19 '15 at 03:52
  • 1
    Array lengths are fixed size, check your line value. Also, consider splitting on `\\s+` which would strip out contiguous line space. – Elliott Frisch Sep 19 '15 at 03:55
  • Don't know about the error, but this line should use `&&`, not `||`, otherwise it's always true: `if(tempTemp >= 60.0 && tempTemp <= 80.0 && tempPrecip == 0.0)` – Andreas Sep 19 '15 at 04:19
  • Quoting good suggestion by @RodrigoGomes: Can you share the first 4 lines of you input file? – Andreas Sep 19 '15 at 04:33
  • Eric Thomas Columbus Nebraska Columbus 18 February 1990 54 0 44 2.2 64 0.06 26 0.5 34 0.02 – LimeRain Sep 19 '15 at 04:39
  • the numbers are in pairs of two and on same line. ex- 54 0 and 44 2.2 – LimeRain Sep 19 '15 at 04:41

1 Answers1

0

If your file contains null values then you should handle it separately.... using something like this:

if (name == null) {
    //do something
}
else {
    // do something else;
}

A good discussion on nulls can be seen here...How to check for null value in java

Also, after splitting a string, you need to check if the array (which is the output) has values at the indices that you are using.

For example:

String name = "A/B/C";
String[] nameArray = name.split("/");

In the above case, nameArray[3] will throw an error.

Community
  • 1
  • 1
PNB
  • 41
  • 4