How can i detect that user has pressed enter key (in java) And what, if i want to use enter key in the condition of a loop? -


please me!

m new java. want specific line of code must executed after enter button has been pressed. like: int x= in.nextint(); value stored in x after pressing enter.

now want use such mechanism in loop.means; perform statement if enter button has pressed else statement.

if(!enter key pressed)

{


} else if(space has been pressed)

{


} tell me kind of code should write in java?

i don't want use listeners.

so suggest me should do?

simply read line of input. if you're reading user input:

bufferedreader br = new bufferedreader(new inputstreamreader(system.in)); string input = null; try {      input = br.readline();      //this code executed after user pressed enter } catch (ioexception e) {      //handle exception } 

likewise, if you're reading file, change system.in file (opened file object):

file file = new file(path);     bufferedreader br = new bufferedreader(new inputstreamreader(new filereader(file))); 

if want distinguish between input characters (such enter or space), read character character (instead of complete line) - use read() instead of readline() - , ask value of character is:

char c = (char)br.read(); if (c == ' ') {    // space } else if (c == '\n') {    // new line (enter) } 

notice '\n' work operating systems, linux uses mark new line , windows uses '\r\n'. if want support ios well, may ask if character '\r' (but has no '\n' following it)


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -