1

Please help me in the below code :

import java.util.Scanner;
class A
{
    public static void main(String args[])
    {
        Scanner Sc = new Scanner(System.in);
        System.out.print("Enter first number : ");
        a = Sc.nextInt();
        System.out.print("Enter second number : ");
        b = Sc.nextInt();
        System.out.print("Sum is : " + (a + b));
        System.out.print("Press enter to continue...");
        /* HERE IS MY DOUBT */
        /* WHAT TO WRITE HERE SO THAT WHEN "ENTER" IS PRESSED THEN CODE PROCEEDS */
    }
}

Like in C language we can use getch() to hold a program. Is there something similar in Java too?

I have already tried Sc.nextLine(). The problem with Sc.nextLine() is that, in it first I have to enter a value and then press enter and then the code proceeds. But what I want is to just press enter and proceed.

DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22
Jorvis
  • 386
  • 5
  • 13
  • Possible duplicate of [Equivalent function to C's "\_getch()" in Java?](https://stackoverflow.com/questions/1864076/equivalent-function-to-cs-getch-in-java) – Digvijaysinh Gohil Nov 15 '17 at 04:05
  • "......THEN CODE PROCEEDS" - then code is proceeds to where? what you want to do further? By default you program will be stopped after executing the last line. You need not to exit it manually if you don't have a *infinite loop* that needs to be closed when certain condition matches. – Erfan Ahmed Nov 15 '17 at 04:07
  • How about `sc.nextLine()`? – Jim Garrison Nov 15 '17 at 04:08
  • @ErfanAhmedEmon Ya in this question the code is exiting but there may be cases where just to increase interaction with the user I would require something just like this. – Jorvis Nov 15 '17 at 04:09
  • @JimGarrison I tried it, but it does not proceed until i enter a value and then press enter – Jorvis Nov 15 '17 at 04:10
  • please explain your thought what you want to do. May be what you are trying to do can be handled by a method recursion that executes each time after completion of a task and then again ask for user inputs. – Erfan Ahmed Nov 15 '17 at 04:13
  • @ErfanAhmedEmon What I'm trying to do is suppose, I created a object, and for this I'm showing message "Object Was Created Successfully!". But here just after the message I want to "hold" my program until user presses enter. – Jorvis Nov 15 '17 at 04:14
  • Then what you really want your last console output to be is: **Press Enter to continue...** correct? `Sc.nextLine(); while(!Sc.nextLine().equals("")) { }` and then: `System.out.println("Code Continuing...");`. – DevilsHnd - 退職した Nov 15 '17 at 04:27
  • @DevilsHnd Yes, I want my code in that way. – Jorvis Nov 15 '17 at 04:29

5 Answers5

1

I think this will work for a single key stroke event

public class EnterKeyEvent {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("Press a key");
        String entry = sc.nextLine();
        if(!"".equals(entry)) {
            System.out.println("Enter key not pressed");
        }
        else {
            System.out.println("Enter key pressed");
        }
    }
}

Like if you press Enter key then else block will be executed, but if Enter key is pressed after pressing some other key [like asdf and then Enter key] if block will be executed.

When I tried this code I was expecting the value of entry to come as \n but it was getting an empty String value. And that's what I did in the code.

Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52
0

Do the following,

public static void main(String args[]) {
    //your code...

    while (true) {

        System.out.println("press enter to exit...");
        String keyPressed = sc.nextLine();
        sc.nextLine();

        if (keyPressed.isEmpty()) break;
    }
}

Hope this your work!

Erfan Ahmed
  • 1,536
  • 4
  • 19
  • 34
0

Robot or Actions Class Press the desired Key by making the object of actions or Robot class. Like Actions act=New Action(webdriver Dr) Act.PressyourKey Use the auto suggestions.This is how we press any desired Key you can use this Act.pressyourKey if true in a loop and it should execute your code

balvinder dhillon
  • 109
  • 1
  • 2
  • 10
0

Try using code in this class but this works only form command line . you can't use it in IDE's console .c.reader will return null if you use in IDE

import java.io.Console;
import java.io.IOException;
import java.io.Reader;
public class GetChar {

public static void main(String  args[]) {

    int num=0;
    Console c =System.console();
    Reader r = c.reader();
try {
    num=    r.read();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

System.out.println(num);

}

}

The value of num will be 10 if '\n' is pressed pressed i,e Enter . I hope your code can use this logic

harsha kumar Reddy
  • 1,251
  • 1
  • 20
  • 32
-1
import java.util.Scanner;
class A
{
    public static void main(String args[])
    {
        while(true) {
            Scanner Sc = new Scanner(System.in);
            System.out.print("Enter first number : ");
            int a = Sc.nextInt();
            System.out.print("Enter second number : ");
            int b = Sc.nextInt();
            System.out.println("Sum is : " + (a + b));
            System.out.println("Press enter to continue...");
        }
    }
}
always007
  • 50
  • 4