Trying to create a little app that creates randomized passwords for the fun of it, and I'm currently at the stage of trying to make a way to let my user stop the application. However, the loop keeps on going and won't get to my scanner, but for some reason this code would work, if I took out the use of the scanner and synchronized code block.
Anyway, what is wrong with this code?:
public class Application {
public static void main(String[] args) {
Scanner stopScanner = new Scanner(System.in);
final PasswordGenerator pG = new PasswordGenerator();
Thread t1 = new Thread(new Runnable(){
@Override
public void run() {
try {
pG.passwordGenerator();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t1.run();
System.out.println("Press enter to stop!");
stopScanner.nextLine();
pG.shutDown();
}
}
class PasswordGenerator{
private volatile static boolean running = true;
protected Scanner scan = new Scanner(System.in);
public void passwordGenerator() throws InterruptedException{
synchronized(this){
System.out.println("Select charecter set:");
System.out.println(" 1: ABCDEF");
System.out.println(" 2: GHIJKL");
System.out.println(" 3: MNOPQR");
System.out.println(" 4: TUVWXYZ");
String charecters = scan.nextLine();
System.out.println("Select a password length");
int number = scan.nextInt();
scan.nextLine();
if(number <= 6){
System.out.println("Number cannot be smaller or equal to six!");
} else {
switch(charecters){
case "1":
while(running == true){
System.out.println("placeholder");
Thread.sleep(1000);
}
break;
case "2":
break;
case "3":
break;
case "4":
break;
default:
System.out.println("No valid set chosen!");
break;
}
}
}
}
public void shutDown(){
running = false;
}
}