Reputation: 39
Hi Im new to this forum and also new to java, I need some help for my intro to java homework. Ive done most of the logic. The problem is to write a program that displays all the numbers from 100 to 1000, ten per line, that are divisible by 5 and 6. Numbers are separated by exactly one space. My professor wants it to be done in a Joptionpane window. When I try to do that, only one answer pops up in a window. How do I make my answers appear ten in a line, separated by exactly one space in only one window? My professor wants us to use an escape function to be able to display the lines of answer.
public class FindFactors {
public static void main(String[] args) {
String message = "";
final int NumbersPerLine = 10; // Display 10 numbers per line
int count = 0; // Count the number of numbers divisible by 5 and 6
// Test all numbers from 100 to 1,000
for (int i = 100; i <= 1000; i++) {
// Test if number is divisible by 5 and 6
message = message + " " + i;
count++;
if (count == 10) {
message = message + "\r\n";
count = 0;
}
if (i % 5 == 0 && i % 6 == 0) {
count++; // increment count
// Test if numbers per line is 10
if (count % NumbersPerLine == 0)
JOptionPane.showMessageDialog(null, i);
else
JOptionPane.showMessageDialog(null, (i + " "));
}
}
}
}
Upvotes: 4
Views: 2003
Reputation: 173
Please see the below approach, a slight change to your code and will give the required output.
public class FindFactors {
public static void main(String[] args) {
final int NumbersPerLine = 10; // Display 10 numbers per line
int count = 0; // Count the number of numbers divisible by 5 and 6
// Test all numbers from 100 to 1,000
String numbersPerLine = "";
for (int i = 100; i <= 1000; i++) {
// Test if number is divisible by 5 and 6
if (count == 10) {
count = 0;
}
if (i % 5 == 0 && i % 6 == 0) {
numbersPerLine =numbersPerLine+" "+i;
count++; // increment count
// Test if numbers per line is 10
if (count % NumbersPerLine == 0)
numbersPerLine =numbersPerLine+"\n";
}
}
JOptionPane.showMessageDialog(null, numbersPerLine);
}
}
Upvotes: 1
Reputation: 11153
A JOptionPane
has the ability to show images, text or any other components, for this particular case you may want to create your own JPanel
which adds every line of numbers into a JLabel
and then adds that JLabel
to it.
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MultiLineOptionPane {
private JPanel pane;
public static void main(String[] args) {
SwingUtilities.invokeLater(new MultiLineOptionPane()::createAndShowGui);
}
public void createAndShowGui() {
pane = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
StringBuilder sb = new StringBuilder(); //This object will be used to concatenate in the next for loop
for (int i = 0; i < 500; i++) { //Sample for loop
if ((i) % 10 == 0) { //Every 10 numbers we restart the StringBuilder
pane.add(new JLabel(sb.toString())); //We add a new JLabel to the JPanel with the contents of the StringBuilder
sb.delete(0, sb.length()); //We restart the StringBuilder
} else {
sb.append(i); //We append the current number to the StringBuilder
sb.append(" "); //We append a space after the number
}
}
pane.add(new JLabel(sb.toString())); //We add the last line of numbers in the StringBuilder to the pane
JOptionPane.showMessageDialog(new JFrame(), pane, "Numbers", JOptionPane.PLAIN_MESSAGE); //We display the message
}
}
The output for the above program looks like (cropped or it would be too tall):
Upvotes: 0