Solved

Java String Exercise

Posted on 2016-07-26
11
22 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
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Some pointers:

String.toUpperCase
String.replace
StringBuilder.reverse
2
 

Author Comment

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

Author Comment

by:David King
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Accepted Solution

by:
David King earned 0 total points
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
Too late with my replaceAll suggestion :-(
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
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
Comment Utility
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
Comment Utility
Figured it out.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
network + 7 73
mapShare challenge 13 67
Modeling a class in java 5 31
Requested array size exceeds VM limit 3 44
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 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

12 Experts available now in Live!

Get 1:1 Help Now