• 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

print statement twice

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there, my code is printing twice the "Number from zero to 99: " statement, can someone please give me a hint why? Thank you so much :-)

/*
Write a program that asks the user a number N,  N < 100,
and writes the multiplication table for that number.
*/

 
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your main method, how many times do call the method that prints that statement?
 
Graciete Silva
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ah! thank you.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it is because of do-while statement.....just remove the do while loop statement and check.
 
Graciete Silva
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I decided to remove the first method, now the number is asked and validadet in main directly.
It looks like this now:

 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's actually a step in the wrong direction.  You want as little as possible in your main() method, usually only one or two lines.  See MainIsAPain (That's a link).
 
Graciete Silva
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much Knute, you are right. My problem is asking for an input number and validate it in the same method, maybe I should separate it into 2 methods.
I will try again ...

 
Graciete Silva
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ach, just realized, I just had to remove one line of code in main *facepalm*.
It works now, with one line of code in main!
Thank you for your help :-D




 
Junilu Lacar
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Graciete Silva wrote:Thank you so much Knute, you are right. My problem is asking for an input number and validate it in the same method, maybe I should separate it into 2 methods.
I will try again ...


No, that part is actually fine. Just delete line 27 in the code that you originally posted.  That way, you'll only have one line in your main() method and you eliminate the extra unused call to the askValidateN() method.

Try to use better names for the things in your program. "Ask a validate N" is not as good as "get a valid number" so you can rename askValidateN() to getValidNumber() and your program will make more sense.

The same thing goes for your multTable() -- a better description of what it's trying to do is "print multiplication table" so a better name for that method is, of course, printMultiplicationTable().  You don't gain anything from abbreviating names, it only makes your code more difficult to read.
 
Graciete Silva
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Junilu, I will consider it for the names.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic