...

View Full Version : Why wont my program print to the output file?



jimmyv
07-20-2009, 05: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.

ckeyrouz
07-20-2009, 06:28 PM
I would suggest as a start to remove the throws IOException from the public static void main method and put all your code in a try / catch block as follows:



public static void main(String[] args)
{
try
{
.....
.....
.....
.....
}
catch(Exception ex)
{
ex.printStackTrace();
}
}


give it a try and if there is an exception it will surely be displayed in front of you

jimmyv
07-20-2009, 07:45 PM
Awesome, so i went and tried it, and if nothing gets displayed does that men there arent any issues? I'm thinking somewhere in fillPrimeInfo() is wrote something that isnt sending it back to nums[i] properly in main().

ckeyrouz
07-20-2009, 08:08 PM
If nothing appears here then we should go deeper into debugging the method itself.

jimmyv
07-22-2009, 03:46 AM
Alright so I've tried everything, if I comment out everything except for it to print "Mixed Numbers" to the output file, it'll print that title. It should still at least print it when the program has a math error, and just print garbage or nothing after that. However when I un-comment the code, nothing at all prints. Any more ideas? I'm lost.


Edit: okay nvm, I commented out everything in getPrimeInfo() and then everything printed, so my error is there somewhere.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum