1

I have a program that is supposed to write stuff into a file and then display the same file at the end of the program. I have a smaller test program that does the same thing and it works, but the original doesn't work.

while( input != 5 )
    {    
        System.out.println();
        System.out.print("Enter Selection (1-5): ");
        input = in.nextInt();

        File myFile = new File ("password.txt");
        PrintWriter outFile = new PrintWriter(myFile);

        if(input == 1 )
        {    
            System.out.print("Password Length (6 +): ");
            inputLength = in.nextInt();
            if(inputLength >= 6)
            {   
                for(int codeLength = 0; codeLength <= inputLength;)
                {
                    randNum = random.nextInt(123);
                        if(randNum >= 97 && randNum <= 122)
                        {    
                            code+=(char)randNum;
                            codeLength++;
                        }
                        else if(randNum >= 48 && randNum <= 57)
                        {    
                            code+=(char)randNum;
                            codeLength++;
                        }

                }

                outFile.println("   " + codeNum + "\t" + code);
                outFile.close();
            }    
            else
                System.out.println("The password is too short");
        }
        else if(input == 2)
        {    
            System.out.print("Password Length (6 +): ");
            inputLength = in.nextInt();
            if(inputLength >= 6)
            {   
                for(int codeLength = 0; codeLength <= inputLength;)
                {
                    randNum = random.nextInt(123);
                        if(randNum >= 65 && randNum <= 90)
                        {    
                            code+=(char)randNum;
                            codeLength++;
                        }
                        else if(randNum >= 48 && randNum <= 57)
                        {    
                            code+=(char)randNum;
                            codeLength++;
                        }
                }

                outFile.println("   " + codeNum + "\t" + code);
                outFile.close();
            }
            else
                System.out.println("The password is too short");
        }
        else if(input == 3 )
        {    
            System.out.print("Password Length (6 +): ");
            inputLength = in.nextInt();
            if(inputLength >= 6)
            {   
                for(int codeLength = 0; codeLength <= inputLength;)
                {
                    randNum = random.nextInt(123);
                        if(randNum >= 33 && randNum <= 47)
                        {    
                            code+=(char)randNum;
                            codeLength++;
                        }
                        else if(randNum >= 48 && randNum <= 57)
                        {    
                            code+=(char)randNum;
                            codeLength++;
                        }
                }

                outFile.println("   " + codeNum + "\t" + code);
                outFile.close();
            }
            else
                System.out.println("The password is too short");
        }
        else if(input == 4 )
        {    
            System.out.print("Password Length (6 +): ");
            inputLength = in.nextInt();
            if(inputLength >= 6)
            {   
                for(int codeLength = 0; codeLength <= inputLength;)
                {
                    randNum = random.nextInt(123);
                        if(randNum >= 33 && randNum <= 126)
                        {    
                            code+=(char)randNum;
                            codeLength++;
                        }
                        else if(randNum >= 48 && randNum <= 57)
                        {    
                            code+=(char)randNum;
                            codeLength++;
                        }
                }

                outFile.println(code);
                outFile.close();
            }
            else
                System.out.println("The password is too short");
        }
        else if(input == 5 )
        {
            System.out.print("Thank you for using Fast Pass :)\n");
        }
        else
        {
            System.out.println("Invalid option. Try again.");
    }   
    System.out.println();
    System.out.println("Here are your randomly generated codes:");

    System.out.println(code);
    Scanner inFile = new Scanner("password.txt");
    while( inFile.hasNext() )
    {
        String file = inFile.next();
        System.out.println(file);
    }       

What is supposed to happen is a display of the codes like 1 -------- 2 --------

But what happens is the codes don't write into the file and then the file doesn't print when run

J. Murray
  • 1,460
  • 11
  • 19
gabys6
  • 11
  • 2
  • Everything aside, you have a lot of repeated code, a ton of it. So, do this: select all the code in any of the `if`-blocks where you are checking for `input`'s value to be....say 1 or 2, then right click and go to refactor -> then go to extract and then select method (or if you don't know how to) then simply Google "how to refactor and extract method in ". Your code will be a lot better. Also, use a switch statement when you know exactly what the values that you need to check against so in this case instead of `input == 1`, use switch-case – Shankha057 Oct 23 '19 at 23:39
  • I would recommend stepping through this with your debugger, to see what path you're actually taking through the code. If you haven't run `outFile.close()`, you're not going to be able to open the file and read the data. – Dawood ibn Kareem Oct 24 '19 at 03:03

1 Answers1

-1

In your PrintWriter constructor, pass in true as the second argument after wrapping the File argument using a FileOutputStream (use the parameterized constructor with the File argument and true as the second argument for the FileOutputStream)
The true argument in the PrintWriter constructor is for enabling auto flush and it works when you use methods like println, printf, or format (but not write, in that case you have to manually call the flush method)
Here is the documentation for that

Shankha057
  • 1,296
  • 1
  • 20
  • 38
  • Could the downvoter please explain the reason for doing so? A constructive and logical argument is much appreciated over clicking and ghosting. Thank You. – Shankha057 Oct 24 '19 at 00:24
  • I tried using flush but it didn't seem to do anything. – gabys6 Oct 24 '19 at 00:35
  • @gabrielas6 you are going with `PrintWriter` so simply do something like `new PrintWriter(new FileOutputStream(myfile,true), true);` and see what happens. And in this approach you won't need to call `flush()`. See the docs I answered in the links. – Shankha057 Oct 24 '19 at 00:37