Two Laptop Bag*
The moose likes Beginning Java and the fly likes Problem with FOR loop..Need Help ASAP... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with FOR loop..Need Help ASAP..." Watch "Problem with FOR loop..Need Help ASAP..." New topic
Author

Problem with FOR loop..Need Help ASAP...

ELL TIM
Greenhorn

Joined: Mar 24, 2009
Posts: 3
i've this class assignment that says:

Design and write an ArithmeticGUI class that allows users to input play with numbers multiplication. The GUI should contain three JTextField, one for input first number, second for second number and third for the result of multiplication of both numbers. There should be one button which the user can signal that correct multiplication result has been entered. The multiplication table of the second number should be displayed in the text area and the correctness of the multiplication result should also be displayed in a text field, but this should be set uneditable. A sample Arithmetic GUI interface is displayed below:

If users input a correct multiplication result of two numbers, the below should happened:



If users input a wrong multiplication result of two numbers, the below should happened:




i've already try it but i have problem at the for loop structure...
here the java code that i try..





please help me solve this...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

http://faq.javaranch.com/java/UseAMeaningfulSubjectLine
http://faq.javaranch.com/java/UseCodeTags

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2563
    
    9

What you really doing is a times table. Then the Times assignment of the Cattle Drive may be some help.

If you figure out the algorithm for that, you should have solve your original problem.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Please use the code button; it makes your code much easier to read.

You are right about having problems with for loops; I suggest you find a book and read up about for loops, or find the syntax in the Java Tutorials.

Anmd welcome to JavaRanch
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

for (num1 * num2 == result) //(have problem)

I'm not sure what you are trying to do here. What you have in your parens: "num1 * num2 == result" is essentially a boolean. it's either 'true' or 'false'.

A 'for' statement doesn't take a boolean - it takes three things for setting up, testing, and updating variables. Then, the code inside the curly brackets potentially runs multiple times.

So really, the question is, what are you trying to DO there? Are you trying to test something? i don't think so, because it's a repeat of what's in your if statement just above it.

I assume you are trying to loop around and print out all those products.

So, as others have said, look in a book or online tutorial for how to set up a for-loop. Then, see if you can write one that does nothing more that prints "hello" 12 times. Then get it to print something like:

1
2
3
4
5
6
7
8
9
10
11
12

etc, and add a little bit each time.


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

Joined: Mar 24, 2009
Posts: 3
fred rosenberger wrote:
for (num1 * num2 == result) //(have problem)

I'm not sure what you are trying to do here. What you have in your parens: "num1 * num2 == result" is essentially a boolean. it's either 'true' or 'false'.

A 'for' statement doesn't take a boolean - it takes three things for setting up, testing, and updating variables. Then, the code inside the curly brackets potentially runs multiple times.

So really, the question is, what are you trying to DO there? Are you trying to test something? i don't think so, because it's a repeat of what's in your if statement just above it.

I assume you are trying to loop around and print out all those products.

So, as others have said, look in a book or online tutorial for how to set up a for-loop. Then, see if you can write one that does nothing more that prints "hello" 12 times. Then get it to print something like:

1
2
3
4
5
6
7
8
9
10
11
12

etc, and add a little bit each time.



Sorry bout the code,i'm new to this site..also new to java programming...the thing is i want the output to print out like this when user key in the second number,then the multiplication are base on the second number.

1 x 2 = 2
2 x 2 = 4
3 x 2 = 6
4 x 2 = 8
...
...
...
12 x 2 = 24

i try using this code but still cant get what i want..

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

one of the hardest things to learn as a new programmer is to not try and do too much at once. The less you try and accomplish on each build/debug/test cycle, the easier it is to find your problems.

So, I'd suggest this:

try and write code that when the user enters their second number, you get it to print the number 1-12, each on a new line. Get that to work. ONLY after you are sure that is working perfectly, try and change it to print

1 x <the input second number>
2 x <the input second number>
... etc

ONLY after you have that working, try and get it to print the actual product on the end.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
ELL TIM wrote:..also new to java programming...
Has whoever is teaching you not taught you about the structure of a for loop?
ELL TIM
Greenhorn

Joined: Mar 24, 2009
Posts: 3
just figured it out actually. Thanks for the tips! (^_^)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(OT) Mostly I was just entertained that we got to say "buttCheck".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with FOR loop..Need Help ASAP...