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 9236091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:12:21+00:00 2026-06-18T07:12:21+00:00

Possible Duplicate: How do I compare strings in Java? I’m fairly new to Java

  • 0

Possible Duplicate:
How do I compare strings in Java?

I’m fairly new to Java and to practice I’m trying to create a hexadecimal to decimal number converter since I’ve successfully managed to make a binary to decimal converter.

The problem I’m having is basically comparing a given character of in a String with another string. This is how I define the current character that is to be compared:

String current = String.valueOf(hex.charAt(i));

This is how I try to compare the character:

else if (current == "b") 
   dec += 10 * (int)Math.pow(16, power);

When I try to run the code by entering just numbers e.g. 12, it works but when I try to use a ‘b’, I get a weird error. Here is the entire result of running the program:

run:
Hello! Please enter a hexadecimal number.
2b
For input string: "b" // this is the weird error I don't understand
BUILD SUCCESSFUL (total time: 1 second)

Here is an example of successfully running the program with just a number conversion:

run:
Hello! Please enter a hexadecimal number.
22
22 in decimal: 34 // works fine
BUILD SUCCESSFUL (total time: 3 seconds)

Any help with this would be appreciated, thanks.

Edit: I think it will be useful if I put the entire method here.

Edit 2: SOLVED! I don’t know who’s answer that I should accept though because they were all so good and helpful. So conflicted.

for (int i = hex.length() - 1; i >= 0; i--) {
        String lowercaseHex = hex.toLowerCase();
        char currentChar = lowercaseHex.charAt(i);

        // if numbers, multiply them by 16^current power
        if (currentChar == '0' || 
                currentChar == '1' || 
                currentChar == '2' || 
                currentChar == '3' || 
                currentChar == '4' || 
                currentChar == '5' || 
                currentChar == '6' || 
                currentChar == '7' || 
                currentChar == '8' || 
                currentChar == '9')
            // turn each number into a string then an integer, then multiply it by
            // 16 to the current power.
            dec += Integer.valueOf(String.valueOf((currentChar))) * (int)Math.pow(16, power);

        // check for letters and multiply their values by 16^current power
        else if (currentChar == 'a') 
            dec += 10 * (int)Math.pow(16, power);
        else if (currentChar == 'b') 
            dec += 11 * (int)Math.pow(16, power);
        else if (currentChar == 'c') 
            dec += 12 * (int)Math.pow(16, power);
        else if (currentChar == 'd') 
            dec += 13 * (int)Math.pow(16, power);
        else if (currentChar == 'e') 
            dec += 14 * (int)Math.pow(16, power);
        else if (currentChar == 'f') 
            dec += 15 * (int)Math.pow(16, power);
        else
            return 0;
        power++; // increment the power
    }

    return dec; // return decimal form
}
  • 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-18T07:12:22+00:00Added an answer on June 18, 2026 at 7:12 am

    Try initializing a char to the char value returned by charAt()

    char current = hex.charAt(i);
    

    Then in your conditional use the literal char:

    else if (current == 'b') 
    

    Since char is a primitive you can compare it using the == operator. In the former code you were comparing a String using ==, since a String is an Object the code is checking to see if they are the same Object not if they have the same value as the String.equals() method would.

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

Sidebar

Related Questions

Possible Duplicate: How do I compare strings in Java? im new to android and
Possible Duplicate: How do I compare strings in Java? I am new to Java
Possible Duplicate: How do I compare strings in Java? I'm new to Java. And
Possible Duplicate: How do I compare strings in Java? I am trying to compare
Possible Duplicate: How do I compare strings in Java? I'm using String.valueOf to convert
Possible Duplicate: How do I compare strings in Java? java == vs equals() confusion
Possible Duplicate: How do I compare strings in Java? I have the following code
Possible Duplicate: How do I compare strings in Java? Java String.equals versus == I
Possible Duplicate: How do I compare strings in Java? why equals() method when we
Possible Duplicate: How do I compare strings in Java? String Comparison in Java…? I'm

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.