Everrick Wright
Everrick Wright

Reputation: 31

PrintWriter Not Writing to java file

I have been trying to get my code to print to notepad for hours. Basically I create a file and put 10 random integers in it. Then I create another file that multiplies those 10 random ints from the first file by 10. I have tried flushing, open close, etc. I am assuming there is a problem in my code. Thanks!

    import java.io.File;


    import java.util.Random;
    import java.io.PrintWriter;
public class UghFiles {

    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub

        Random rand = new Random();
        int x = 0;
        File file = new File("LearnProgram.txt");

        if(file.exists()) {

            System.out.println("File alrady exists");
            System.exit(0);
        }
        else {
            PrintWriter output = new PrintWriter(file);
            for(int i= 0; i<10; i++){
                x = 0 + rand.nextInt(99-0)+1;

                output.print(x);
                //System.out.print(x + " ");
                //System.out.print("");

            }
            output.close();
        }
        File f2 = new File("Multiply.txt");
        if(f2.exists()) {

            System.out.println("File already exists");
            System.exit(0);
        }
        else {
            java.util.Scanner input = new java.util.Scanner(file);
            PrintWriter output = new PrintWriter(file);

            while(input.hasNext()) {
                x = x*10;

                output.println("" + x);
                System.out.print(x);
            }
            output.close();
            input.close();
        }

    }   
}

Upvotes: 3

Views: 83

Answers (2)

Michael Berry
Michael Berry

Reputation: 72399

A few mistakes here.

Firstly, you almost certainly want the numbers to apppear on a different line rather than being concatenated together:

for(int i= 0; i<10; i++){
    x = 0 + rand.nextInt(99-0)+1;
    output.print(x);
    //System.out.print(x + " ");
    //System.out.print("");
}

...so the third line there should read output.println(x);.

Secondly (and the big one):

java.util.Scanner input = new java.util.Scanner(file);
PrintWriter output = new PrintWriter(file);

You're ignoring f2 that you just created, and almost certainly want to write to. (The second argument should therefore be f2.) This is why your file is empty - it's actually being written to in your code above just fine, just overwritten by empty data at this step!

And thirdly:

while(input.hasNext()) {
    x = x*10;

You're just multiplying the same number again and again, and not actually reading from your scanner. You almost certainly want that to say x = input.nextInt() * 10; instead.

Upvotes: 2

user2670200
user2670200

Reputation:

output.print(x) just print an int-string. The next output.print(x) concatenates an int-string to the preceding string. At the end your file contains a very very long string.

And when while(input.hasNext()) loop starts it finds only 1 item, and the input has no "next". I think to solve your problem you should separate the values with a blank or a NL (use println(int)) so that your Scanner could recognize the "hasNext()".

Upvotes: 1

Related Questions