Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8233321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:05:23+00:00 2026-06-07T18:05:23+00:00

Basically what I’m trying to do is take a String, and replace each letter

  • 0

Basically what I’m trying to do is take a String, and replace each letter in the alphabet inside, but preserving any spaces and not converting them to a “null” string, which is the main reason I am opening this question.

If I use the function below and pass the string “a b”, instead of getting “ALPHA BETA” I get “ALPHAnullBETA”.

I’ve tried all possible ways of checking if the individual char that is currently iterated through is a space, but nothing seems to work. All these scenarios give false as if it’s a regular character.

public String charConvert(String s) {

    Map<String, String> t = new HashMap<String, String>(); // Associative array
    t.put("a", "ALPHA");
    t.put("b", "BETA");
    t.put("c", "GAMA");
    // So on...

    StringBuffer sb = new StringBuffer(0);
    s = s.toLowerCase(); // This is my full string

    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);

        String st = String.valueOf(c);
        if (st.compareTo(" ") == 1) {
            // This is the problematic condition
            // The script should just append a space in this case, but nothing seems to invoke this scenario
        } else {
            sb.append(st);
        }

    }

    s = sb.toString();

    return s;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T18:05:24+00:00Added an answer on June 7, 2026 at 6:05 pm

    compareTo() will return 0 if the strings are equal. It returns a positive number of the first string is “greater than” the second.

    But really there’s no need to be comparing Strings. You can do something like this instead:

    char c = s.charAt(i);
    
    if(c == ' ') {
        // do something
    } else {
        sb.append(c);
    }
    

    Or even better for your use case:

    String st = s.substring(i,i+1);
    if(t.contains(st)) {
        sb.append(t.get(st));
    } else {
        sb.append(st);
    }
    

    To get even cleaner code, your Map should from Character to String instead of <String,String>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
Basically i'd like to allow an arbitrary (but not empty ) number of key-value
Basically I am trying to search the page when it loads for the string
basically I want to validate whether or not a text box is a certain
Basically what I'm trying to do is make a very simple vertical bullet projectile
Basically my sign up form is not working. When I sign someone up it
Basically my question is, if I have a number string, and i'm going to
Basically, each time I click on a graphicsview, I'd like a new QGraphicsEllipseItem to
Basically I'm trying to improve on the Ghosts in a Pacman game I'm making.
Basically i'm trying to build a comment system. The user looks at a photo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.