Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Java

  1. #1
    New Coder
    Join Date
    Mar 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java

    i have a code:

    Code:
    System.out.print(strWord.substring(0,1));
    for (intCounter = 1; intCounter <= intStringLength-1; intCounter++){
    					
    intCounter2 = intCounter + 1;
    strSubFinalWord = strWord.substring(intCounter,intCounter2);
    strFinalWord = "-" + strSubFinalWord;			
    					
    System.out.print(strFinalWord);
    }
    System.out.println("");
    is it possible to not do a lot of system.out.println and system.out.print n make a string that would equal to the final product of the above
    Last edited by Aonlan; 03-14-2005 at 06:07 AM.

  • #2
    Regular Coder
    Join Date
    Feb 2004
    Location
    WA
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sure..
    Code:
    strWordOutput = strWord.substring(0,1);
    for (intCounter = 1; intCounter <= intStringLength-1; intCounter++){
        intCounter2 = intCounter + 1;
        strSubFinalWord = strWord.substring(intCounter,intCounter2);
        strWordOutput = strWordOutput + "-" + strSubFinalWord;		
    }
    There might be a better way to do this. The word you want is in strWordOutput.
    Nobody is Perfect. I am Nobody.

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kentucky
    Posts
    133
    Thanks
    0
    Thanked 1 Time in 1 Post
    You could also use a StringBuffer in java.util:

    Code:
    StringBuffer buffer = new StringBuffer();
    
    buffer.append(strWord.substring(0,1));
    
    for (intCounter = 1; intCounter <= intStringLength-1; intCounter++)
    {
         intCounter2 = intCounter + 1;
         buffer.append("-" + strWord.substring(intCounter,intCounter2));
    					
         System.out.print(buffer.toString());
    }

  • #4
    New Coder
    Join Date
    Mar 2005
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    NIO's CharBuffer would work in the case as well.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •