Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question about a java problem. I wanted to know if there is a way to display wha

ID: 3620988 • Letter: Q

Question

Question about a java problem. I wanted to know if there is a way to display what i have in the loop in a single JOptionPane message dialog box.

System.out.println ("Hour" + " Distance Traveled" +
"--------------------------------------");
for (int x = 1; x <= hours; x++)
{
distance = speed * x;
System.out.println(x + " " + distance);
}

the output for this is something like
Hour Distance Traveled
----------------------------------------
1 40
2 80
3 120

i would like to use option pane to display this in a single dialog box, my previous attempts resulted in multiple dialog boxes for each hour.

Explanation / Answer

please rate - thanks

create a string with the output and output the entire string at once

import java.util.*;

import javax.swing.*;
import java.lang.*;
public class main
{
public static void main(String []args)
{int hours=3,speed=40,distance;
String output="Hour Distance Traveled ---------------------------------------- ";
for (int x = 1; x <= hours; x++)
{
distance = speed * x;
output=output+x + "           " + distance +" ";
}
JOptionPane.showMessageDialog(null,output);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote