Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Jave - Remove carriage return + special characters from text

Posted on 2016-09-01
3
Medium Priority
?
129 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 2000 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

A look at what happened in the Verizon cloud breach.
Let's recap what we learned from yesterday's Skyport Systems webinar.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This videā€¦

715 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