0% found this document useful (0 votes)
5 views9 pages

File Handling Menu Java

The document outlines the development of two Java console applications: one for string manipulation and another for file handling. The string manipulation application allows users to perform operations like length calculation, case conversion, and substring checking through a menu interface. The file handling application enables users to create, read, write, append, and delete text files, also using a menu-driven approach.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

File Handling Menu Java

The document outlines the development of two Java console applications: one for string manipulation and another for file handling. The string manipulation application allows users to perform operations like length calculation, case conversion, and substring checking through a menu interface. The file handling application enables users to create, read, write, append, and delete text files, also using a menu-driven approach.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

String Manipulation

Aim
To develop a Java console application that performs various string manipulation operations such
as length calculation, case conversion, reversal, character replacement, substring checking, and
concatenation through a user-interactive menu.

Algorithm
1. Start the program and declare necessary variables and a `Scanner` object to take input
from the user.
2. Display a menu inside a loop to offer various string manipulation choices.
3. perform the appropriate operation using `switch-case`. For each case, implement a string
operation like length, uppercase, lowercase, etc.
4. If the user chooses to find the length, use `inputString.length()` and display the result. For
case conversions, use `toUpperCase()` and `toLowerCase()` methods respectively.
5. For reversing the string, use `StringBuilder(inputString).reverse()` to generate the output.
For replacing a character, prompt the user to enter both the old and new character, then
use `replace()`.
6. To check for a substring, ask the user for a substring and use `contains()` to verify its
presence. For concatenation, get a new string and append it to the original using `+`.
7. If the user chooses to exit, display a goodbye message and break the loop.

Source Code
Import java.util.Scanner;

