1 Replies - 243 Views - Last Post: 03 October 2012 - 01:36 PM Rate Topic: -----

#1 Toiletzombie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-October 12

Problem with Formatting

Posted 03 October 2012 - 11:59 AM

Hi everyone, im having some trouble with formatting some numbers correctly.
What I am trying to do, is to print 10 numbers per line, for 10 lines.
Anyone's help is greatly appreciated, thanks!
public class Emirp {
	
public static void main(String [] args) {
		
		
		System.out.println("This program will display the first 100 numbers that are emirps.\n");

		final int prime_numbers_per_line = 10;
		int count = 0;
		int number = 13;
		
		
		while (count < 100){
		
			if(isEmirp(number)){
				
				count++;
				
				if (isEmirp(number) && (!isPalindrome(number))) {
						System.out.print(number + "\t");
				
					}
				
				
				
				
				if (count % prime_numbers_per_line == 0){
					System.out.println();
				}
				
					
				}
					
			number++;		
			
		}
	}
		
		public static boolean isPrime(int number) {
			for ( int divisor = 2; divisor <= number / 2; divisor ++){
				if (number % divisor == 0) {
					return false;
				}
			}
			return true;
	
		}
	
		public static boolean isEmirp(int x) {
			return isPrime(x) && isPrime(reverse(x));
			
			
			
		}
		
		public static int reverse(int r) {
			
			if(r<10) return r;
			return switchnum(r%10,r/10);
		}
		
		
		public static int switchnum( int a, int B)/> {
			
			if (b<1) return a;
			return switchnum(a*10+b%10,b/10);
		
		}
		
		static boolean isPalindrome(int number) {
			return number == reverse(number);
		}








}


The problem i am having is that the output looks like:

13 17 31 37 71 73 79 97 107
113 149 157 167 179 199
311 337 347 359 389 701
709 733 739 743 751 761 769
907 937 941 953 967 971 983
991 1009 1021 1031 1033 1061 1069 1091 1097 1103
1109 1151 1153 1181 1193 1201 1213 1217 1223 1229
1231 1237 1249 1259 1279 1283 1301 1321 1381 1399
1409 1429 1439 1453 1471 1487 1499 1511 1523 1559
1583 1597 1601 1619 1657 1669 1723 1733 1741 1753

This post has been edited by pbl: 03 October 2012 - 01:32 PM
Reason for edit:: Fixed the code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with Formatting

#2 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Problem with Formatting

Posted 03 October 2012 - 01:36 PM

Please :code:
also not easy to follow... please indent your code correctly and avoid realy useless white lines
The principile is easy

Add 1 to the count of number printed in the line
if the count % whatever equals 0 then print a line feed

if you apply these two principle in your correctly uindented code it should work
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1