Solved

Jave - Remove carriage return + special characters from text

Posted on 2016-09-01
3
73 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Should localization be done inside spring controller 5 13
servlet example issue 6 33
arguments to jar 5 18
CPU at 100% usage, why? 27 88
Big data transfers via information superhighways require special attention and protection. Learn more about the IT-regulations of the country where your server is located. Analyze cloud providers and their encryption systems for safe data transit. S…
Three simple tips to quickly and efficiently back up and protect the contents of your PC and Mac®.
The viewer will learn how to implement Singleton Design Pattern in Java.
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now