Padding String with spaces -tried known methods but code not working!

I am trying to figure out an easy way to pad a string with spaces at the end. The way that definitely works is a for loop where string = string + " "; until string is the length that I need. However, for several dozen strings this is a lot of unnecessary overhead. I've done some research and tried a few different things - yet none of them seem to actually pad the string.
import org.apache.commons.lang.StringUtils;
public class Test
     public static void main(String[] args)
          String one = "1234";
                //print string at current lenght
          System.out.println(one+"*");
                //try padding using String.format (in padRight method below)
          one = padRight(one,(8-one.length()));
          System.out.println(one+"*");
                one="1234";
                //try padding using StringUtils
          String tmp = StringUtils.rightPad(one, (8-one.length()));
          System.out.println(tmp+"*");
     private static String padRight(String s, int n)
          return String.format("%1$-" + n + "s", s);
}I've researched and these are the two most common methods people are suggesting. I am using java 1.5 so the String.format should work.. however, when I run it (I use JBuilder) the output is always "1234*".... after I try to pad it the output should be "1234 *" (with 4 spaces between the 4 and *) . Any suggestions is greatly appreciated! Thanks in advance.

Wow, I completely botched that... uncle_alice, thank you for clearing that up for me.
pbrockway2 - thank you, I actually did peruse the Formatter documentation - I would never post without researching something first... but I must have read it with one eye shut because walked away with the impression that I needed to submit the difference. I obviously misunderstood the documentation.
Thanks!

