This is what I have...
{
public static void main(String[] args)
{
//variable declaration
char letter;
getLetter();
letter = "";
System.out.println(letter)
}
public static int getLetter()
{
String text;
char letter;
text = JOptionPane.showInputDialog("Enter a letter.");
letter = text.charAt(0);
System.out.println(letter);
return letter;
}
}
I want to get the letter the user inputs from the method getLetter and transfer it in the main method where I can display it on the screen. What am I doing wrong here?
The getLetter()
method should return type char
not int
. This is because you have assigned the local variable letter
as type char
.
Also, methods are called by method();
not '(method)'
.
Try out the following code:
public class YourClass {
public static void main(String[] args) {
//variable declaration
char letter;
letter = getLetter();
System.out.println(letter);
}
public static char getLetter() {
String text;
char letter;
text = JOptionPane.showInputDialog("Enter a letter.");
letter = text.charAt(0);
System.out.println(letter);
return letter;
}
}
The return type for your method is an int, but you're returning a character. Your method invocation is incorrect as well; it should be:
letter = getLetter(); // follows the same format as the declaration.
You should make use of the Java Tutorials, they can be found here: http://docs.oracle.com/javase/tutorial/
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.