Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiplication Table

 
Benjamin Chau
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gamini,

The alignment was off. I added editing tags. Check it now...
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I misplaced/added too many variables. Here's the code that works!

 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Benjamin Chau
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,
I'm a new user of this forum.

You can try my java file with Multiplication Table.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ics noob,
Your post was moved to a new topic.
 
Sudharshan natarajan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
I hope OP didn’t wait 4 years for your reply.
 
Tim Mreger
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a another simple solution.

 
Max Valdes
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
props to Tim Mreger, but an even easier way is:
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic