so I'm learning programming and I have this teacher that give us this code for do some homework. What I need to do is to find a way to close the console/app using a combination of keys from the keyboard (CTRL + a letter). But I did a lot of research and all I could find was that this is only possible using an interface (Swing,GIU) and the KeyEvents methods, KeyStrokes or Key bindings. Actually, just one guy from another class could find how to do it and the teacher just said: "It's possible using ALT + a letter, look for it in the documention. Using CTRL is more difficult but also possible". This is my first post and I'm also a beginner in programming, please be kind. :(
package progra2;
import java.io.FileNotFoundException;
import java.lang.SecurityException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class Creararchivotexto {
private static Formatter output; //outputs text to a file
public static void main(String[] args) {
openFile();
addRecords();
closeFile();
}
//open file clients.txt
public static void openFile() {
try {
output = new Formatter("clients.txt"); //open the file
} catch (SecurityException securityException) {
System.err.println("Write permission denied. Terminating. ");
System.exit(1); //terminate the program
} catch (FileNotFoundException fileNotFoundException) {
System.err.println("Error opening file. Terminating. ");
System.exit(1); //terminate the program
}
}
public static void addRecords() {
Scanner input = new Scanner(System.in);
System.out.printf("%s%n? ", "Enter account number, first name, last name and balance");
try {
//output new record to file; assumes valide input
output.format("%d %s %s %.2f%n", input.nextInt(), input.next(), input.next(), input.nextDouble());
} catch (FormatterClosedException formatterClosedException){
System.err.println("Error writing to file. Terminating");
}catch (NoSuchElementException elementException){
System.err.println("Invalid input. Please try again");
input.nextLine(); //discard input so user can try again
}
System.out.print("? ");
}
//close file
public static void closeFile() {
if (output!= null) {
output.close();
}
}
}