Tech Support Guy banner
Status
Not open for further replies.

Help with Java writing to text files

Solved 
Tags
java
2K views 5 replies 2 participants last post by  TheGingerNinja 
#1 ·
Here's a program I've written to find prime numbers till a given range and prints them all out to a text file.

Code:
    import java.math.BigInteger;


    import java.io.*;
    import java.util.Scanner;

    public class PrimeLister
    {
    private static BigInteger two=new BigInteger("2");
    private static BigInteger three=new BigInteger("3");

    public static boolean isPrime(BigInteger n)
    {
        if(n.compareTo(BigInteger.ONE)==0 || n.compareTo(two)==0)
        {
            return true;
        }
        BigInteger half=n.divide(two);

        for(BigInteger i=three; i.compareTo(half)<=0;i=i.add(two))
        {

            if(n.mod(i).equals(BigInteger.ZERO))
            {
                return false;
            }

        }
        return true;

    }

   
    public static void write (BigInteger a) throws FileNotFoundException
    {
      String filename = "output.txt";   
      PrintWriter pw = new PrintWriter (filename);
      byte b = 1;
      OutputStream.write (b);
      OutputStream.close();
     
    }
   
    public static void main (String args[]) throws IOException
    {
        PrimeLister pl = new PrimeLister();

        BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
        String prompt = ">";

        BigInteger num = BigInteger.valueOf (2);

        System.out.println ("Enter Max Limit");
        System.out.print (prompt);
        int max = Integer.parseInt(br.readLine());

        while (num.equals(BigInteger.valueOf(max))!=true)
        {
            if(pl.isPrime(num))
            pl.write(num);
            num= num.add (BigInteger.valueOf(1));
        }
    }
    }
I've finished the part where you find the prime numbers but i need help with printing them to the textfile, I'm not really fluent in that area of conding at all.

Thank you.

Sincerely,
Andrew
 
See less See more
#3 ·
There are plenty of different ways to write to a file in Java; the important thing is that you're consistent.
Here you're making a PrintWriter to open the file, but not using any of the PrintWriter methods to actually write to it. Have a look at the documentation: https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html
As a tip, if there isn't a function which accepts a BigInteger or whatever unusual type you're using at the moment, you can often call toString on a java object to get the text representation of it (though it isn't always the most helpful or natural representation).

Aside: Even once you tidy it up and use PrintWriter methods throughout, that write method is somewhat analagous to opening a notebook and picking up a pen, writing, and putting the lid back on and the notebook where it belongs. It's the correct sequence for keeping everything neat and tidy, but dreadfully inefficient if you do the prep and clearup between writing every word! You almost certainly want to pass it a whole bunch of things to write, and have a loop through them between opening and closing it.
 
#4 ·
Yeah! Changed OutputStream to PrintWriter and switched a few things around! I followed your suggestion of looping the writing!

but dreadfully inefficient if you do the prep and clearup between writing every word!
Yeah, I had to do what you told, by printing it in a loop , flushing , then after the loop is over, closing. Do you know an alternative, better way of printing to text files instead of PrintWriter?

But the program works now. Thanks!
 
Status
Not open for further replies.
You have insufficient privileges to reply here.
Top