jimmyv

07-20-2009, 06:32 PM

Well I'm writing a program that will fill an array with random numbers, check if the numbers are prime or not, give the next smaller prime number, and the next larger prime number. And output that all to an output file. I get no compiler errors, but nothing shows up in the output file.

heres the main class:

public class Prog4

{

public static void main(String[] args) throws IOException

{

String fname; int temp;

MyLong nums[];

Scanner kbd = new Scanner(System.in);

System.out.print("Enter the output filename: ");

fname = kbd.nextLine();

FileWriter fwriter = new FileWriter(fname);

PrintWriter myOutputFile =new PrintWriter(fwriter);

System.out.print("How many MyLong objects do you want to have? ");

temp = kbd.nextInt();

nums = new MyLong[temp];

for(int i=0; i<nums.length; i++)

{

nums[i] = new MyLong();

nums[i].fillPrimeInfo();

}

myOutputFile.print("Mixed Numbers:\n\n ");

for(int i=0; i<nums.length; i++)

{

if(nums[i].getSmallestFactor() == 1)

{

myOutputFile.printf("%8d is a prime number\n", nums[i].getNum());

}

else

{

myOutputFile.printf("%8d is not a prime number - it is divisible by %2d \n", nums[i].getNum(), nums[i].getSmallestFactor());

}

myOutputFile.printf("Largest prime number smaller than %8d is %8d\n", nums[i].getNum(), nums[i].getNextSmallerPrimeNumber());

myOutputFile.printf("Smallest prime number larger than %8d is %8d\n\n", nums[i].getNum(), nums[i].getNextLargerPrimeNumber());

}

fwriter.close();

}

}

and here is "MyLong", which fills in all the info:

public class MyLong

{

private long num;

private byte smallestFactor;

private long nextSmallerPrimeNumber, nextLargerPrimeNumber;

public MyLong()

{

Random rd = new Random();

num = Math.abs(rd.nextLong());

smallestFactor = 1;

nextSmallerPrimeNumber = 0;

nextLargerPrimeNumber = 0;

}

public void fillPrimeInfo()

{

//store primeOrNot() into primeChecked

long primeChecked = PrimeChecker.primeOrNot(num);

//returns smallest factor, or 1

if(primeChecked == 1)

{

smallestFactor = 1;

}

else

{

smallestFactor = (byte) primeChecked;

}

//returns the next larger prime number

long largerGetter = num;

long lG = num + 1;

while(largerGetter != 1)

{

largerGetter = PrimeChecker.primeOrNot(lG);

lG++;

}

nextLargerPrimeNumber = lG;

//returns next smaller prime

long smallerGetter = num;

long sG = num - 1;

while(smallerGetter != 1)

{

smallerGetter = PrimeChecker.primeOrNot(sG);

sG--;

}

nextSmallerPrimeNumber = sG;

}

public long getNum()

{

return num;

}

public byte getSmallestFactor()

{

return smallestFactor;

}

public long getNextSmallerPrimeNumber()

{

return nextSmallerPrimeNumber;

}

public long getNextLargerPrimeNumber()

{

return nextLargerPrimeNumber;

}

}

also, 'primeOrNot' returns a long number "1", or a numbers smallest factor.

heres the main class:

public class Prog4

{

public static void main(String[] args) throws IOException

{

String fname; int temp;

MyLong nums[];

Scanner kbd = new Scanner(System.in);

System.out.print("Enter the output filename: ");

fname = kbd.nextLine();

FileWriter fwriter = new FileWriter(fname);

PrintWriter myOutputFile =new PrintWriter(fwriter);

System.out.print("How many MyLong objects do you want to have? ");

temp = kbd.nextInt();

nums = new MyLong[temp];

for(int i=0; i<nums.length; i++)

{

nums[i] = new MyLong();

nums[i].fillPrimeInfo();

}

myOutputFile.print("Mixed Numbers:\n\n ");

for(int i=0; i<nums.length; i++)

{

if(nums[i].getSmallestFactor() == 1)

{

myOutputFile.printf("%8d is a prime number\n", nums[i].getNum());

}

else

{

myOutputFile.printf("%8d is not a prime number - it is divisible by %2d \n", nums[i].getNum(), nums[i].getSmallestFactor());

}

myOutputFile.printf("Largest prime number smaller than %8d is %8d\n", nums[i].getNum(), nums[i].getNextSmallerPrimeNumber());

myOutputFile.printf("Smallest prime number larger than %8d is %8d\n\n", nums[i].getNum(), nums[i].getNextLargerPrimeNumber());

}

fwriter.close();

}

}

and here is "MyLong", which fills in all the info:

public class MyLong

{

private long num;

private byte smallestFactor;

private long nextSmallerPrimeNumber, nextLargerPrimeNumber;

public MyLong()

{

Random rd = new Random();

num = Math.abs(rd.nextLong());

smallestFactor = 1;

nextSmallerPrimeNumber = 0;

nextLargerPrimeNumber = 0;

}

public void fillPrimeInfo()

{

//store primeOrNot() into primeChecked

long primeChecked = PrimeChecker.primeOrNot(num);

//returns smallest factor, or 1

if(primeChecked == 1)

{

smallestFactor = 1;

}

else

{

smallestFactor = (byte) primeChecked;

}

//returns the next larger prime number

long largerGetter = num;

long lG = num + 1;

while(largerGetter != 1)

{

largerGetter = PrimeChecker.primeOrNot(lG);

lG++;

}

nextLargerPrimeNumber = lG;

//returns next smaller prime

long smallerGetter = num;

long sG = num - 1;

while(smallerGetter != 1)

{

smallerGetter = PrimeChecker.primeOrNot(sG);

sG--;

}

nextSmallerPrimeNumber = sG;

}

public long getNum()

{

return num;

}

public byte getSmallestFactor()

{

return smallestFactor;

}

public long getNextSmallerPrimeNumber()

{

return nextSmallerPrimeNumber;

}

public long getNextLargerPrimeNumber()

{

return nextLargerPrimeNumber;

}

}

also, 'primeOrNot' returns a long number "1", or a numbers smallest factor.