Write a method named showChar. The method should accept two
arguments: a reference to a Stringobject and an integer. The
integer argument is a character position within the String, with
the first character being at position 0.
When the method executes, it should display the character at that
character position. The method does not return anything.
Here is an example of a call to the method:
showChar("New York", 2);
In this call, the method will display the character w
because it is in position 2. Demonstrate the method in a complete
program.
Sample Run
java Method_showChar
Enter a line of text: The elevation of Mount Everest is 29,029 feet
(8,848·in·meters)↵
Enter yourindex:3 5↵
9↵
import java.util.Scanner;
public class Method_showChar {
public static void showChar(String a, int n)
{
System.out.println(a.charAt(n));
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a line of text: ");
String one = sc.nextLine();
System.out.print("Enter your index: ");
int n = sc.nextInt();
showChar(one, n);
}
}
Get Answers For Free
Most questions answered within 1 hours.