adaminspaceship adaminspaceship - 1 year ago 94
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;

public class passwordDriver {

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

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) {


// 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



Answer Source

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));
} catch (IOException ioe) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download