Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    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

  • #3
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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().

  • #4
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    If nothing appears here then we should go deeper into debugging the method itself.

  • #5
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 06:11 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •