11

I must display all server names in the range of v10000 up to v10500.
Below is the code that I tried, but sometimes it displays a zero.

String template = "v10"; int count = 0; while (count < 501) { String number; if (count < 100) { number = "00" + Integer.toString(count); } else if(count < 10) { number = "0" + Integer.toString(count); } else { number = Integer.toString(count); } String server = template + number; System.out.println(server); count++; } 

But when I show this solution to my boss, he just laughs and says:

I can do this better.

How can I alter my code to make it work properly? I'm new to Java.

4
  • 6
    switch your if else/if around. the count < 10 being on top and count < 100 being on bottom Commented Nov 15, 2013 at 12:04
  • 4
    You need a more helpful boss. Commented Nov 15, 2013 at 16:49
  • always check for lowest value first else it will never get to that test (9 is less than 100 and less than 10 so you would go into the less than 100. I now changed code to do less than 10 first. Also for values under 10 you add 2 zeros, for values under 100 you add only 1 zero that way all values will have 3 digits :-) Commented Nov 15, 2013 at 18:50
  • @RobChurch You are right. Seniors/Bosses should be more helpful. Commented Nov 15, 2013 at 18:51

7 Answers 7

23

I would do

for(int i = 10000; i <= 10500; i++) System.out.println("v" + i); 
Sign up to request clarification or add additional context in comments.

2 Comments

change < to <= please. I've upvoted too fast :).
That is what I meant to write...
14

It is a very long way. Better use String.format()
A working solution for you would be:

for (int i = 0; i <= 500; i++) { String server = String.format("v10%03d", i); System.out.println(server); } 

The format String is builded like that:
v10 -> your String template of the server
%0 --> the zeros you need
3d --> three digits will be added

So your int i will be formatted like that.

4 Comments

Using a String.format is pretty expensive, and a bit complicated. IMHO.
@PeterLawrey: In general, String.format is a strong and good solution. But in the case of the OP's question, your answer would fit better.
@dTDesign: in my case, Peter's solution is better. But I'll keep String format in my mind. +1 for that
I agree that String.format is a more flexible solution and it is useful to be aware of.
8

Every other answers suggest a better approach to this problem. I was going myself to suggest to use format, but a loop starting from 10000 is fine too:

for (int i = 0; i <= 500; i++) { String server = String.format("v10%03d", i); System.out.println(server); } 

or simply (and faster):

for(int i = 10000; i <= 10500; i++) { System.out.println("v" + i); } 

But I think that we should also fix the bug in OP's code:

while (count < 501){ String number; if(count < 100){ number = "00" + Integer.toString(count); }else if(count < 10){ number = "0" + Integer.toString(count); }else{ number = Integer.toString(count); } String server = template + number; System.out.println(server); count++; } 

I would recommend using a for-loop instead but that's not the problem. The problem is the order of the tests. if count < 100 is false, then count < 10 is also false. You will never enter this block. Switch the order of your if statements.

So the fixed code:

for (int count = 0; count <= 500; count++){ String number; if(count < 10){ number = "00" + Integer.toString(count); }else if(count < 100){ number = "0" + Integer.toString(count); }else{ number = Integer.toString(count); } String server = template + number; System.out.println(server); } 

1 Comment

I like your first solution best. It is practical, shows the intention and is easily extended / reused.
5

Just use a for loop:

for(int i = 10000; i <= 10500; i++) System.out.println("v" + i); 

1 Comment

Just a few seconds late!
1
 String template = "v"; for(int i=10000;i<=10500;i++){ System.out.println(template+i); } 

Comments

0

Use String.format inside a for loop:

for (int i = 10000; i <= 10500; ++i) { System.out.println(String.format("v%d", i)); } 

This will count through those integers and prepend a "v" to each.

Comments

0

You can do a for from 10000 to 10500 and convert that int (or whichever numeric type you use) to String using a variety of solutions, e.g. Integer.toString().

Then you can concatenate "v" and your result using a StringBuilder.

Comments