Java プロジェクトで別のクラスからメソッドを呼び出すにはどうすればよいですか? 2 つの異なるクラスがあり、それぞれが同じ Java プロジェクトにあります。最初のクラスの名前は Applications で、2 番目のクラスの名前は ShowChar です。これらのクラスは、それぞれ同じ Java プロジェクトにあります。私がしなければならないことは、ユーザーから文字列入力を取得することです。次に、ユーザーから整数を取得する必要があります。次に、ユーザーが選択した整数に含まれる文字をユーザーに伝える必要があります。私はそれをすべてやった。それが ShowChar クラスの機能ですが、私がすべきことは、Applications クラスの ShowChar クラスからメソッドを呼び出すことです。これを機能させることはできません。助けてください。
これが私の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;
}
}`
これが、アプリケーションクラスで理解したものです-
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) );
}
}
一番下に、見つけた手紙をコンソールに出力することになっていますが、うまくいかないようです。入力を使用してプログラムを実行すると、常に System.out.println ステートメントに「文字が見つかりました: 0」というメッセージが返されます。