Lab Assignment Report - Remove All Non Alpha Characters

Categories: Technology

Objective

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.

Procedure

In this lab assignment, we will develop a Java program with the following key components:

Main Program

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:

  1. Prompt the user to input a string using the Scanner class.
  2. Initialize an empty string result to store the filtered output.
  3. Iterate through each character in the input string using a for loop.
    • Check if the current character is an alpha character (a letter) by comparing it with the ASCII values of 'A' to 'z'.
    • If the character is an alpha character, append it to the result string.
  4. Print the filtered result to the console.

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

Results

The program successfully removes all non-alpha characters from the input string and displays the filtered result.

Get to Know The Price Estimate For Your Paper
Topic
Number of pages
Email Invalid email

By clicking “Check Writers’ Offers”, you agree to our terms of service and privacy policy. We’ll occasionally send you promo and account related email

"You must agree to out terms of services and privacy policy"
Write my paper

You won’t be charged yet!

Get quality help now
Bella Hamilton
Bella Hamilton
checked Verified writer

Proficient in: Technology

star star star star 5 (234)

“ Very organized ,I enjoyed and Loved every bit of our professional interaction ”

avatar avatar avatar
+84 relevant experts are online
Hire writer

This ensures that only letters (both uppercase and lowercase) are retained in the output.

Discussion

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.

Example

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

Conclusion

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.

References

1. Java Scanner Class Documentation: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Scanner.html

Updated: Jan 03, 2024
Cite this page

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

Lab Assignment Report - Remove All Non Alpha Characters essay
Live chat  with support 24/7

👋 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