Public class SimpleStringManipulation {

Public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

String inputString;

System.out.print(“Enter a string: “);

inputString = scanner.nextLine();

int choice;

do {

System.out.println(“\n===== STRING MANIPULATION MENU =====”);

System.out.println(“1. Find Length of String”);


System.out.println(“2. Convert to UPPERCASE”);

System.out.println(“3. Convert to lowercase”);

System.out.println(“4. Reverse String”);

System.out.println(“5. Replace a Character”);

System.out.println(“6. Check for Substring”);

System.out.println(“7. Concatenate Another String”);

System.out.println(“8. Exit”);

System.out.print(“Enter your choice: “);

Choice = scanner.nextInt();

Scanner.nextLine(); // clear buffer

Switch (choice) {

Case 1:

System.out.println(“Length: “ + inputString.length());

Break;

Case 2:

System.out.println(“Uppercase: “ + inputString.toUpperCase());

Break;

Case 3:

System.out.println(“Lowercase: “ + inputString.toLowerCase());

Break;

Case 4:

System.out.println(“Reversed: “ + new StringBuilder(inputString).reverse());

Break;

Case 5:

System.out.print(“Enter character to replace: “);

Char oldChar = scanner.next().charAt(0);


System.out.print(“Enter new character: “);

Char newChar = scanner.next().charAt(0);

Scanner.nextLine(); // clear buffer

System.out.println(“After replacement: “ + inputString.replace(oldChar, newChar));

Break;

Case 6:

System.out.print(“Enter substring to check: “);

String sub = scanner.nextLine();

System.out.println(“Contains \”” + sub + “\”: “ + inputString.contains(sub));

Break;

Case 7:

System.out.print(“Enter string to concatenate: “);

String another = scanner.nextLine();

System.out.println(“Concatenated string: “ + inputString + another);

Break;

Case 8:

System.out.println(“Exiting… Goodbye!”);

Break;

Default:

System.out.println(“Invalid choice. Try again.”);

} while (choice != 8);

Scanner.close();

Sample Output
Enter a string: HelloWorld

===== STRING MANIPULATION MENU =====

1. Find Length of String


2. Convert to UPPERCASE
3. Convert to lowercase
4. Reverse String
5. Replace a Character
6. Check for Substring
7. Concatenate Another String
8. Exit

Enter your choice: 1

Length: 10

===== STRING MANIPULATION MENU =====

Enter your choice: 2

Uppercase: HELLOWORLD

===== STRING MANIPULATION MENU =====

Enter your choice: 4

Reversed: dlroWolleH

===== STRING MANIPULATION MENU =====

Enter your choice: 5

Enter character to replace: o

Enter new character: a

After replacement: HellaWarld

===== STRING MANIPULATION MENU =====

Enter your choice: 6

Enter substring to check: Hello

Contains “Hello”: true

===== STRING MANIPULATION MENU =====

Enter your choice: 7


Enter string to concatenate: Java

Concatenated string: HelloWorldJava

===== STRING MANIPULATION MENU =====

Enter your choice: 8

Exiting… Goodbye!

File Handling
Aim:
To develop a Java console-based file handling menu application that allows users to create, read,
write, append, and delete a text file using standard file operations.

Algorithm:
1. Start the program and initialize required objects like Scanner and define the file name. Set up a
loop to display the menu until the user chooses to exit.
2. Display a menu with numbered options for file operations like create, write, read, append,
delete, and exit.
3. Use a switch-case structure to handle the user's choice. Call the appropriate method based on
the selected option.
4. For file creation, check if the file already exists. If not, create a new file and display a success
message.
5. For writing, prompt the user for content. Overwrite existing file content with the new input
using FileWriter.
6. For reading, check if the file exists and read its contents line by line. Display the file contents
on the console.
7. For appending, add new content at the end of the file. For deletion, remove the file if it exists;
otherwise, show an error.

Source Code:

import java.io.*;
import java.util.Scanner;

public class FileHandlingMenu {


static final String FILE_NAME = "sample.txt";
static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {


int choice;

do {
displayMenu();
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine(); // Clear the buffer

switch (choice) {
case 1 -> createFile();
case 2 -> writeFile();
case 3 -> readFile();
case 4 -> appendToFile();
case 5 -> deleteFile();
case 6 -> System.out.println("Exiting program. Goodbye!");
default -> System.out.println("Invalid choice! Please try again.");
}

System.out.println(); // Line space after operation


} while (choice != 6);

scanner.close();
}

public static void displayMenu() {


System.out.println("===== FILE HANDLING MENU =====");
System.out.println("1. Create File");
System.out.println("2. Write to File");
System.out.println("3. Read from File");
System.out.println("4. Append to File");
System.out.println("5. Delete File");
System.out.println("6. Exit");
}

public static void createFile() {


try {
File file = new File(FILE_NAME);
if (file.createNewFile()) {
System.out.println(" File created: " + file.getName());
} else {
System.out.println(" File already exists.");
}
} catch (IOException e) {
System.out.println(" Error while creating the file.");
e.printStackTrace();
}
}

public static void writeFile() {


try {
FileWriter writer = new FileWriter(FILE_NAME);
System.out.print("Enter content to write to the file: ");
String content = scanner.nextLine();
writer.write(content);
writer.close();
System.out.println(" Successfully wrote to the file.");
} catch (IOException e) {
System.out.println(" Error while writing to the file.");
e.printStackTrace();
}
}

public static void readFile() {


try {
File file = new File(FILE_NAME);
if (!file.exists()) {
System.out.println(" File does not exist.");
return;
}
Scanner fileReader = new Scanner(file);
System.out.println(" File Content:");
while (fileReader.hasNextLine()) {
System.out.println(fileReader.nextLine());
}
fileReader.close();
} catch (FileNotFoundException e) {
System.out.println(" File not found.");
e.printStackTrace();
}
}

public static void appendToFile() {


try {
FileWriter writer = new FileWriter(FILE_NAME, true);
System.out.print("Enter content to append to the file: ");
String content = scanner.nextLine();
writer.write("\n" + content);
writer.close();
System.out.println(" Content appended successfully.");
} catch (IOException e) {
System.out.println(" Error while appending to the file.");
e.printStackTrace();
}
}

public static void deleteFile() {


File file = new File(FILE_NAME);
if (file.exists() && file.delete()) {
System.out.println(" File deleted: " + file.getName());
} else {
System.out.println(" Failed to delete file or file does not exist.");
}
}
}

Sample Output:

===== FILE HANDLING MENU =====


1. Create File
2. Write to File
3. Read from File
4. Append to File
5. Delete File
6. Exit
Enter your choice: 1
File created: sample.txt

===== FILE HANDLING MENU =====


Enter your choice: 2
Enter content to write to the file: Hello World
Successfully wrote to the file.

===== FILE HANDLING MENU =====


Enter your choice: 3
File Content:
Hello World

===== FILE HANDLING MENU =====


Enter your choice: 4
Enter content to append to the file: This is Java.
Content appended successfully.

===== FILE HANDLING MENU =====


Enter your choice: 5
File deleted: sample.txt

You might also like