Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java String value increment

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    5
    My Mood
    Sleepy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up Java String value increment

    Java-code --
    String value = 00008, increment something like 00008 to 00009.
    If anyone can help me here I'd greatly appreciate it, thanks!


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Java String value increment

    Are you sure you really want to increment a String? The reason I ask is that if you are continually wanting to increment an integer value associated with a String it might make more sense to have an int variable and increment that.

    If you really have a String and want to produce another string associated with the incremented value: use Integer.parseInt() to obtain the integer value, increment that value, and finally produce the new String with String.format().

    The last step will involve something like String.format("%05d",val) but check the documentation linked to for details about the format string.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    5
    My Mood
    Sleepy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java String value increment

    Code is something like..
    String s = "00008";
    int i =Integer.parseInt(s);
    System.out.println("i.."+i); // 8
    String s1 = String.format("%05d",i);
    System.out.println("s1......."+s1); // 00008

    How to increment results = s1+1 ; ?

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Java String value increment

    String.format("%05d",i);
    --->
    String.format("%05d", i + 1);

    ... ?

  5. The Following User Says Thank You to jps For This Useful Post:

    raghu3.ankam (May 24th, 2013)

  6. #5
    Junior Member
    Join Date
    May 2013
    Posts
    5
    My Mood
    Sleepy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java String value increment

    thanks..

Similar Threads

  1. Increment a data by 1 after a certain time
    By shimira in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 16th, 2013, 07:14 AM
  2. post increment
    By deependeroracle in forum Java Theory & Questions
    Replies: 1
    Last Post: November 12th, 2012, 11:31 AM
  3. Processing of increment and decrement operators in Java
    By fredyeboah in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 22nd, 2012, 11:03 AM
  4. Increment a date
    By colerelm in forum Java Theory & Questions
    Replies: 1
    Last Post: September 30th, 2011, 05:57 AM
  5. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM

Tags for this Thread