Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JOptionpane window?

  1. #1
    Junior Member
    Join Date
    Oct 2017
    Thanked 0 Times in 0 Posts

    Default JOptionpane window?

    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?

    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
    for (int i = 100; i <= 1000; i++) {
    // Test if number is divisible by 5 and 6
    if (i % 5 == 0 && i % 6 == 0) {
    count++; // increment count
    // Test if numbers per line is 10
    if (count % NUMBERS_PER_LINE == 0)
    JOptionPane.showMessageDialog( null, i);
    JOptionPane.showMessageDialog( null,(i + " "));

  2. #2
    Join Date
    Apr 2014
    Thanked 7 Times in 7 Posts

    Default Re: JOptionpane window?


    It sounds like you'll want to build a String value that has everything, then show the JOptionPane only once at the end (instead of inside the loop). You can use the "+=" operator on Strings to concatenate:

    String str = "a";
    str += "b";
    System.out.println(str); // Prints "ab"

    To make it wrap to the next line, simply concatenate the system line separator:

    String str = "a";
    str += System.getProperty("line.separator");
    str += "b";
    System.out.println(str); // Prints the following:
    // a
    // b

    Hope that helps!

Similar Threads

  1. Applet viewer window is diplaying in fron of the current window every time
    By jsreddy99 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 24th, 2013, 07:16 AM
  2. Replies: 2
    Last Post: August 27th, 2012, 09:19 PM
  3. Replies: 1
    Last Post: December 17th, 2011, 03:32 AM
  4. [SOLVED] Scroll down in JOptionPane and window problems.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 60
    Last Post: June 16th, 2010, 12:04 PM
  5. How do i show all the values in one window(JOptionPane)??
    By Antonioj1015 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 25th, 2009, 09:24 PM