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?

2
  • If you had a GUI, you can add a KeyListener to your JFrame. Commented Jan 24, 2014 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 Commented Jan 24, 2014 at 18:05

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

-2

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();
        }
            }
        }
    }
}

8 Comments

-1 for non-compilable code that doesn't make much sense or work even if it could compile.
@HovercraftFullOfEels thnk u.
Please fix your answer so that it can be tested. Do so, and if it works, I'll up-vote it. But please don't post junk code.
Plz give me 5 min to update it.I will make sure nxt time
It runs, sweetheart, but it doesn't react to a single key stroke.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.