• Post Reply Bookmark Topic Watch Topic
  • New Topic

print statement twice  RSS feed

 
Graciete Silva
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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.
*/

 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
ah! thank you.
 
Surangana Ghosh
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
  • 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:

 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you very much Junilu, I will consider it for the names.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!