1

I was wondering if there is an option in Java which would allow you to pause program until any key pressed. This is one part of my code, eventually it is a part of a while loop in another class.

public void menu()
{
    boolean exit = false;

    System.out.println("-- Command interpreter --");
    System.out.println("[1] Write results to console");
    System.out.println("[2] Write results to text file");
    System.out.println("[3] exit");
    System.out.println("-------------------------");
    System.out.print("Input Value: ");

    String s = scaner.nextLine();

    int i = Integer.parseInt(s);

    switch (i)
    {
        case 1:
        {
            writeToScr(listPopulation);
            System.out.println("\n\nPress any key to continue..");
            scaner.next();
            break;
        }
        case 2:
        {
            writeToFile(listPopulation);
            System.out.println("\n\nPress any key to continue..");
            scaner.next();
            break;
        }
        case 3:
        {
            exit = true;
            break;
        }
        default:
        {
            System.out.println("Unknown Entry.");
            break;
        }
    }

    if (exit)
    {
        System.out.println("Exit!");
        return;
    }
}

So what i want to accomplish is, for example in case 1, when i get results written to console, i want for program to pause and let me check what i have until i press any key. My code works only for ENTER, or any text + ENTER. Is there a way that i could press any key without pressing ENTER afterwards and the program continues?

newGuy
  • 119
  • 1
  • 2
  • 6
  • If you had a GUI, you can add a `KeyListener` to your `JFrame`. – Josh M Jan 24 '14 at 17:47
  • Thanks Josh i added KeyListener - implemented interface. I am assuming your thinking of a method keyPressed(). I'm having a bit of trouble implementing it to code, first time i heard of KeyListener, can you help me bit more? Thanks – newGuy Jan 24 '14 at 18:05

3 Answers3

4

The problem is with the Java console which needs to work with every operating system. This console does not allow one to listen to individual key presses. For this to work, you'll need to either create a GUI such as a Swing GUI or use an alternative 3rd party console library such as jCurses.

Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346
3

I don't think this is possible through Java console. You would need some sort of API that can listen for events(key strokes(Swing or JNI for example)).

jrowe08
  • 390
  • 1
  • 9
-1

As you want to wait the current execution in switch case,So below is small code which can help you.It will wait till user will type click+Enter.

Code :

import java.lang.Thread;
import java.util.Scanner;


public class App{

    public void menu() throws InterruptedException
   {
    boolean exit = false;
    Scanner scaner = new Scanner(System.in);
    System.out.println("-- Command interpreter --");
    System.out.println("[1] Write results to console");
    System.out.println("[2] Write results to text file");
    System.out.println("[3] exit");
    System.out.println("-------------------------");
    System.out.print("Input Value: ");

    String s = scaner.nextLine();
    int i = Integer.parseInt(s);

    switch (i)
    {
        case 1:
        {
            ThreadB b = new ThreadB();
        b.start();
                synchronized(b)
                {
                    b.wait();
                }
            System.out.println("\n\nPress any key to continue..");
            scaner.next();
            break;
        }
        default:
        {
            System.out.println("Unknown Entry.");
            break;
        }
    }

    if (exit)
    {
        System.out.println("Exit!");
        return;
    }
}

  public static void main(String arg[])
    {
        try {
            new App().menu();
        } catch (Exception ex) {
           ex.printStackTrace();
        }
    }
}

class ThreadB extends Thread{

    Scanner scan = new Scanner(System.in);

    @Override
    public void run(){

        while(scan.hasNext())
        {
        String abc = scan.next();
            if(abc.equalsIgnoreCase("click"))
            {
         synchronized(this)
                {
                  notify();
        }
            }
        }
    }
}
Kick
  • 4,689
  • 1
  • 18
  • 24