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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:33:41+00:00 2026-06-06T06:33:41+00:00

I have to parse a String that can assume hex values or other non-hex

  • 0

I have to parse a String that can assume hex values or other non-hex values

0xff, 0x31 or A, PC, label, and so on.

I use this code to divide the two cases:

String input = readInput();

try {
    int hex = Integer.decode(input);            
    // use hex ...

} catch (NumberFormatException e) {
   // input is not a hex, continue parsing
}

Can this code be considered “ugly” or difficult to read? Are there other (maybe more elegant) solutions?

EDIT : I want to clarify that (in my case) a wrong input doesn’t exist: i just need to distinguish if it is a hex number, or not.
And just for completeness, i’m making a simple assebler for DCPU-16.

  • 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-06T06:33:43+00:00Added an answer on June 6, 2026 at 6:33 am

    Exception handling is an integral part (and one of the design goals) of the Java programming language… you shouldn’t cast them off just because you think they are "ugly".

    That said, if you want a simple and readable way to handle NumberFormatExceptions, you might consider using the NumberUtils class instead.

    The toInt(String str, int defaultValue) method converts a String to an int, returning a default value if the conversion fails. If the string is null, the default value is returned.

     NumberUtils.toInt(null, 1) = 1
     NumberUtils.toInt("", 1)   = 1
     NumberUtils.toInt("1", 0)  = 1
    

    The method encapsulates exception catching and handling, as seen in the source code below. As a result, the client only needs to make a single method call.

    public static int toInt(String str, int defaultValue) {         
        if(str == null) {
            return defaultValue;
        }
        try {
            return Integer.parseInt(str);
        } catch (NumberFormatException nfe) {
            return defaultValue;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web method that returns a String[][] and I'm trying to parse
I have string as abvd.qweqw.sdfs.a=aqwrwewrwerrew . I need to parse this string and get
I have a static method like this public static string DoSomethingToString(string UntrustedString) { //parse
I have a string that looks something like this: my name is: andrew I'd
I have to parse a string. It contains one or two numbers. It can
Trying to write a short method so that I can parse a string and
The issue I have is that I want to parse strings in order, so
I have to parse a String to create a PathSegmentCollection . The string is
I need to parse a string from a datatable into a double. I have
I have some sort of recursive function, but I need to parse a string,

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.