aspose file tools*
The moose likes Beginning Java and the fly likes Multiplication Table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Multiplication Table" Watch "Multiplication Table" New topic
Author

Multiplication Table

Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
OK, I'm having trouble with another program. This program is for a multiplication table using nested loops. Here are the instructions:

Purpose: To learn how to convert integers to strings, how to format text, how to use nested loops.
Write a program that (algorithmically) shows a times table.
In other words, I want to type
java Times
and see

To do this, you will need to know how to:

use nested loops
convert integers to strings
format text

Here's the code I have so far:



And, here's the error message I get when I try to compile:

C:\Documents and Settings\Benjamin Q. Chau\WRITTENPROGRAMS>javac Times.java
Times.java:15: cannot resolve symbol
symbol : variable display
location: class Times
display += i;
^
Times.java:20: cannot resolve symbol
symbol : variable display
location: class Times
display += " " + i *

^
Times.java:22: cannot resolve symbol
symbol : variable display
location: class Times
display += " " + i * j

^
Times.java:26: cannot resolve symbol
symbol : variable display
location: class Times
display += "\n";
^
Times.java:27: cannot resolve symbol
symbol : variable display
location: class Times
System.out.println(display);
^
5 errors

Now, I'm thinking the display variable can be used as such when I declare it as a string and set it equal to "" (which I understand to mean null). I write "display +=" because I'm thinking this involves a concatenation operation and you can do this with strings? But when you do it with integers it becomes an arithmetic operation? I'm still a little fuzzy about that. I believe the errors point to the display variable as being the main culprit why the program is not compiling.

Also, I'm still having trouble determining when I use the capital letter as in "String" to declare a variable as opposed to when to use the lowercase letter such as in "string". I know that the capital references the class, in this case, the String class, but when specifically, are each used?

Thanks all. Any help would be greatly appreciated...

Benjamin
[ November 19, 2008: Message edited by: Benjamin Chau ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18123
    
  39



The display variable is a local variable declared in the block of the first for-loop. It is out of scope for the second for-loop.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18123
    
  39

Also, I'm still having trouble determining when I use the capital letter as in "String" to declare a variable as opposed to when to use the lowercase letter such as in "string". I know that the capital references the class, in this case, the String class, but when specifically, are each used?


There is no string class. There is only the String class.... unless you are naming your String variables, as "string".

Henry
Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
Henry, you're a genius. That was definitely the problem.

But now, I have another problem. Turns out I didn't need to declare a static variable "display" afterall since I'm really only needing it in one block of code. However, I reworked all the code and now it'll compile but the output is unorganized - not like it's supposed to look. Will you take a look at my code?



The programs looks like it's trying to display correctly but something is not right or out of place. I'll go back and edit/cut this post down to save space on the JavaRanch website but just want you to see it first.

Here's the output from the Command Prompt after I compiled and ran it:

C:\Documents and Settings\Benjamin Q. Chau\WRITTENPROGRAMS>java Times
0 1 2 3 4 5 6 7 8 90
0 0
0
0 0
0
0 0
0
0 0
0

etc. etc. etc.
[ November 19, 2008: Message edited by: Benjamin Chau ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18123
    
  39

The println() method adds a carriage return. Is that what you wanted?

For example...



Doesn't this displays two carriage returns?

Henry
[ November 19, 2008: Message edited by: Henry Wong ]
Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
Henry, you're right about the carriage return. Thanks. Here's my output after getting rid of the "ln". It's getting closer - the organization of data is looking better but the logical output is strange now.

C:\Documents and Settings\Benjamin Q. Chau\WRITTENPROGRAMS>java Times
0 1 2 3 4 5 6 7 8 900 000 000 000 000 000 000 000 000 000
011 011 111 211 311 411 511 611 711 811 922 022 222 422 622 822
1022 1222 1422 1622 1833 033 333 633 933 1233 1533 1833 2133 2433 2744 044
444 844 1244 1644 2044 2444 2844 3244 3655 055 555 1055 1555 2055 2555 3055
3555 4055 4566 066 666 1266 1866 2466 3066 3666 4266 4866 5477 077 777 1477
2177 2877 3577 4277 4977 5677 6388 088 888 1688 2488 3288 4088 4888 5688 6488
7299 099 999 1899 2799 3699 4599 5499 6399 7299 81
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
Did I miss something? But the times table in your first post seems wrong. 2 times 1 should be 2, but, in your table it is 1.
Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
Gamini,

The alignment was off. I added editing tags. Check it now...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36516
    
  16
Read very carefully what you are printing, and when.

****************************************************************************

You may find it easier to print the numbers directly onto the screen.
Why create a lot of String objects if you aren't using them elsewhere?
Find out about the % tags here and here. Print directly to the screen, taking maybe 4 characters for each number. That will read something like

2 4 6 8 10 12 14 16 18 20

Use the println method with no arguments to print a new line.

But you will have to install Java5 or Java6 for the % tags to work.
Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
I misplaced/added too many variables. Here's the code that works!

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

We generally ask that you cite where you get questions/problems from. It's only polite to give the original author credit. It turns out, this problem comes directly from our very own cattle drive.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
Sorry about that Fred. My instructor apparently got many assignments from the Cattle Drive.

To the Moosehead Saloon: Thanks for all the great programming assignments I've submitted so far, including this one, "Time". I've been learning a lot!
do pham
Greenhorn

Joined: May 12, 2011
Posts: 1
Hi everybody,
I'm a new user of this forum.

You can try my java file with Multiplication Table.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36516
    
  16
Welcome to the Ranch

I removed your e-mail address for security reasons and corrected one error in your code.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

ics noob,
Your post was moved to a new topic.
Sudharshan natarajan
Greenhorn

Joined: Jan 30, 2013
Posts: 1
Try this generic code guys

public void mtable(Integer num){
Integer len = (num * num);
len = len.toString().length();
ArrayList<String> prefix = new ArrayList<String>();
for(int i =0 ; i < len ; i++){
if(i==0){
prefix.add("");
}
else if(i == 1){
prefix.add(" ");
}
else{
prefix.add(" " + prefix.get(i - 1));
}

}

StringBuilder sb = new StringBuilder("");
for(int i=1 ; i <= num ; i++){
for(int j = 1; j <= num ; j++){
sb.append(prefix.get(len-(""+(i*j)).length()) + (i*j) + " ");
}

System.out.println(sb.toString() + "\n");
sb = new StringBuilder("");
}
}
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36516
    
  16
Welcome to the Ranch
I hope OP didn’t wait 4 years for your reply.
Tim Mreger
Greenhorn

Joined: Feb 06, 2013
Posts: 1
Here is a another simple solution.

Max Valdes
Greenhorn

Joined: Feb 21, 2013
Posts: 1
props to Tim Mreger, but an even easier way is:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36516
    
  16
Welcome to the Ranch
Using \t is a very old‑fashioned way to program; you should use the %d tags with a width parameter.
You should always use the code button; I shall see if I can use it on your post because it looks a lot better.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiplication Table
 
Similar Threads
class 3
The Programme is not Compiling
A Hint at Times is good?
package javax.faces.component does not exist
Whats wrong with system.err?