简体   繁体   中英

Why is my program printing “null” instead of the string?

I'm trying to create a program that will print the temperature's for each day of the week. I don't know why my code is printing "null" instead of each day of the week. I've never seen this error before (I'm in a beginning programming course) and I can't find any info in the textbook. This is my code so far...

import java.util.Scanner;
public class TempsInOneWeek {

public static void main(String[] args) {
    Scanner scnr = new Scanner (System.in);
    final int NUMBER_OF_DAYS = 7;
    String[] dayOfWeek = new String[NUMBER_OF_DAYS];
    int[] temp = new int [7]; //array of 7 temperatures for 7 days of the week
    int tempForDay = 0;
    int i = 0; //loop variable

    System.out.println("Enter the temperature for each day, beginning with Sunday.");

    for (i = 0; i <= temp.length; ++i) {
        temp[i] = scnr.nextInt();
        System.out.println(dayOfWeek[i] + "/'s temperature was: " + temp[i] + " degrees.");
    }

    dayOfWeek[i] = "Sunday";    
    dayOfWeek[i] = "Monday";
    dayOfWeek[i] = "Tuesday";
    dayOfWeek[i] = "Wednesday";
    dayOfWeek[i] = "Thursday";
    dayOfWeek[i] = "Friday";
    dayOfWeek[i] = "Saturday";

    temp[i] = scnr.nextInt();
    temp[i] = scnr.nextInt();
    temp[i] = scnr.nextInt();
    temp[i] = scnr.nextInt();
    temp[i] = scnr.nextInt();
    temp[i] = scnr.nextInt();
    temp[i] = scnr.nextInt();
}

Obviously the arrays are in the wrong place but it doesn't affect the output right now so I wanted to show that I'm at least trying here. Really my only question is how to get the day of the week to print. And maybe a little direction on what to do with my two arrays above.

Simply move these following assignments before for loop

dayOfWeek[i] = "Sunday";    
dayOfWeek[i] = "Monday";
dayOfWeek[i] = "Tuesday";
dayOfWeek[i] = "Wednesday";
dayOfWeek[i] = "Thursday";
dayOfWeek[i] = "Friday";
dayOfWeek[i] = "Saturday";

When you attempt to retrieve the data from your array...

System.out.println(dayOfWeek[i] + "/'s temperature was: " + temp[i] + " degrees.");

...you haven't actually put anything into the array at this point.

When you create your array...

String[] dayOfWeek = new String[NUMBER_OF_DAYS];

This creates the array and populates each index with null

Remember, when you declare and initialise an array, unless you explicitly populate the array, it will be filled with default values for the array's type (String in your case - and the default value for String is null).

So, you need to move the lines of code that actually populate the array to before the point at which you attempt to retrieve the data...

dayOfWeek[i] = "Sunday";    
dayOfWeek[i] = "Monday";
dayOfWeek[i] = "Tuesday";
dayOfWeek[i] = "Wednesday";
dayOfWeek[i] = "Thursday";
dayOfWeek[i] = "Friday";
dayOfWeek[i] = "Saturday";

for (i = 0; i <= temp.length; ++i) {
    temp[i] = scnr.nextInt();
    System.out.println(dayOfWeek[i] + "/'s temperature was: " + temp[i] + " 
    degrees.");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM