How to detect enter key in console?

Hi Friends,

I have a loop in my program that asks the user to enter a number,I want that if the user presses the 'enter' key instead of entering any number,then the loop should exit.

But I don't know how to detect the 'enter' key as I am not using any GUI or ActionListener classes,is there some ASCII code for the enter key,that I can read and exit the loop.

Please help.

[402 byte] By [java80a] at [2007-10-2 20:15:14]
# 1
I don't know if there is any way to "detect" the <Enter> key but you could just test the input for a null value.
ValentineSmitha at 2007-7-13 22:57:30 > top of Java-index,Java Essentials,New To Java...
# 2

Thanks Smith,

Got the answer in another forum,posting it here for someone like me ,incase he needs in future...lol

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

if (input.length()==0){

break;

}

java80a at 2007-7-13 22:57:30 > top of Java-index,Java Essentials,New To Java...
# 3

You can use the following method to detect the enter key;

import java.util.*;

public class detector

{

public static void main(String [] args)

{

Scanner keyboard = new Scanner(System.in);

System.out.println("Enter a line of input");

String inputdata = keyboard.nextLine();

int value = inputdata.length();

char c = ' ';

for(int i =0; i<value; i++)

{

c = inputdata.charAt(i);

if(c=='\n') //'\n' is the ASCII key for Enter

{

break;

}

}

Hope This Helps You Out...>

Sanchita at 2007-7-13 22:57:30 > top of Java-index,Java Essentials,New To Java...
# 4
> You can use the following method to detect the enter key;That won't work. readLine strips off the enter key. The OP's solution (checking that the length of the input data is 0) is correct.
MLRona at 2007-7-13 22:57:30 > top of Java-index,Java Essentials,New To Java...
# 5

import java.util.*;

public class detector

{

public static void main(String [] args)

{

Scanner keyboard = new Scanner(System.in);

System.out.println("Enter a line of input");

String inputdata = keyboard.next();

int value = inputdata.length();

char c = ' ';

for(int i =0; i<value; i++)

{

c = inputdata.charAt(i);

if(c=='\n') //'\n' is the ASCII key for Enter

{

break;

}

}

>

Sanchita at 2007-7-13 22:57:30 > top of Java-index,Java Essentials,New To Java...
# 6
Sanchit, I'm glad you figured out the code tags. But, the OP's solution is still more correct than what you just posted. :-)
MLRona at 2007-7-13 22:57:30 > top of Java-index,Java Essentials,New To Java...
# 7

import java.util.*;

public class detector

{

public static void main(String [] args)

{

char c = ' ';

int number;

while(c!='\n')

Scanner keyboard = new Scanner(System.in);

System.out.println("Enter data");

String inputdata = keyboard.next();

c = inputdata.charAt(0);

try

{

number = Integer.parseInt(inputdata)

}

catch(NumberFormatException e)

{

System.out.println("Not a valid Number or an Enter key");

inputdata = keyboard.next();

c = inputdata.charAt(0);

}

}

}

}

Sanchita at 2007-7-13 22:57:30 > top of Java-index,Java Essentials,New To Java...
# 8
I appologize for necroposting, but can I use this same method to detect some key other than the enter key? Say I want my application to do something as soon as the users presses the 'A' key... is there a way to detect the pressing of the 'A' key as soon as it happens? (from the
L4E_WakaMol-Kinga at 2007-7-13 22:57:30 > top of Java-index,Java Essentials,New To Java...
# 9

> I appologize for necroposting, but can I use this

> same method to detect some key other than the enter

> key?

You couldn't do that prior to Java 6. I don't know if it's possible to do it using the Reader returned by Console.reader in Java 6. You could give it a try.

The reader returned might still only allow reading of data after enter is pressed.

Kaj

kajbja at 2007-7-13 22:57:30 > top of Java-index,Java Essentials,New To Java...