To install StudyMoose App tap and then “Add to Home Screen”
Save to my list
Remove from my list
The objective of this lab assignment is to create a Java program that removes all non-alpha characters (anything that is not a letter) from a given input string. The program should prompt the user to input a sentence or any string, process the input, and display the result with non-alpha characters removed.
In this lab assignment, we will develop a Java program with the following key components:
The main program interacts with the user, reads the input string, processes it to remove non-alpha characters, and then displays the resulting string.
It follows these steps:
Scanner
class.result
to store the filtered output.for
loop.
result
string.
import java.util.Scanner;
public class Lab2_2 {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
System.out.print("Please type in a string: ");
String input = stdin.nextLine();
String result = "";
char currentChar;
for (int i = 0; i < input.length(); i++) { currentChar = input.charAt(i); if (currentChar >= 'A' && currentChar <= 'z') {
result += currentChar;
}
}
System.out.println("The result is: " + result);
}
}
The program successfully removes all non-alpha characters from the input string and displays the filtered result.
This ensures that only letters (both uppercase and lowercase) are retained in the output.
This lab assignment demonstrates the use of string manipulation and character checking in Java. By iterating through each character in the input string and verifying whether it is an alpha character, the program effectively filters out unwanted characters. This can be useful in various applications where text processing is required.
Let's consider an example to illustrate the functionality of the program. If the user inputs the following string:
-Hello, 1 world$!
The program will process it and produce the following output:
The result is: Helloworld
In conclusion, this lab assignment has provided practical experience in Java programming by creating a program that removes non-alpha characters from a given input string. The program successfully accomplishes this task and can be used for cleaning and processing text data. It showcases the use of loops, conditional statements, and string manipulation techniques in Java.
1. Java Scanner Class Documentation: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Scanner.html
Lab Assignment Report - Remove All Non Alpha Characters. (2023, Aug 04). Retrieved from https://studymoose.com/document/comp-182-lab-assignment-2-2-remove-all-non-alpha-characters
👋 Hi! I’m your smart assistant Amy!
Don’t know where to start? Type your requirements and I’ll connect you to an academic expert within 3 minutes.
get help with your assignment