Declaring Java Array and Accessing Elements results in null

Asked 2 years ago, Updated 2 years ago, 33 views

I am a beginner in java.We are working on the problem of using the array we found in the problem book to find the zodiac sign, but the result will be null with the source code below.If you enter the Western calendar and divide it by 12, I would like to make it the Year of the Child, and if it is the Year of the Ox, I would like to ask you an additional questions.

package practice;

import java.util.Scanner;

public class Practice {

    public static void main(String[]args) {
        Scanner scan = new Scanner (System.in);
        int year=scan.nextInt();

        String [ ] array = new String [12];
        System.out.println(array[year%12]);

        array[0] = "It's the new year.";
        array[1]="It's the Year of the Rooster.";
        array[2] = "It's the year of the dog.";
        array[3] = "It's the Year of the Pig.";
        array[4] = "It's a child year.";
        array[5] = "It's the Year of the Ox.";
        array[6] = "It's the Year of the Tiger.";
        array[7] = "It's the Year of the Hare.";
        array[8] = "It's the Year of the Dragon.";
        array[9]="It's the Year of the Snake.";
        array[10] = "It's the year of the afternoon.";
        array[11] = "It's been a long time.";

        for(inti=0;i<array.length;i++){

        }
    }
}

java

2022-09-30 19:00

2 Answers

array is not initialized.
After substituting the array variable array,

System.out.println (array [year%12])

I think it will be the behavior you expect (other than null to output).

# Omit the code to print the correct zodiac sign for the year you entered.


2022-09-30 19:00

There should be no problem if you change the order as below.

public class Practice {
  public static void main(String[]args) {
    Scanner scan = new Scanner (System.in);
    int year=scan.nextInt();
    String [ ] array = new String [12];
    array[0] = "It's the new year.";
    array[1]="It's the Year of the Rooster.";
    array[2] = "It's the year of the dog.";
    array[3] = "It's the Year of the Pig.";
    array[4] = "It's a child year.";
    array[5] = "It's the Year of the Ox.";
    array[6] = "It's the Year of the Tiger.";
    array[7] = "It's the Year of the Hare.";
    array[8] = "It's the Year of the Dragon.";
    array[9]="It's the Year of the Snake.";
    array[10] = "It's the year of the afternoon.";
    array[11] = "It's been a long time.";
    System.out.println(array[year%12]);
  }
}

Also, if you do the following, it will be easier to understand, even if you specify something other than a number, it will not be an exception.

public class Practice {
  public static void main(String[]args) {

    final String[]array=new String[]{"Shin", "Tori", "Dog", "Pig", "Child", "Cow", "Tora", "Tora", "Ra", "Ta", "Ta", "Ta", "Ta", "Ta", "Morning", "Mi";

    while(true){
      Scanner scan = new Scanner (System.in);
      if(scan.hasNextInt()){//true only if the next token for this scanner is a valid int value
        int year=scan.nextInt(); // If you do not have the above decision control, a non-numeric character is specified, this is the exception.

        System.out.println(array[year%12]+" year."); // I don't need to put the common parts in the array.
      } else{
        continue; // Continue if not a valid int value
      }
    }
  }
}

If you want to run the above code on eclipse, you can exit by pressing the red button on the console tab.
For command operations, you can abort with Ctrl+C.


2022-09-30 19:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.