adaminspaceship adaminspaceship - 3 months ago 14
Java Question

all input from user on console to .txt java

I want to have all user input in the console be copied to a .txt file.
Here is my code, thanks!

import java.util.Scanner;
import java.io.Console;


public class passwordDriver {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

Console cnsl = null;
String alpha = null;

boolean checkPass = true;
// creates a console object
cnsl = System.console();

if (cnsl != null) {
/** // read line from the user input
alpha = cnsl.readLine("Name: ");

// prints
//System.out.println("Name is: " + alpha);
*/
}


while (checkPass) {


try{


// if console is not null
if (cnsl != null) {

//tell the user what the app does
System.out.println("Type your password and check how strong it is!");


// read password into the char array
char[] pwd = cnsl.readPassword("Password: ");



if (PassCheck.validatePassword(String.valueOf(pwd))) {
checkPass = false;
}
else {
System.out.println("Your password is invalid. ");
}
}
}catch(Exception ex){

// if any error occurs
ex.printStackTrace();
}




}


}

Answer

So in your code, where you are checking password just put this code:

If typed password is wrong then->

BufferedWriter bw = null;

try {
    bw = new BufferedWriter(new FileWriter("passwords.txt", true));
    bw.write(String.valueOf(pwd));
    bw.newLine();
    bw.flush();
} catch (IOException ioe) {
    ioe.printStackTrace();
}
Comments