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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:12:04+00:00 2026-06-08T16:12:04+00:00

I have a string entered by user 25|35|40 . I have format any number

  • 0

I have a string entered by user 25|35|40. I have format any number below 100=25+35+40 to this format. For instance,

5  --> 05|00|00
30 --> 25|05|00
65 --> 25|35|05

The order of 25|35|40 should be maintained. This is similar to date formatting MM:dd:yyyy, in that we know month cannot exceed 12 and date cannot exceed 31. but here any value can occur. the value in its position determine the maximum value. Another example,

If the user string is 40|110|2500|350

5    --> 05|000|0000|000
100  --> 40|060|0000|000
450  --> 40|110|0300|000
2900 --> 40|110|2500|250

If the number exceeds the total 3000=40+110+2500+350, I can make it as number -= 3000.
Currently I am trying to format it using custom code which will check for the number and will create the required output string. Is there any inbuilt format API available in java?

  • 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-08T16:12:06+00:00Added an answer on June 8, 2026 at 4:12 pm

    There is no public API for such a specific use case. This is one way to solve it:

    int num = 65;
    int i1 = num > 25 ? 25 : num;
    int i2 = num < 25 ? 0 : num > 60 ? 35 : num - 25;
    int i3 = num < 60 ? 0 : num - 60;
    System.out.format("%02d|%02d|%02d", i1, i2, i3);
    

    prints

    25|35|05
    

    Or, to generalise it:

    public static String format(String format, int num) {
        String[] split = format.split("\\|");
        int numLeft = num;
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            int boundary = Integer.parseInt(split[i]);
            int number = Math.min(numLeft, boundary);
            if (result.length() > 0) {
                result.append('|');
            }
            result.append(leftPad(number, split[i].length()));
            numLeft = Math.max(0, numLeft - boundary);
        }
        return result.toString();
    }
    
    private static String leftPad(int number, int length) {
        StringBuilder sb = new StringBuilder();
        sb.append(number);
        while (sb.length() < length) {
            sb.insert(0, '0');
        }
        return sb.toString();
    }
    
    public static void main(String[] args) {
        String result = format("25|35|40", 65);
        System.out.println(result);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a search method that takes in a user-entered string, splits it at
I need all records which have year entered from search criteria. for ex: String
I have string that look like Array that fetched from other webservice like this
I have string as abvd.qweqw.sdfs.a=aqwrwewrwerrew . I need to parse this string and get
I have string like this : <input name=my_name id=my_id value=my_value class=my_class /> I would
I have string like this: G:\Projects\TestApp\TestWeb\Files\Upload\file.jpg How can I remove all text before Files
I want to display the first and last characters of any given string entered
I have this set of codes $user = htmlentities($_POST['user']); $pass = htmlentities($_POST['pass']); function sha512($str)
I have this property value and i need its string value as its being
I am testing a website very similar to Google. So, if a user entered

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.