• Post Reply Bookmark Topic Watch Topic
  • New Topic

College Assignment Help  RSS feed

 
Mario Jorge
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Carey Brown
Saloon Keeper
Posts: 3318
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 3318
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Specifically "for" loops.
 
Junilu Lacar
Sheriff
Posts: 11489
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Junilu - thanks for advise... I'm sure I will learn to laugh it off!

@Campbell Ritchie - thanks for the welcome
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
Read about printf in the Java™ Tutorials: 1 2.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be better to use a StringBuilder:-Only one print call so might have faster execution.
 
Mario Jorge
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
[Thumbnail for 1_Java_Code.jpg]
Output
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 3318
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rewrite this per Campbell's suggestion

Edit: Specifically System.out.printf("%2d ",i*j);
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!