Similar Messages

  • TS1538 red dot glowing on proximity sensor of my iphone 4 and it is dead. it is not connecting with itunes. tried hard reset it's not working as well. also followed iphone troubleshooting assistant but also it's not working. what am i supposed to do now?

    red dot glowing on proximity sensor of my iphone 4 and it is dead. it is not connecting with itunes. tried hard reset it's not working as well. also followed iphone troubleshooting assistant but also it's not working. what am i supposed to do now?

    Make an appointment at the Genius Bar at your local Apple Store or take the phone into an Authorized Apple Service provider.

  • HT5622 I dowloand  ios7 but is not working FaceTime and iMessage.I tried turn off but still not working.

    I download IOS 7 I'm really dissapoint because my FaceTime and iMessage is not working.I tried turn off but is not working.:(

    Try resetting the SMC.
    MacBook and MacBook Pro with a battery you can remove
    Shut down the computer.
    Disconnect the MagSafe power adapter from the computer, if it's connected.
    Remove the battery.
    Press and hold the power button for 5 seconds.
    Release the power button.
    Reconnect the battery and MagSafe power adapter.
    Press the power button to turn on the computer
    To reset the SMC on newer Mac portables with a battery you cannot remove
    Shut down the computer.
    Plug in the MagSafe power adapter to a power source, connecting it to the Mac if its not already connected.
    On the built-in keyboard, press the (left side) Shift-Control-Option keys and the power button at the same time.
    Release all the keys and the power button at the same time.
    Press the power button to turn on the computer.  
    Note: The LED on the MagSafe power adapter may change states or temporarily turn off when you reset the SMC.

  • When i go in my settings on my ipod touch 3gen it says no wi-fi as in i cant connect to any wi-fi at all i tried to restore but did not work HELP PLEASE

    when i go in my settings on my ipod touch 3gen it says no wi-fi as in i cant connect to any wi-fi at all i tried to restore but did not work HELP PLEASE

    See:
    iOS: Wi-Fi or Bluetooth settings grayed out or dim
    If not successful, an appointment at the Genius Bar of an Apple store is usually in order.
    Apple Retail Store - Genius Bar

  • Trying to put pics on a flashdrive and keep getting error that there is not enough free space when there is.  I tried to empty trash with the flash drive in but still not working.  Help!

    Trying to copy photos in folder on desktop to a flash drive but keep getting error that there is not enough free space when there is.  I read to delete trash with the flash drive in but that still doesn't work.  Help!!

    How is the flash drive formatted? Open Disk Utility (Applications>Utilities) and see how it's formatted - that might be the problem.
    Clinton

  • JRE Uninstall string for v7 all the same - but do not work

    We have multiple Windows 7 machines running v7u1 thru v7u10.
    I found the uninstall string and was able to run MsiExec.exe /X{26A24AE4-039D-4CA4-87B4-2F83217010FF} on my own machine to remove the software successfully.
    In looking at u6,u7,u9 and u11 installations, I noticed the Uninstall Strings are exactly the same. But when ran on those machines, the system states "This action is only valid for products that are currently installed".
    However, I can remove the version from the Control Panel.
    So what are the correct uninstall strings for v7u1 thru v7u11?

    All of our users are local admins and tend to install whatever. This leads to a messy environment and we don't always know which version of Java is installed. So, to address that, I created a small script that scours 'HKLM\software\microsoft\windows\currentversion\uninstall' for Java entries, reads the uninstall string then runs the reported uninstall string. The assumption here is that the uninstall string will be an MSI command. (All the machines & versions of Java used the same format.)
    This is a Windows 7 Enterprise (32-bit) environment, but the script also works on 64-bit installations of Windows 7 Enterprise.
    I've tested this with 7u11 and went as far back as 6u21. It should work with just about any version that uses MSI for uninstalling.
    @echo off
    setlocal enabledelayedexpansion
    echo. & echo Checking for Obsolete Versions of Java...
    for /f "usebackq tokens=*" %%a in (`reg query HKLM\software\microsoft\windows\currentversion\uninstall`) do (
         rem echo a is %%a
         for /f "usebackq tokens=2*" %%b in (`reg query "%%a" /v DisplayName 2^>nul ^| find /i "java"`) do (
              rem echo b is %%b
              rem echo c is %%c
              for /f "usebackq tokens=3,4" %%d in (`reg query "%%a" /v UninstallString 2^>nul`) do (
                   echo.     Removing %%c
                   rem echo d is %%d
                   rem echo e is %%e
                   set uninstallcmd=%%e
                   set uninstallcmd=!uninstallcmd:^/I=^/X!
                   rem %%d %%e /qb-
                   %%d !uninstallcmd! /qb-
    :end
    endlocal
    Standard Dislaimer:*
    Neither I nor the respective owners and/or maintainers of Oracle make representations about the suitability of this software for any purpose. This material is provided "as is", with absolutely no warranty of the attached items whatsoever, whether express, implied, or statutory, including, but not limited to, any warranty of merchantability or fitness for a particular purpose or any warranty that the contents of the item will be error-free. Any use is at your own risk. Blah blah blah.

  • I need to buy a new charger for my iPad 2 as the original one has broken what do I need to look at I just need the lead I've tried a few but they not working what is the correct one I need please thanks

    Hi can anyone help please I need a new charger for my iPad 2 as the original lead that came with it has broken can anyone tell me where I can get one from and what is it I'm looking for as I have tried loads and they not working thanks

    This one is good:
    http://store.apple.com/us/product/MD836LL/A/apple-12w-usb-power-adapter

  • Can not get web page all of the way out to edge of computer screen tried to drag but did not work

    just got a new computer ..when i go to a web page the page does not go all of the way out to the side of the screen...i tried to grab and drag it but it did not work...i am new to this computer thing...need help

    I just had that problem today and although it unfortunately probably won't help you minus47, (as you said disabling your Add-ons didn't help) I tracked my problem down to 'Fierr' as the culprit and disabling it fixed it.
    To be honest I don't know why as 'Fierr' hasn't been updated since January '11 and Firefox only started playing up today.
    The only other thing I updated today was Flash Player but I'm not sure if I installed it before it started playing up though.
    Hopefully this might help someone else.

  • HT1349 My phone has stuck on to the headphone setting. How do i get it back to normal, have tried settings reset, but has not worked

    My phone has stuck on the headphone setting. Have tried the reset option, but to not resolved.
    When you press the side voulme controls, ( Headphones ) shows, even without them plugged.
    Any idea's

    Have you tried power off/on?  Hold the sleep/power button until the red slider appears and slide to power off.  Wait 10-15 minutes and then hold the button until it powers up again.  Often a simple power cycle clears faults.

  • Adobe Photoshop Lightroom 3 updates won't work? Also tried DNG converter but will not work either.

    Does anyone know how to get the Adobe Photoshop Lightroom 3 to update so you can use it? I can't process raw files with my new Canon SD Mark 3.
    It looks like they stopped doing updates.
    Next I tried the DNG converter but it was useless. It could not find any of my photos.
    SOS - I needed to complete a job with this last night and their site won't help and they finally respond by saying we will get back to you in a few days.

    That camera requires at least Lightroom 4.1. There is no update for Lightroom 3 that will support the files from that camera. I'm guessing that you're having the typical problem that new users at the DNG converter encounter. The converter prompts for the *FOLDER*. If you open that folder then the DNG converter will tell you there are no files to convert. Just choose the folder. I know, it's really stupid prompt and method. But it is what it is.

  • HT1476 Hi there my i phone will not charge i have replaced the battery and tried different cables but still not working! what can i do now?

    Hi There my i phone will not charge all of a sudden tried using diffrent cables and now i have had the battrey replaced but still nothing. It is now completey  dead.  Not sure what ealse to do?

    Hey jojo-10!
    Here is an article for you that can help you troubleshoot this issue with your iPhone:
    iPhone and iPod touch: Charging the battery
    http://support.apple.com/kb/HT1476
    Thanks for coming to the Apple Support Communities!
    Regards,
    Braden

  • Use REGEXP_INSTR to find a text string with space(s) in it

    I am trying to use REGEXP_INSTR to find a text string with space(s) in it.
    (This is in a Function.)
    Let's say ParmIn_Look_For has a value of 'black dog'. I want to see if
    ParmIn_Search_This_String has 'black dog' anywhere in it. But it gives an error
    Syntax error on command line.
    If ParmIn_Look_For is just 'black' or 'dog' it works fine.
    Is there some way to put single quotes/double quotes around ParmIn_Look_For so this will
    look for 'black dog' ??
    Also: If I want to use the option of ignoring white space, is the last parm
    'ix' 'i,x' or what ?
    SELECT
    REGEXP_INSTR(ParmIn_Search_This_String,
    '('||ParmIn_Look_For||')+', 1, 1, 0, 'i')
    INTO Position_Found_In_String
    FROM DUAL;
    Thanks, Wayne

    Maybe something like this ?
    test@ORA10G>
    test@ORA10G> with t as (
      2    select 1 as num, 'this sentence has a black dog in it' as str from dual union all
      3    select 2, 'this sentence does not' from dual union all
      4    select 3, 'yet another dog that is black' from dual union all
      5    select 4, 'yet another black dog' from dual union all
      6    select 5, 'black dogs everywhere...' from dual union all
      7    select 6, 'black dog running after me...' from dual union all
      8    select 7, 'i saw a black dog' from dual)
      9  --
    10  select num, str
    11  from t
    12  where regexp_like(str,'black dog');
           NUM STR
             1 this sentence has a black dog in it
             4 yet another black dog
             5 black dogs everywhere...
             6 black dog running after me...
             7 i saw a black dog
    5 rows selected.
    test@ORA10G>
    test@ORA10G>pratz
    Also, 'x' ignores whitespace characters. Link to doc:
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/conditions007.htm#i1048942
    Message was edited by:
    pratz

  • How to print a string with out using main method??

    how to print a string with out using main method??
    if we can tell me with example?
    thanks in adavance
    raja

    Hi,
    actually there's none. The UITableView class needs to "know" the tableView's height so either it can calc <number of rows> times <row height> or it has to sum the height of each single row.
    If the row which needs to be of different size always is the first or the last row you can user a tableHeader or tableFoot view.
    Regards
    Dirk

  • My ipad mini 2 is not turning on. I tried to reset with the power/home buttons but it didnt work. I tried to restore it in DFU mode but it didnt work, i get error 4004. Please help me!

    My ipad mini 2 is not turning on. I tried to reset with the power/home buttons but it didnt work. I tried to restore it in DFU mode but it didnt work, i get error 4004. Please help me!

    I also tried the ol' whacking 3 times trick...Didn't work to my surprise

  • HT201401 Hi.. My Iphone acts as if it is dead. Tried a lot with Sleep +home buttons as well, but did not worked. When it is plugged in, keeps on charging with a regular beep. Please advise.

    Hi.. My Iphone acts as if it is dead. Tried a lot with Sleep +home buttons as well, but did not worked. When it is plugged in, keeps on charging with a regular beep. Please advise.

    Yes, I understand that. But The screen issue is not a result of user damage. The phone was not dropped when the screen got cracked. My daughter was standing in a chair at the table and dropped one of her toys onto the bottom of the phone and cracked it right by the home button. (Nowhere near the LCD/digitizer flex cables, connections, or logic board components) then a few months later my phone had died, I plugged it in, it showed it was charging, then Apple logo disappeared and screen didn't work. AFTER contacting Apple about the issue and them telling me that it "is a result of the very minimal crack to the sheet of glass that has no relation to the LCD or Digitizer and that the warranty does not cover it I had taken it upon myself to change the LCD/digitizer to no avail, as well as backlight ic and backlight coil to no avail. But how do I go about letting Apple know that it is in fact NOT due to the cracked screen nor is it due to the flex cable connections? I can't. So the lady that works the call center phones  trying to "convince" me that she knows what she is talking about and "convince" me that it is from the cracked screen I have to take her word because I can't show Apple that its not because of that without paying them $199 to fix the screen in which case they will just send me a new phone rather than fix the faulty hardware under warranty.

Maybe you are looking for