The document is a Java program that presents a menu with two options: replacing a character in a string with a special symbol or forming a pattern based on a given string. The program uses a Scanner to take user input and performs actions based on the user's choice. It includes error handling for invalid menu selections.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
7 views1 page
Question 10
The document is a Java program that presents a menu with two options: replacing a character in a string with a special symbol or forming a pattern based on a given string. The program uses a Scanner to take user input and performs actions based on the user's choice. It includes error handling for invalid menu selections.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1
/**
NAME : ABHIGYAN DAS
CLASS : X SECTION : G ROLL NUMBER : 1 UID : 8525562 */ import java.util.Scanner;// Importing Scanner public class Question10 // Class Declared { public static void main(String[] args) // Main method of the program { Scanner in = new Scanner(System.in); // Display the menu System.out.println("Menu:"); System.out.println("a) Replace a character with a special symbol"); System.out.println("b) Accept a string and form a pattern"); System.out.print("Enter your choice (a/b): "); char c = in.next().charAt(0); in.nextLine(); // To consume the newline character switch (c) { case 'a':// First case { // Replace a character with a special symbol System.out.print("Enter a string: "); String iS = in.nextLine(); System.out.print("Enter the character to replace: "); char cR = in.next().charAt(0); System.out.print("Enter the special symbol: "); char sS = in.next().charAt(0); // Replace the specified character with the special symbol String mS = iS.replace(cR, sS); System.out.println("Output: " + mS); } break; case 'b':// Second case { // Form a pattern based on the string System.out.print("Enter a string: "); String S = in.nextLine(); System.out.println("Pattern:"); for (int i = 0; i < S.length(); i++) { for (int j = 0; j <= i; j++) { System.out.print(S.charAt(j) + " "); } System.out.println(); } } break; default:// Print invalid choice System.out.println("Invalid choice"); } } }