aspose file tools*
The moose likes Beginning Java and the fly likes a simple counter 1 to 100 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "a simple counter 1 to 100" Watch "a simple counter 1 to 100" New topic
Author

a simple counter 1 to 100

mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi all!!

I have a question regarding printing numbers.I am printing 1 to 100 and there can't be a "," before the 1 or after the 100.I have it so it works but want to know if there is a better way to do it.



thanks
Mike
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

Welcome to JavaRanch!

Looking at your code, it actually prints numbers from 0 to 100, not from 1 to 100, because count starts at 0 in line 11. I'd advise you to use proper indentation, or better yet, surround the statement that's supposed to be in the loop in braces, because the way it looks now is very confusing:

Write it like this:

One way to avoid printing the 100 afterwards is like this:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Mahesh Kedari
Ranch Hand

Joined: Nov 28, 2009
Posts: 111
Or rather try this


Regards,
Mahesh Kedari - Fidus Technologies Ltd.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Jesper Young wrote: . . . I'd advise you to use proper indentation.
. . . that means 4 spaces at a time, not tab characters. And it is probably a bad idea to use an IDE at this early stage; your code has "Eclipse" written all over it
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi ,

Thanks for the tipps;) to clear things up about Eclipse,I am taking a correspondence course "Programming with JAVA and Eclipse" ;) that is why i am using it.The course is 15 months plus minus how fast i learn,and i just started 3 weeks ago.
I am going through the books pretty quick as i can't get enough of it !!! Hopefully i won't have to write too many silly questions here but i am glad i found this forum and it seems really active!!!

Thanks again;
Mike
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
It says
No question is too simple or too small!
on the Beginning Java front page. So you aren't asking silly questions. You have actually hit a common problem called the out-by-one error which will reappear many times in your career the way dandelions reappear on a lawn!
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Is out-by-one the same as off-by-one? They seem to be the same, but I've never heard out-by-one.

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Hunter McMillen wrote:Is out-by-one the same as off-by-one?
yes. If I used the wrong name, sorry.
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

hi,

Thanks again and a general question.Is it not a good idea to learn JAVA with Eclipse? Well even if it is i will have to continue it because i have paid quite a bit for the course;)
But i am also using other sources and will be purchasing a book or 2 as well.

I am glad to see allot of participation here though, and maybe i will be able to answer a question or 2 myself eventually ;)

Take care
Mike
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

If you're taking a course it most likely won't be that big of an issue, as you have an instructor who can help you through some of the quirks of your IDE. In general though, if you were learning on your own it would be better NOT to use an IDE, because you end up wasting time learning the IDE and not the language.

Hunter
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
There are also things you learn to do at the command line which you miss because the IDE does them all for you.
john chris
Greenhorn

Joined: Aug 02, 2010
Posts: 8
@Test
public void StringBuilderTest(){
StringBuffer sb = new StringBuffer();

for(int i = 0; i<100; i++ ){
sb.append(i).append(",");
}
System.out.println(sb.toString());
System.out.println(sb.deleteCharAt(sb.length()-1));
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a simple counter 1 to 100