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