Solved

Java String Exercise

Posted on 2016-07-26
11
43 Views
Last Modified: 2016-07-31
I am working on an exercise in JAVA 1.8 that goes like this.
I have a String : "Able was I, ere I saw Elba."
I need to remove spaces and punctuation and capitalize it all.
Then I need to save a copy of it in reverse order and compare the two to see if they are the same.

Does anyone have a suggestion on how they might achieve this ?
I was thinking along the lines of a regex /[a-zA-Z]+/g    as a possibility. But I don't know how to extract the words using a RegEx.
The result of the extraction would have the above expression returned as "ABLEWASIEREISAWELBA" .

Thanks,

Dave
0
Comment
Question by:David King
[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
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 41729398
Some pointers:

String.toUpperCase
String.replace
StringBuilder.reverse
2
 

Author Comment

by:David King
ID: 41729424
Thanks, I'll research the API docs on those.
0
 

Author Comment

by:David King
ID: 41729504
I have tested my RegEx /[a-zA-Z]+/g on my test string and it selects all of the words correctly. Is there a String class method that I can use that will allow me to use my RegEx expression to select the Words in my String and Return them back as a string, leaving the un-selected text behind?
?
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 86

Expert Comment

by:CEHJ
ID: 41729565
I have tested my RegEx /[a-zA-Z]+/g on my test string and it selects all of the words correctly.
How and where?
Is there a String class method that I can use that will allow me to use my RegEx expression to select the Words in my String and Return them back as a string
That's not really how regex works. You need to think about replacement
0
 

Author Comment

by:David King
ID: 41729600
I am trying to use the String.replace(regex , string) method to get the job done.

Like this;

public class PalCode
{

    public PalCode()
    {
        String palString = new String("Able was I, ere I saw Elba."); // original input String
        String workedString = new String(""); // String after manipulation
        workedString = palString.replace("[^a-zA-Z]+/g" , "");
        System.out.println("Manipulated string result is: " + workedString);

    }
}// end class PalCode

The regex expression should match character that are NOT a-z or A-Z and replace them with "nothing".
The program runs and compiles without any errors but the regex has NO effect. ??
0
 

Accepted Solution

by:
David King earned 0 total points
ID: 41729612
OK, I got it. This works.

public class PalCode
{

    public PalCode()
    {
        String palString = new String("Able was I, ere I saw Elba."); // original input String
        String workedString = new String(""); // String after manipulation
        workedString = palString.replaceAll("[^a-zA-Z]+" , "").toUpperCase();
        System.out.println("Manipulated string result is: " + workedString);

    }
}// end class PalCode


("Able was I, ere I saw Elba."); // original input String
Manipulated string result is: ABLEWASIEREISAWELBA
0
 
LVL 32

Expert Comment

by:awking00
ID: 41729632
The String replace method will replace a char sequence or array. I think you should look at the replaceAll method which takes a regular expression as its first argument.
0
 
LVL 32

Expert Comment

by:awking00
ID: 41729634
Too late with my replaceAll suggestion :-(
0
 
LVL 32

Expert Comment

by:awking00
ID: 41729645
Now you just need to create a string that's the reverse of the string you have and compare them to complete your task.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41729717
When you come back again with your next question, be great if you could stick your code in the code formatting delimiters. ;)
1
 

Author Closing Comment

by:David King
ID: 41736374
Figured it out.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mysql jsp example issue 32 96
add projects t working set in maven 2 70
Need a simple RegEx to search for two words 27 124
Running JavaFX on the Raspberry Pi 27 290
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

739 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