Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-20-2009, 05:32 PM   PM User | #1
jimmyv
New to the CF scene

 
Join Date: Jul 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
jimmyv is an unknown quantity at this point
Why wont my program print to the output file?

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:
Code:
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:

Code:
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.
jimmyv is offline   Reply With Quote
Old 07-20-2009, 06:28 PM   PM User | #2
ckeyrouz
Senior Coder

 
ckeyrouz's Avatar
 
Join Date: Jun 2009
Location: Montreal, Canada
Posts: 1,044
Thanks: 5
Thanked 179 Times in 179 Posts
ckeyrouz is on a distinguished road
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:

Code:
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
ckeyrouz is offline   Reply With Quote
Old 07-20-2009, 07:45 PM   PM User | #3
jimmyv
New to the CF scene

 
Join Date: Jul 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
jimmyv is an unknown quantity at this point
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().
jimmyv is offline   Reply With Quote
Old 07-20-2009, 08:08 PM   PM User | #4
ckeyrouz
Senior Coder

 
ckeyrouz's Avatar
 
Join Date: Jun 2009
Location: Montreal, Canada
Posts: 1,044
Thanks: 5
Thanked 179 Times in 179 Posts
ckeyrouz is on a distinguished road
If nothing appears here then we should go deeper into debugging the method itself.
ckeyrouz is offline   Reply With Quote
Old 07-22-2009, 03:46 AM   PM User | #5
jimmyv
New to the CF scene

 
Join Date: Jul 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
jimmyv is an unknown quantity at this point
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.

Last edited by jimmyv; 07-22-2009 at 05:11 AM..
jimmyv is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:53 AM.


Advertisement
Log in to turn off these ads.