The Artima Developer Community
Sponsored Link

Java Answers Forum
simple question

3 replies on 1 page. Most recent reply: Sep 15, 2010 11:05 PM by George B

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
DM Seubert

Posts: 2
Nickname: poncaman41
Registered: Sep, 2010

simple question Posted: Sep 15, 2010 9:38 AM
Reply to this message Reply
Advertisement
I'm new to java and my question is simple: I have a string variable 35 characters in length. I want to modify the last 21 characters with the string representation of an int variable. How do I code the overwriting of the 21 characters in question so that the int variable (valid values are 1 to 125,000,000) is converted to string representation, does not destroy the integrity of the left-most 14 chars, and is right justified in the recipient 21 chars? Thanking you in advance for your help.


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: simple question Posted: Sep 15, 2010 1:59 PM
Reply to this message Reply
Why do you want to use Java to solve this problem?

DM Seubert

Posts: 2
Nickname: poncaman41
Registered: Sep, 2010

Re: simple question Posted: Sep 15, 2010 5:14 PM
Reply to this message Reply
because I'm learning java and I want to know how to do it in that language.

George B

Posts: 24
Nickname: hmgeorge
Registered: Feb, 2008

Re: simple question Posted: Sep 15, 2010 11:05 PM
Reply to this message Reply
    String overwrite21(String originalStr, int num) {
        if (originalStr.length() != 35)
            throw new IllegalArgumentException("wrong length");
        
        String intStr = String.format("%21d", num);
 
        return originalStr.substring(0, 14) + intStr;
    }

Flat View: This topic has 3 replies on 1 page
Topic: Thread and Process priority Previous Topic   Next Topic Topic: HELP! Design Review & Risk Management topics (in Java projects) required

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use