Solved

Jave - Remove carriage return + special characters from text

Posted on 2016-09-01
3
115 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

2017 Webroot Threat Report

MSPs: Get the facts you need to protect your clients.
The 2017 Webroot Threat Report provides a uniquely insightful global view into the analysis and discoveries made by the Webroot® Threat Intelligence Platform to provide insights on key trends and risks as seen by our users.

Question has a verified solution.

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

Article by: Justin
In light of the WannaCry ransomware attack that affected millions of Windows machines, you might wonder if your Mac needs protecting. Yes, it does and here is how to do it.
This process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

622 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