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

# College Assignment Help

Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:
Hi All - First time user of this forum... probably going to be using this a lot! First year studying Java and I have a question on an assignment I just cant get right... I think I've gone a stupid way but... basically I need a program which asks a user for a number of rows between 3 to 10 and a number of columns between 5-10... and the program should display the number for example:

For example:
Enter rows: 4
Enter cols: 7
1 2  3   4   5   6  7
2 4  6   8  10 12 14
3 6  9  12 15 18 21
4 8 12 16 20 24 28

Code I have so far (please dont laugh)

When I run this I get:
Please enter number of rows between the range of 3 to 10: 3
Please enter number of Columns between the range of 5 to 10: truetrue

Few Q's
How can I get the user input?
How if someone select 3 rows and 4 columns do I get it to compute/calculate it?
How do I print it nearly and in proper rows?

I use eclipse and yeah any help would be appreciated

Thanks a lot
Mario

Saloon Keeper
Posts: 10653
85
• 1
• Number of slices to send:
Optional 'thank-you' note:
Have you learned about loops yet? This is a classic case of two nested loops, the outer loop for the row and the inner loop for the column. Because the output values can be calculated from the row and column you should have no need for arrays.

Mario Jorge
Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:
Hi Carey - thanks for replying! They have touched on loops but nothing in depth yet. So nested loop is what I should use? Will do some searching on it and see if I can figure it out... thanks a lot - Mario

Carey Brown
Saloon Keeper
Posts: 10653
85
• Number of slices to send:
Optional 'thank-you' note:
Specifically "for" loops.

Sheriff
Posts: 17644
300
• Number of slices to send:
Optional 'thank-you' note:

Mario Jorge wrote:Code I have so far (please dont laugh)

If you aspire to be a programmer, you'll need to learn to do that to yourself.

There's a lot to be said about egoless programming but it's good to remember that "Ignorance can be fixed with education but you can't fix stupid."

So, when you learn how to do that in a better way, come back to your code and laugh at your past ignorance. Good luck.

Marshal
Posts: 79061
375
• Number of slices to send:
Optional 'thank-you' note:
...and welcome to the Ranch

I hadn't heard of egoless programming before. So I searched and found this. Looks good.

Mario Jorge
Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:
@Junilu - thanks for advise... I'm sure I will learn to laugh it off!

@Campbell Ritchie - thanks for the welcome

Bartender
Posts: 732
10
• Number of slices to send:
Optional 'thank-you' note:

The above is not correct. You are only checking whether there is an input; you have not actually read the value yet.

It is also not the correct syntax for checking an integer value, and will not even compile.
The second line above would only compile if 'numRows' were a function taking a single boolean argument, and returning a boolean value.
You just need to compare the input value with 3 here (let's assume you read the integer value into a variable called ivalue):
if ( ivalue < 3 ) {
// write you error message here
}
Note that your error message should display the actual value that was read.
And you need to handle the case where the user inputs something other than an integer - for example, she mis-types and enters "w" instead of '3'.

Mario Jorge
Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:
Hi all - thanks for the help... I think I figured out the majority of it...

Some things I'm wondering... Right now the output is:
Please enter number of rows between the range of 3 to 10: 8
Please enter number of Columns between the range of 5 to 10: 8

Which is right but is there a way to format it so the rows / columns actually line up underneath each other ?

Another thing... I'm to "validate users input" ?
And one last thing I promise.. they want us to use another method user than main... I dont need it tho!!?

Thanks again all

Mario

Campbell Ritchie
Marshal
Posts: 79061
375
• Number of slices to send:
Optional 'thank-you' note:
You didn't use the code tags right; please look here for information about how they work. I also added code tags round your output and removed the bold tags because the two don't work together.
It is quite easy to have the numbers the same width:-
System.out.printf("%2d ", i);

Sheriff
Posts: 7125
184
• Number of slices to send:
Optional 'thank-you' note:

Another thing... I'm to "validate users input" ?

If you run your program and enter "aksjdhf", what happens?

And one last thing I promise.. they want us to use another method user than main... I dont need it tho!!?

You do, but ideally you would have only a line or two in main().  Read about it in MainIsAPain (that's a link).

Campbell Ritchie
Marshal
Posts: 79061
375
• Number of slices to send:
Optional 'thank-you' note:
It might be better to use a StringBuilder:-Only one print call so might have faster execution.

Mario Jorge
Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:

Campbell Ritchie wrote:It might be better to use a StringBuilder:-Only one print call so might have faster execution.

Hey Campbell Ritchie - Thanks for above... How would I initialize this? I have succeeded in the majority - new code below the only thing I am not missing the neat output... Do i need to rewrite the whole thing or..? New code below

1_Java_Code.jpg

author
Posts: 23949
142
• Number of slices to send:
Optional 'thank-you' note:

Mario Jorge wrote:
Hey Campbell Ritchie - Thanks for above... How would I initialize this? I have succeeded in the majority - new code below the only thing I am not missing the neat output... Do i need to rewrite the whole thing or..?

Well, if you don't want to convert your code to use the format() method, as shown by Campbell -- another option is to use the printf() method instead of the print() method. The first parameter of the printf() method is a format string, that can control the width (and justification) of how each number is to be printed.

Henry

Carey Brown
Saloon Keeper
Posts: 10653
85
• Number of slices to send:
Optional 'thank-you' note:
Rewrite this per Campbell's suggestion

Edit: Specifically System.out.printf("%2d ",i*j);