有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java数组长度结果

我的问题可能非常简单,我只是错过了一些东西。我的程序在第一次迭代时由于单元格1的空值而崩溃。我自己解决了一点问题,在迭代1时意识到数组长度是1,然后在所有其他迭代之后,长度是2。初始长度不正确会导致完全崩溃。有什么想法吗

`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!");

}

埃里克·托马斯 哥伦布 内布拉斯加州 哥伦布 18 二月 1990 54 0 44 2.2 64 0.06 26 0.5 34 0.02


共 (1) 个答案

  1. # 1 楼答案

    如果你的文件包含空值,那么你应该单独处理。。。。用这样的方法:

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

    关于空值的讨论可以在这里看到How to check for null value in java

    此外,在拆分字符串后,需要检查数组(即输出)是否在使用的索引处具有值

    例如:

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

    在上述情况下,nameArray[3]将抛出一个错误