0

I have just started studying Java and now I need to make an Java function that returns a formatted string representation of an int. This function must return "001" instead of just 1 and "010" instead of just 10, and so on... my function looks like this:

int value = 1;

public String getcountervalue() {
   String Return_string = Integer.toString(value);
   return (Return_string);
}

This is just a small part of an bigger code. the count of the value is handled by an other part of the code. I guess that the Integer.toString part will convert the int to an string, or?, but how can i make it properly formated (as explained above)?

I'm sorry if this question have been asked before, I where not able to find it.

I'm using java 1.7

Logan Murphy
  • 6,120
  • 3
  • 24
  • 42
user3416789
  • 71
  • 1
  • 6
  • 6
    http://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html – jmj Apr 09 '14 at 20:43
  • This question appears to be off-topic because it asks for the implementation of a requirement, rather than asking for help with a specific issue encountered while implementing the requirement. – T.J. Crowder Apr 09 '14 at 20:47
  • @T.J. Crowder I know this can be done whit an if/else statement, but that would be ugly locking, so i need to know if there is any better way of doing this. that is at least to me, an issue encountered while implementing that i need help whit. I know that I didn't say anything about it in the main post, but I do in fact know some ways of doing this, but none of them are locking good. – user3416789 Apr 09 '14 at 21:00
  • @Brian u are right, it is an duplication, I'm sorry weren't able to find it when i made this post – user3416789 Apr 09 '14 at 21:01

3 Answers3

6

There is a handy format() method, provided by String. See Oracle documentation

In your case, something like this should do it:

public String getCounterValue(int value) {
    return String.format("%03d", value);
}
incredibleholg
  • 583
  • 1
  • 7
  • 19
0
String.format("%02d", i);

or

for (i=0; i<=9; i++){
    System.out.println("00"+i)
}
for (i=10; i<=99; i++){
    System.out.println("0"+i)
}
if(i=100){
    System.out.println(i) 
}
hichris123
  • 10,145
  • 15
  • 56
  • 70
xhulio
  • 1,093
  • 1
  • 13
  • 32
  • 1
    Why would you ever use method 2? – Ryan Gates Apr 09 '14 at 21:24
  • he just started studying java, going the long way might help him practice and better understanding what the program should do. – xhulio Apr 10 '14 at 07:48
  • I understand what you mean in the same sense that I learned quicksort, mergesort, etc. However no one that is googling their way here to try to solve this problem in production should ever use method 2. Additionally method 2 doesn't handle any number over 100. – Ryan Gates Apr 10 '14 at 13:18
  • he wanted to count until 100 – xhulio Apr 12 '14 at 10:15
-2
public String getcountervalue() {
   String Return_string = Integer.toString(value);

   while(Return_string.length < 3){
       Return_string = '0'+Return_string;
   }

   return (Return_string);
}
Pat
  • 314
  • 2
  • 4
  • 14
  • 1
    This is a terrible way to approach the problem. Java provides a number of ways to do this. – Brian Roach Apr 09 '14 at 20:49
  • How else can you do this besides a number formatter? – Logan Murphy Apr 09 '14 at 20:50
  • @LoganMurphy It all comes down to `printf` at the core, and java exposes that via various methods and classes. `String.format()` for example. – Brian Roach Apr 09 '14 at 20:52
  • I wouldn't say this way is terrible if it is for learning purposes though. This code may help someone solve a bigger problem or explain how Java may have solved it with `String.format()` – Logan Murphy Apr 09 '14 at 20:53