Java method argument return value

Asked 2 years ago, Updated 2 years ago, 30 views

Prerequisites/What you want to achieve

The variable int num is given an input value by the system.
A method is created, the method is called by main, and all elements of the return value are displayed.
At that time, processing is implemented so as not to display the 0th element.
Also, when the return value is null, the output is not the output of the element but "the specified ID cannot be found".
When calling a method, the real argument sets the variable int num.

Input values

1
2
3
4

Expectations

 Ayeo
kakikukeko
sasisuseso

Method Contents

{"1", "A", "I", "Ueo"}
{"2", "ka", "ki", "kakeko"}
{"3", "Sa", "Shi", "Sasiso"}

Declare three static array variables with the above elements.
A numeric type argument is converted into a String type variable.
A 0th value of each array is compared with an argument converted into a String type, and when the argument is compared and matched, the matched array variable is used as a return value.
If it does not match any array, null is the return value.

Problems/Error Messages you are experiencing

java.lang.NullPointerException

appears.
I think the reason is that there was no processing when null was returned, but I looked into various things and wrote the code, but I couldn't process it well.
I would appreciate your advice.

Source Code

import java.util.Scanner;
public class Main {
  public static void main(String[]args) {
    Scanner scan = new Scanner (System.in);
    String text = scan.next();
    int num = Integer.parseInt(text);
    String [ ] fuga = getUserProfile (num);
      for (inti=1; i<fuga.length;i++) {
        if(fuga[i]!=null){
          System.out.println(fuga[i]);
        } else {
          System.out.println("The specified ID is not found");
        }
      }
  }
  static String [ ] getUserProfile(int num) {
    String [ ] data1 = {"1", "A", "I", "Ueo";
    String [ ] data2 = {"2", "ka", "ki", "kakeko";
    String [ ] data3 = {"3", "sa", "sh", "sasu" };
    String hoge = Integer.toString(num);
      if(data1[0]==hoge){
        return data1;
      } else if(data2[0]==hoge){
        return data2;
      } else if(data3[0]==hoge){
        return data3;
      } else {
        return null;
      }
  }
}

java

2022-09-30 15:05

2 Answers

This is because == is used to compare strings.All

==hoge

.equals(hoge)

If you replace it with , it will do the desired behavior.However, if you enter a number other than 1, 2, and 3 after making this correction, NPE will occur because the following conditions are met:

} else{
        return null;

There are several other parts that need to be fixed.


2022-09-30 15:05

It may be a long way to say this, but I think you should first write down which steps are failing and what you typed in num.I think that would be easier to explain.
After that,

for(inti=1;i<fuga.length;i++){ 

I think "fuga" is NULL in , so I can't see the length.If the previous step is NULL,

System.out.println("The specified ID cannot be found");

I think it will be the action you expect.


2022-09-30 15:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.