This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Help in printing the result of the for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help in printing the result of the for loop" Watch "Help in printing the result of the for loop" New topic
Author

Help in printing the result of the for loop

Mahmood Ali
Greenhorn

Joined: Aug 04, 2010
Posts: 13
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

Joined: Oct 02, 2003
Posts: 11161
    
  16

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)



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ben Alex Barreto
Greenhorn

Joined: May 10, 2011
Posts: 15

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

-ben
jishnu dasgupta
Ranch Hand

Joined: Mar 11, 2011
Posts: 103

Ben Alex Barreto wrote:[

Hope it helped :P




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


If debugging is the process of removing bugs, then programming must be the process of putting them in. -- Edsger Dijkstra

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

one other note...it's generally more common to write a loop as

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
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

Joined: Oct 13, 2005
Posts: 38033
    
  22
Yes, I know, it would be more elegant not to append the final comma at all . . .
Mahmood Ali
Greenhorn

Joined: Aug 04, 2010
Posts: 13
THanks for all of you.

It worked
Ben Alex Barreto
Greenhorn

Joined: May 10, 2011
Posts: 15

jishnu dasgupta wrote:

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


Well noted lol thanks for the correction
 
wood burning stoves
 
subject: Help in printing the result of the for loop
 
Similar Threads
Accessing of character by character from a String...
Help in FutureTask class with function timeout
Shuffle a string
length of integers
Loop Porblems in JSP