Solved Help with Java writing to text files

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

TheGingerNinja

Andrew
Thread Starter
Joined
Dec 11, 2015
Messages
118
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
 

TheGingerNinja

Andrew
Thread Starter
Joined
Dec 11, 2015
Messages
118
Oh, and I use BigIntegers to store the prime numbers. I need to print the BigIntegers to the text file.

Thank you.
 

Ent

Josiah
Trusted Advisor
Joined
Apr 11, 2009
Messages
5,467
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.
 

TheGingerNinja

Andrew
Thread Starter
Joined
Dec 11, 2015
Messages
118
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
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top