aspose file tools*
The moose likes Beginning Java and the fly likes Need Help with asking user input for Times Table number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need Help with asking user input for Times Table number" Watch "Need Help with asking user input for Times Table number" New topic
Author

Need Help with asking user input for Times Table number

David Kurey
Greenhorn

Joined: May 01, 2013
Posts: 3
I'm very new to java and I'm having a hard time with it. My assignment is to create a Multiplication Times Table from 0-12 with any number and ask the user if he or she wants to see another number to be displayed again for the times table, if yes the times table runs again with another number from the user and then asks the user if they want to see another number yes or no and if no the program can exit. I've been trying for a week now and I'm having a hard time figuring it out. here is the code I got for the times table to work but I can't get a loop to work to repeat. Any help or examples would greatly be appericated. Thank You

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

putting all your code in your main() method is a bad idea.

I would suggest you break what you have here into a few methods.

Move the code for getting user input into a method, and have it return that as an int.
Move the code that prints stuff out to a method that takes an int as a parameter

I would then write a method that validates the input as being between 0 and 12, inclusive, returning a boolean.

Once you have each of these pieces, you can build up from there.



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
sagar khana
Greenhorn

Joined: Jan 15, 2012
Posts: 21
hi ,

use a do-while loop also make a variable which stores user input for repetition and using that input keep looping if its yes then put condition according to it

Regards,
Sagar
David Kurey
Greenhorn

Joined: May 01, 2013
Posts: 3
How do i split it into methods? I very confused can you provide any examples. Thank You
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Welcome to the Ranch That is one way to do it.
David Kurey
Greenhorn

Joined: May 01, 2013
Posts: 3
Can someone just get me started with the code I got posted and point out what I have to do? I need user input
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
You can find out about user input from the Java Tutorials. You might find the scanning section the most useful.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

David Kurey wrote:Can someone just get me started with the code I got posted and point out what I have to do? I need user input

I'm pretty sure I did point out what you need to do. I"m pretty sure you are also already getting user input.

Can you look at your code in your first post, and identify which lines print out your table? I'll give you a hint - there are three lines all together that do this.

Move those into a method, and replace them in main with a call to the method.

OR...

start over. write a main method that only prints "I'm in main". get that to compile and run. Once you've done that, write a method that prints "I'm in the method", and have your main call it. once THAT works, change it so that you can pass in a parameter - like a 7 or a 42. Update the method to print out the value passed in.

Each time you make a little change like this, re-compile, debug, and test. Make sure it really does what you want.

Eventually you will have a method that you can pass in a number, and it will print out the times table for that number. You may have your main call it 2-3 times with different values to prove to yourself it works.

Once you have that method working, then you can start looking at getting user input.

The beauty here is that once you have a method that takes an int as a param and prints the times table, it DOESN'T MATTER how you GET that number. You can read from a file, you can hard-code it, you can ask for user input...

you want to separate the different pieces of your code as much as possible.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need Help with asking user input for Times Table number