简体   繁体   English

如何从同一个java项目中的不同类调用另一个方法

[英]How to call another method from a different class in the same java project

How do I call a method from a different class in a java project? 如何从java项目中的其他类调用方法? I have two different classes, each are in the same java project. 我有两个不同的类,每个类都在同一个java项目中。 The name of the first class is Applications, while the name of the second class in ShowChar. 第一个类的名称是Applications,而ShowChar中的第二个类的名称。 These classes are each in the same java project. 这些类都在同一个java项目中。 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. 这就是ShowChar类所做的,但我应该做的是从Applications类中的ShowChar类调用一个方法。 I can not get this to work. 我不能让这个工作。 Please help. 请帮忙。

Here is my ShowChar class- 这是我的ShowChar课程 -

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. 每当我使用任何输入运行我的程序时,System.out.println语句总是返回“找到的字符:0”我必须遗漏一些东西。

You don't assign a value to letter 您没有为信件分配值

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));
        letter = text.charAt(index);
        return letter;
        }
    }
    }

return letter;
}

you make initialization as : 你初始化为:

char letter='0';

but you never assign the char a value later, that is why it return "0" which is the default value . 但是你以后永远不会给char赋值,这就是它返回默认值“0”的原因。

suggestion : 建议:

if(index>=0 && index<text.length()) 
        {
        System.out.println("The character you asked for is: " + text.charAt(index));
        letter = text.charAt(index));
        return letter;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM