简体   繁体   中英

How to call a method from a different class in a java project

How do I call a method from a different class in a java project? I have two different classes, each are in the same java project. The name of the first class is Applications, while the name of the second class in ShowChar. These classes are each in the same java project. What I must do is obtain a string input from the user, then I must obtain an integer from the user, then I must tell the user what letter lies on the integer they chose. I did all of that. That is what the class ShowChar does, but what I am supposed to do is call a method from the ShowChar class in the Applications class. I can not get this to work. Please help.

Here is my ShowChar class-

public class ShowChar {


char showChar(String text, int index)
{
    char letter='0';

    if((text.equals(null)))
    {
    System.out.print("Invalid input string. The process"
                          + "terminates");
    }
    else{ 
        if(index<0 || index>=text.length())
        {
            System.out.print("Invalid input for index\n"
                           + "The first character of the text is " + text.charAt(0));
            return letter;
        }
    else{
        if(index>=0 && index<text.length()) 
        {
        System.out.println("The character you asked for is: " + text.charAt(index));
        return letter;
        }
    }
    }

return letter;
}
}`     

Here is what I have gotten figure out with my Applications class-

public static void main(String[] args) {
    Scanner keyboard= new Scanner(System.in);




    String text=JOptionPane.showInputDialog("Enter the text: ");
    System.out.println("Enter the index: ");
    int index= keyboard.nextInt();

    ShowChar sc = new ShowChar();




    System.out.println("character found: " + sc.showChar(text, index) );



}

}

Down at the bottom I am supposed to print to the console the letter that I found, but i cant seem to get it to work. Whenever I run my program with any input the System.out.println statement always comes back with "character found: 0" I must be missing something

在以下行添加cancat运算符+

 System.out.println("character found: "+sc.showChar(text, index) );

Aren't you doing exactly that ?

ShowChar sc = new ShowChar(); // object of ShowCar class




    System.out.println("character found: " + sc.showChar(text, index) ); // calling a method of ShowCar class

A method of a class can be called using the object name and the method name separated by dot( . ), as mentioned here:

objName.methodName(params);

And you are calling it correctly in your sysout:

sc.showChar(text, index)

You are simply missing to concatenate "character found:" string with the output of showChar method using + operatror in your sysout

 System.out.println("character found: "+ sc.showChar(text, index) );

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