-4
public class a2 {
    public static void main(String[] args) {
        isHappy(args[0]);
    }

    public static boolean isHappy(){
        int i = Integer.parseInt(args[0]);
        if(i == 132){
            return false;
        }
    }
}

I have a question because I am new at JAVA, how can I invoke method isHappy from main method and use args[0] as an argument in method isHappy? This is my code. Thank you.

MFisherKDX
  • 2,840
  • 3
  • 14
  • 25

3 Answers3

1

First of all try to learn Java naming convention, in java class name first Letter should be capital.

public class A2 {

    public static void main(String[] args) {

        isHappy(args[0]);

    }

    //Function must take arugment
    public static boolean isHappy(String str) {
        int i = Integer.parseInt(str);
        if (i == 132) {
            return false;
        }
        return true; //missing return type
    }

}
Omore
  • 614
  • 6
  • 18
0
public static boolean isHappy()

needs to be

public static boolean isHappy(String[] param)

where param can be nearly whatever name you want. There are some restrictions

Otherwise, your method can't accept parameters and the compiler should give you an error.

Edit:

If you want to transfer only a String, like args[0] to isHappy, then use public static boolean isHappy(String param)

WagAd22
  • 1
  • 3
0

Pass the value args[0] as a parameter for the isHappy function, Also this code is not having a return statement outside the if So adding else with return as true;

public class a2 {
public static void main(String[] args) {
   isHappy(args[0]);
}
public static boolean isHappy(String argValue){
  int i = Integer.parseInt(argValue);
  if(i == 132){
    return false;
  }
  else{
    return true;
  }
}

}