• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a simple counter 1 to 100

 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15448
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Mahesh Kedari
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or rather try this
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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));
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic