Home > Software design >  How to stop the program when pressing a certain button on the keyboard, for example s
How to stop the program when pressing a certain button on the keyboard, for example s

Time:05-21

I am new to programming and at the moment I am doing a task, the essence of which is the emulation of scanning a person by gender and age with a further pass to a zone defined for its parameters. I was told to supplement the program so that, for example, when you press the S button on the keyboard, the program ends. Please tell me how can I implement this. I have 4 classes in my code:

main

public class Main {
    public static void main(String[] args) {
        PassportScan passportScan = new PassportScan();
        Guard guard = new Guard();
        while (true) {
            Person person = passportScan.methodScan();
            String result = guard.checkPerson(person);
            System.out.println(result);
        }

    }
}

PassportScan

import java.util.Scanner;

public class PassportScan {
    Scanner scanner = new Scanner(System.in);

    public Person methodScan() {
        System.out.println("Scanning gender");
        String gender = scanner.next();
        System.out.println("Scanning age");
        int age = scanner.nextInt();
        return new Person(gender, age);

    }

}

Person

public class Person {
    private String gender;
    private Integer age;

    public Person(String gender, Integer age) {
        this.gender = gender;
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public Integer getAge() {
        return age;
    }

}

Guard

public class Guard {
    public String checkPerson(Person personToCheck) {
        String gender = personToCheck.getGender();
        int age = personToCheck.getAge();

        if (age < 18 && gender.equals("M")) {
            return "Zone 1";
        }
        if (age >= 18 && gender.equals("M")) {
            return "Zone 2";
        }
        if (age < 18 && gender.equals("F")) {
            return "Zone 3";
        }
        if (age >= 18 && gender.equals("F")) {
            return "Zone 4";
        }
        return "";
    }
}

Thanks in advance for the tip and your time!

CodePudding user response:

Basically there will be two scenario for this.

1. Once Press S >>> You want to terminate the program

Simply its not available in normal console but there is way to achieve refer this post for more info https://stackoverflow.com/a/1066647/8524713

2. Press S and then press Enter key >> In this case its easy

check on each gender input if its S break the loop

try below code for reference

    public static void main(String[] args) {
        PassportScan passportScan = new PassportScan();
        Guard guard = new Guard();
        while (true) {
            Person person = passportScan.methodScan();
            //checking if person object is null if ts null means s is enter
            // break from loop
            if(person==null) {
                break;
            }
            String result = guard.checkPerson(person);
            System.out.println(result);
        }
    }
   static class PassportScan {
        Scanner scanner = new Scanner(System.in);

        public Person methodScan() {
            System.out.println("Scanning gender");
            String gender = scanner.next();

            //check if string input is S or s
            // then its returning null person object
            if(gender.equalsIgnoreCase("s")) return null;

            System.out.println("Scanning age");
            int age = scanner.nextInt();
            return new Person(gender, age);

        }

    }

one more way is to directly terminate in PassportScan class once S is typed

class PassportScan {
        Scanner scanner = new Scanner(System.in);

        public Person methodScan() {
            System.out.println("Scanning gender");
            String gender = scanner.next();

            //check if string input is S or s terminating prog
            if(gender.equalsIgnoreCase("s")) System.exit(0)

            System.out.println("Scanning age");
            int age = scanner.nextInt();
            return new Person(gender, age);

        }

    }

CodePudding user response:

You probably have some Component in that you display something, for example a (class that extends) JFrame, lets call it c.

Now call:

c.addKeyListener(new KeyListener() {
    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (Character.toLowerCase(e.getKeyChar()) == 's') System.exit(0);
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }
});

It is not possible to catch a KeyEvent without a GUI (When you think about it, how should Java know that the key was pressed in your application? If it would catch any KeyEvent on the device, you could easily build spyware that catches passwords or sth.)

  •  Tags:  
  • java
  • Related