简体   繁体   中英

Invoke Java Method

I'm trying to invoke a java method to determine if the user inputed letter is a vowel or consonant. Not sure how to properly invoke the method.

import java.util.*;
public class HW8Problem1{
   public static void main(String[] args) {

      Scanner input = new Scanner(System.in);

      System.out.print("Enter a letter: ");

      String letter = input.nextLine();

   }

   public static void vowel(String a){

      if (a.charAt(0) == 'a' || a.charAt(0) == 'A'){
         System.out.println(" is a vowel");

         if (a.charAt(0) == 'e' || a.charAt(0) == 'E')
            System.out.println(" is a vowel");

         if (a.charAt(0) == 'i' || a.charAt(0) == 'I')
            System.out.println(" is a vowel");

         if (a.charAt(0) == 'o' || a.charAt(0) == 'O')
            System.out.println(" is a vowel");

         if (a.charAt(0) == 'u' || a.charAt(0) == 'U')
            System.out.println(" is a vowel");

         else
            System.out.println(" is a consonant");    

      }
   }
}

You can invoke a method by specifying its name followed by parentheses ( () ), which contain the arguments you want to pass to the method, if there are any. In your case, eg:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a letter: ");
    String letter = input.nextLine();
    vowel(letter); // Here!
}

As Mureinik said, you can call the method using vowel(line);
But seeing your code, I'd suggest another improvements. Always try not to repeat yourself. Can you see how you repeat almost the same part of code multiple times? Remove the duplicates and create the method differently, eg:

private static boolean isVowel(String line) {
    line = line.toLowerCase();
    return line.equals("a") || line.equals("e") || line.equals("i") || line.equals("o") || line.equals("u");
}

and then in the main method just print

System.out.println(isVowel(line) ? " is a vowel" : " is a consonant");

Note that this solution assumes that the line is not null and that line.length() == 1 . Try to exercise the input parameter treatment by yourself so you don't pass words into the isVowel() method.

Below is my suggestion, it is tested and well working. Please be noted that you have to make sure the if expression should be in one line.

import java.util.*;

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

      Scanner input = new Scanner(System.in);

      System.out.print("Enter a letter: ");

      String letter = input.nextLine();
//the below is the start of  IF expression, put it in one line code
      if (letter.toUpperCase().charAt(0)== 'A'||letter.toUpperCase().charAt(0)== 'E'||letter.toUpperCase().charAt(0)== 'O'||letter.toUpperCase().charAt(0)== 'i'||letter.toUpperCase().charAt(0)== 'U' ){ //End of IF expression until here, in 1 line
            System.out.println("is a vowel");
      }else{
        System.out.println(" is a consonant"); 
      }
      
   
   }

 }

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