Solved

Jave - Remove carriage return + special characters from text

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

758 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

18 Experts available now in Live!

Get 1:1 Help Now