Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7581727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:15:52+00:00 2026-05-30T18:15:52+00:00

I am learning Java and have a rather simple program that returns a series

  • 0

I am learning Java and have a rather simple program that returns a series of numbers in accordance with the Collatz Conjecture. I can have it output into the console or have many JOptionPane.showMessageDialog() windows pop up, one with each number in it.

How would I combine the JOptionPane.showMessageDialog()‘s to show all the outputs in one JOptionPane.showMessageDialog()?

Code:

package collatz;

import java.util.Random;
import javax.swing.*;

public class Collatz {

/**
 * Demonstrates the Collatz Cojecture
 * with a randomly generated number
 */

public static void main(String[] args) {
    Random randomGenerator = new Random();
    int n = randomGenerator.nextInt(1000);

    JOptionPane.showMessageDialog(null, "The randomly generated number was: " + n);


    while(n > 1){
        if(n % 2 == 0){
            n = n / 2;
            JOptionPane.showMessageDialog(null, n);
        }
        else{
            n = 3 * n + 1;
            JOptionPane.showMessageDialog(null, n);
        }
    }

    JOptionPane.showMessageDialog(null, n);
    JOptionPane.showMessageDialog(null, "Done.");

}

}

Thanks!

—
ZuluDeltaNiner

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:15:53+00:00Added an answer on May 30, 2026 at 6:15 pm

    Keep track of the full string to be displayed, then display it at the end:

    public static void main(String[] args) {
        Random randomGenerator = new Random();
        int n = randomGenerator.nextInt(1000);
    
        StringBuilder output = new StringBUilder("The randomly generated number was: " + n + "\n");
    
        while(n > 1){
            if(n % 2 == 0){
                n = n / 2;
            }
            else{
                n = 3 * n + 1;
            }
            output.append(n + "\n");
        }
    
        output.append("Done.");
        JOptionPane.showMessageDialog(null, output);
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning java and I have made simple program that simply reads value from
I am learning Java and just found that the Interface can have fields, which
I'm learning Java and I have a problem that I can't understand or solve!
I have started learning Java and I have 1 issue: I write a simple
So I'm learning java, and I have a question. It seems that the types
I'm learning Java and I have a quirk that I think only someone with
I'm just learning java and following a book. I have a program written via
I have a rather large (80k loc) java desktop app that talks to a
im currently learning stacks in java and have a quick question. what will the
I am currently learning about basic networking in java. I have been playing around

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.