home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

another java question

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2590
Discussions: 200,962, Posts: 2,379,579, Members: 246,330
Old March 8th, 2002, 01:54 PM   Digg it!   #1 (permalink)
Member
 
jgargac's Avatar
 
Join Date: Oct 2001
Location: St. Louis, MO
Posts: 463
another java question

Okay here's another puzzler for you. I have program that needs to display the numbers 1 -5 and then the same numbers times 10, 100, & 1000.

package ex416;

import javax.swing.JOptionPane;

public class ex416 {
public static void main( String args[] )
{
String output;
int number;

number = 1;
output = "";

while ( number < 6 ) {
output = "N 10*N 100*N 1000*N\n" +
(number) + " " + ( number * 10 ) + " " + ( number * 100) +
" " + ( number * 1000);
number = number + 1;
}
JOptionPane.showMessageDialog( null, output,
"Results of Program",
JOptionPane.INFORMATION_MESSAGE );

System.exit( 0 );
}

}

This displays the last number (5) only though, and not 1 - 4. There's something fishy with the loop or the results display. I appreciate any help. Thanks,

Jeff
jgargac is offline   Reply With Quote
Old March 8th, 2002, 01:56 PM     #2 (permalink)
Member
 
jgargac's Avatar
 
Join Date: Oct 2001
Location: St. Louis, MO
Posts: 463
If I change the code to this:

package ex416;

import javax.swing.JOptionPane;

public class ex416 {
public static void main( String args[] )
{
String output;
int number;

number = 1;
output = "";

while ( number < 6 ) {
output = "N 10*N 100*N 1000*N\n" +
(number) + " " + ( number * 10 ) + " " + ( number * 100) +
" " + ( number * 1000);
JOptionPane.showMessageDialog( null, output,
"Results of Program",
JOptionPane.INFORMATION_MESSAGE );
number = number + 1;
}


System.exit( 0 );
}

}

I get the proper results in 5 JOptionPane windows. Now I need to get it all in one window.

Jeff
jgargac is offline   Reply With Quote
Old March 8th, 2002, 02:14 PM     #3 (permalink)
Member
 
jgargac's Avatar
 
Join Date: Oct 2001
Location: St. Louis, MO
Posts: 463
I changed the JOptionPane to system.out.println and received the results that I wanted. Thanks,

Jeff
jgargac is offline   Reply With Quote
Old March 8th, 2002, 02:30 PM     #4 (permalink)
Senior Member
 
Join Date: Oct 2001
Location: Utah
Posts: 551
hey bud,
Looks like good code, but let me help you out a bit.
It would really help you understand, to walk through the program just like the computer will. Meaning, step through it, and on paper write down the variables at each step.

So, you're first code was good, except output variable got overwritten each pass of the loop, and only had the last thing in it when it output to the joption pane.

while ( number < 6 ) {


output= output + "\n";
output = output = output + "N 10*N 100*N 1000*N\n" +
(number) + " " + ( number * 10 ) + " " + ( number * 100) +
" " + ( number * 1000);
number = number + 1;
}
JOptionPane.showMessageDialog( null, output,
"Results of Program",
JOptionPane.INFORMATION_MESSAGE );

but, add in what I put in read, and then it just adds on each new group of numbers instead of overwriting.
Your second try opened up a new option pane for every time through the loop, where as your final working thing just printed it to system.out, each time through the loop.

good luck, and keep at it.
dragonb

Last edited by dragonb : March 8th, 2002 at 02:33 PM.
dragonb is offline   Reply With Quote
Old March 8th, 2002, 06:58 PM     #5 (permalink)
Banned
 
qball's Avatar
 
Join Date: Oct 2001
Posts: 447
dragonb,

Quote:
It would really help you understand, to walk through the program just like the computer will. Meaning, step through it, and on paper write down the variables at each step.

Excellent advice. The ability to debug and develop incrementally (take baby steps) is fundamental to programming well, imho. All too often people write the whole program, then wonder why it doesn't work...

Now as far as "good code", humm:

Code:
String output ="";
String temp ="";
for (int i = 1; i <6;i++)
{
   //format as needed
   temp = i + ", " + i*10 + ", " + i*100 + ", " + i*1000 + "\n";
   output =+ temp;
}
//do whatever you want with output
Now, one can argue that the while and for loops are logically the same thing, but you know you're doing 1-5, so just do that (gotta luv for loops).

Also, the first time running the proggy, slap some system.out.println's for each loop (output temp) and end result (output output), thus verifying you get what you want, then remove/comment those lines.
qball is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (2982)
The disrespect of Obama by Russian .. (46)
Making Health Care Worse (181)
Wireless Televisions. (12)
CPU fan stops spinning randomly (11)
Laptop with wireless problem. (7)
Regular Build (11)
windows 7 problem (7)
Point and Shoot Camera Suggestions. (6)
windows vista security holes (13)
Is the PSU I received dead? (13)
Print spooler problem (16)
radeon x850xt platinum & shader.. (6)
HIS HD5770 graphic card question (15)
Recent Discussions
Laptop with wireless problem. (7)
Point and Shoot Camera Suggestions. (6)
CPU fan stops spinning randomly (11)
Nvidia GTX 260 problem (1)
Modern Warfare 2: Who Bought It? (65)
Is the PSU I received dead? (13)
Print spooler problem (16)
windows vista security holes (13)
Kingston Bluetooth Dongle Driver (1)
Multiple Restarts Required at Boot (3)
Open With ..... Win7 (1)
webcam (0)
upgrade for hp a6101 (0)
tv not turn on-makes clicking sound (2)
EVGA 9800 gtx help with finding a goo.. (11)
Regular Build (11)
Help with onclick and buttons (0)
Virus advise (8)
My monitor won't turn on after instal.. (1)
Internet Lost (3)
Dept. of HS: NSA 'Helped' Develop Vis.. (16)
Ideal cheap graph card for PC-Gaming? (18)
radeon x850xt platinum & shader 3 (6)
Graphics Card Upgrade Question (4)
For Sale BFG GTX285 OC2 with 10 year .. (3)


All times are GMT -4. The time now is 12:17 PM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28