Solved

Jave - Remove carriage return + special characters from text

Posted on 2016-09-01
3
80 Views
Last Modified: 2016-09-03
I am writing a code in java that takes text then remove all the Punctuations (, blanks - new line and numerals) all special character and just leave the A to Z letters. it works fine until gets to the first carriage return and stopped. I tried many version of replaceAll but it did not work, please help!

for example

IHN EMATMG EECNIWEA RSHI A RESOEF ES RLTCMEE-COEAACIROH TLNHR PIRMOA ECSHENEV CEDIAODED ULS NPHD TN EAE REIIY-MO TWL-EDTHTTEEN NTCIPRO TO TUERYMT MORCCIECLL,


PIMAATODMC DNL IITIAMRO CUNAIMYNAOINI.
then I get:

IHNEMATMGEECNIWEARSHIARESOEFESRLTCMEECOEAACIROHTLNHRPIRMOAECSHENEVCEDIAODEDULSNPHDTNEAEREIIYMOTWLEDTHTTEENNTCIPROTOTUERYMTMORCCIECLL

 package Cipher1;

import java.util.Scanner;

public class StripCipher
{
    public static void main(String[] args)
    {
        // Take the input of the encrypted text from the user.
        System.out.println(" Enter the cipher text : ");
        Scanner ScanText = new Scanner(System.in);
        String OriginalCipherText = ScanText.nextLine();

        // Eliminate the wide space and special characters present in the input
        // text.
        String CipherText = OriginalCipherText.replaceAll("\\s+", "");
        CipherText = OriginalCipherText.replaceAll("[^a-zA-Z]+", "");
        System.out.println(" Striped Cipher text is : " + CipherText);

        // Calculate the length of the text.
        int CipherTextLength = CipherText.length();
        System.out.println(" Lenght of the cipher text is : " + CipherTextLength);
    }
}

Open in new window


for clarifications I used the following but none of them work:

replaceAll("[\n\r]", ""); 
replaceAll("\\r|\\n", "")
replaceAll("[^\\w\\s]",""); 
replaceAll("[^\\p{L}\\p{Z}]","");

Open in new window

0
Comment
Question by:Aaeshah
3 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 41781323
I believe the command:
        String OriginalCipherText = ScanText.nextLine();

Open in new window

is only getting the first line of text.

The only replaceAll pattern you should need is this, as it removes anything except for alpha characters:
replaceAll("[^a-zA-Z]","")

Open in new window


So you probably want to use something like:
while (OneLine = ScanText.hasNext())
        {
// append OneLine.replaceAll("[^a-zA-Z]", "") to CipherText
}
System.out.println(" Stripped Cipher text is : " + CipherText);

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 41781326
CipherText = OriginalCipherText.replaceAll("[^a-zA-Z]+", "");

Open in new window

No reason why that should not work. It can't contain carriage returns btw, since readLine will remove them
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41782619
So looks like you are copying and pasting into your ScanText Scanner (which should be scanText btw for convention), which will not work for reasons the other experts mention.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tomcat administrtor 12 46
copying evtx files while system is running 2 58
SQL server 2008 and after encryption method 32 46
What's wrong with this code? 4 23
The 21st century solution to antiquated pagers.
How do we balance the user experience (UX) with reasonable security measures? It can be done, if you keep these fundamentals in mind.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question