Output in JOptionPane

Problem formatting loop to output

Page 1 of 1

8 Replies - 2021 Views - Last Post: 23 June 2009 - 08:43 PM Rate Topic: -----

#1 MntMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-January 09

Output in JOptionPane

Posted 22 June 2009 - 07:25 PM

I am having a problem with the format of my output. I have to output only the primes 1-1000 to a single pane in rows of 10.
My code puts out the correct numbers, but some lines have one number on it and others have 3...There are so many lines output I can't see the top or bottom of the pane. Any help building my output message format would be appreciated.

   import javax.swing.JOptionPane;
	public class findPrime
   {
	   static Boolean isPrime (int num)
	  {		 
		 if (num % 2 == 0)
			return num == 2;
		 int lim = (int) Math.sqrt(num);
		 for (int i = 3; i <= lim; i+=2)
			if (num % i == 0)
			   return false;
		 return true;
	  }
	   public static void main (String args[])
	  {
		 int counter = 1;
		 String message = " ";
		 for (int num = 2; num <= 1000; num++)
		 {
			if (isPrime(num))
			   message += num + " ";
			if ((counter % 10) == 0)
			{message += "\n";}
			counter ++;
		 }
		 JOptionPane.showMessageDialog (null,"The first 1,000 primes are: " + message);
	  }
   }



Is This A Good Question/Topic? 0
  • +

Replies To: Output in JOptionPane

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Output in JOptionPane

Posted 22 June 2009 - 08:23 PM

counter++ should only be executed if the number is a prime :)
move it inside your if
Was This Post Helpful? 0
  • +
  • -

#3 MntMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-January 09

Re: Output in JOptionPane

Posted 22 June 2009 - 08:45 PM

Thanks pbl, that kinda worked. Now I only have primes, but the output still has a huge window (takes up the whole screen) with many many empty lines? Not the 10 per line I am trying to get.

		 for (int num = 2; num <= 1000; num++)
		 {
			if (isPrime(num))
			counter ++;	
			   message += num + " ";  
			if ((counter % 10) == 0)
			{message += "\n";}
		 }


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Output in JOptionPane

Posted 22 June 2009 - 08:52 PM

You have a lot of {} missing
		 for (int num = 2; num <= 1000; num++)
		 {
			if (isPrime(num)) {
			   counter ++;	
			   message += num + " ";  
				if ((counter % 10) == 0) {
				   message += "\n";
													 }
												  }
		 }


Was This Post Helpful? 0
  • +
  • -

#5 OrganizedChaos  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 153
  • Joined: 29-November 08

Re: Output in JOptionPane

Posted 22 June 2009 - 08:53 PM

if (isPrime(num)){
			counter ++;	
			   message += num + " ";
}


Have to include brackets on the if statement over 1 line long. Otherwise, message += num + " " will get executed always.
Was This Post Helpful? 1
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Output in JOptionPane

Posted 22 June 2009 - 08:58 PM

View PostOrganizedChaos, on 22 Jun, 2009 - 07:53 PM, said:

if (isPrime(num)){
			counter ++;	
			   message += num + " ";
}


Have to include brackets on the if statement over 1 line long. Otherwise, message += num + " " will get executed always.

better explained than in my post :^:

*Edited than rather than then

This post has been edited by pbl: 22 June 2009 - 09:00 PM

Was This Post Helpful? 0
  • +
  • -

#7 MntMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-January 09

Re: Output in JOptionPane

Posted 22 June 2009 - 09:04 PM

Well this is what I have now, still doesn't put 10 on a line. I get a verrry long pane with a couple of numbers on some lines and none on others? Has me baffled. It must be in my logic with the % 10 huh?

	   public static void main (String args[])
	  {
		 String output = "This program will show you the prime numers from 1 to 1,000"; 
		 JOptionPane.showMessageDialog (null, output);
		 int counter = 1;
		 String message = " ";
		 for (int num = 2; num <= 1000; num++)
		 {
			if (isPrime(num))
			{
				counter ++;
			   message += num + " ";
			}			
			if ((counter % 10) == 0)
			{
				message += "\n";
			}
		 }
		 JOptionPane.showMessageDialog (null,"The first 1,000 primes are: " + message);
	  }


Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Output in JOptionPane

Posted 23 June 2009 - 04:16 PM

View PostMntMan, on 22 Jun, 2009 - 08:04 PM, said:

Well this is what I have now, still doesn't put 10 on a line. I get a verrry long pane with a couple of numbers on some lines and none on others? Has me baffled. It must be in my logic with the % 10 huh?

	   public static void main (String args[])
	  {
		 String output = "This program will show you the prime numers from 1 to 1,000"; 
		 JOptionPane.showMessageDialog (null, output);
		 int counter = 1;
		 String message = " ";
		 for (int num = 2; num <= 1000; num++)
		 {
			if (isPrime(num))
			{
				counter ++;
			   message += num + " ";
			}			
			if ((counter % 10) == 0)
			{
				message += "\n";
			}
		 }
		 JOptionPane.showMessageDialog (null,"The first 1,000 primes are: " + message);
	  }


Sure
when counter is a multiple of 10 % 10 == 0
you add a \n and that will be done until you find another prime
the if(counter % 10) test should be done inside the same if that increments counter
if (isPrime(num))
{
	 counter ++;
	 message += num + " ";			
	 if ((counter % 10) == 0)
		message += "\n";
}


How many times will we have to tell you ? :D
Was This Post Helpful? 1
  • +
  • -

#9 MntMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-January 09

Re: Output in JOptionPane

Posted 23 June 2009 - 08:43 PM

That was that last time you had to tell me...Works great. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1