Question

Write a method named showChar. The method should accept two arguments: a reference to a Stringobject...

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↵

Homework Answers

Answer #1

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);

}

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write in C++ program and write a function named add. It should accept two const double...
Write in C++ program and write a function named add. It should accept two const double type references as arguments. The first argument should be named num1; the second argument should be named num2. The function should return by value of num1 plus num2.
Write in C++ program and write a function named computeCostPlusGST. It should accept two double type...
Write in C++ program and write a function named computeCostPlusGST. It should accept two double type variable as arguments. The first argument should be named cost; the second argument should be named rate. The argument rate should have a default value of 0.15. The function should return by value the value of cost plus the product of cost and rate
Write Java code that attempts to call a method named bootUp() which takes a String parameter...
Write Java code that attempts to call a method named bootUp() which takes a String parameter and returns an integer value. The String parameter should be read from a file named "bootConfig.txt" and is the only value in the file. If a BootUpException is thrown, print the Exception object's message. If a DriverException occurs, call the reboot() method and rethrow the original DriverException object. If any other type of Exception is thrown, print a generic error message to the console....
Write a method that accepts a String object as an argument and displays its contents backward....
Write a method that accepts a String object as an argument and displays its contents backward. For instance, if the string argument is "gravity" the method should display "ytivarg". Demonstrate the method in a program that asks the user to input a string and then prints out the result of passing the string into the method. Sample Run java BackwardString Enter·a·string:Hello·world↵ dlrow·olleH↵
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
COMPLETE IN C++ Declare and initialize a global constant named SIZE with the value 50. Write...
COMPLETE IN C++ Declare and initialize a global constant named SIZE with the value 50. Write a void function called count that accepts two parameters-- a C-string called str representing a C-string passed to the function and an array of integers called alphabets to store the count of the letters of the C-string. Note that the alphabets array stores 26 counters – one for each letter of the alphabet. Use the same counter for lowercase and uppercase characters. The first...
Write a Python program named lastNameVolumes that finds the volumes of different 3 D objects such...
Write a Python program named lastNameVolumes that finds the volumes of different 3 D objects such as a cube, sphere, cylinder and cone. In this file there should be four methods defined. Write a method named cubeVolFirstName, which accepts the side of a cube in inches as an argument into the function. The method should calculate the volume of a cube in cubic inches and return the volume. The formula for calculating the volume of a cube is given below....
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT