• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

a simple counter 1 to 100

 
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or rather try this
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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));
}

reply
    Bookmark Topic Watch Topic
  • New Topic