• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help in printing the result of the for loop

 
Mahmood Ali
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

I am having a problem while writing this java for loop code. I am trying to print "NETProduct" a number of times which is depending on the passed "length" variable and to print after the word "NETProduct" the current value of the length + " ," and when it reached the last "NetProduct", it shoudn't produce "," after the "NETProduct" variable.
As an example:



So, what I am looking for is for the result:

NETProduct1,NETProduct2,NETProduct3,NETProduct4

and the result is in the same line.

Can you please help
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12102
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is this:

sentence = "NETProduct" + i + ",";

you have explicitly told it to print a comma after every "NETProductx". simply put in some logic to decide if you want a comma or not. something like this (which has not been tested in any way)

 
Ben Alex Barreto
Greenhorn
Posts: 15
Java Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:The problem is this:

sentence = "NETProduct" + i + ",";

you have explicitly told it to print a comma after every "NETProductx". simply put in some logic to decide if you want a comma or not. something like this (which has not been tested in any way)



Or he could simply include the comma before the inclusion of "i" in the string, such as



Hope it helped :P
 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Alex Barreto wrote:[

Hope it helped :P




Or else he will get a whole lot of NETPRODUCT...
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12102
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one other note...it's generally more common to write a loop as

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Beware of the += operator on Strings; it can cause slow performance.

If you have difficulties with Strings, try a StringBuilder.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I know, it would be more elegant not to append the final comma at all . . .
 
Mahmood Ali
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THanks for all of you.

It worked
 
Ben Alex Barreto
Greenhorn
Posts: 15
Java Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jishnu dasgupta wrote:

Or else he will get a whole lot of NETPRODUCT...


Well noted lol thanks for the correction
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic