java - How to read in an "Enter"/"Return" -


i'd exit while loop when user inputs enter/return key.

here's code:

do {     //id=sclocal.nextint();             try{         id=sclocal.nextint();     }catch(inputmismatchexception exception)     {                  system.out.println("invalid input!");         return;     }      if(listperformer.size() <= id || id < 0)     {         system.out.println("invalid input!");         return;     }     else     {         idlist.add(listperformer.get(id));     }  }while(sclocal.nextline().length() > 0);  //here want: * 

*if user not want input id, input nothing else , press "enter" leave loop.

if working jframe or other swing components can similar example.

import javax.swing.*; import java.awt.*; import java.awt.event.*;  public class test extends jframe {     private jpanel testpanel = new jpanel(new gridbaglayout());     private gridbagconstraints c = new gridbagconstraints();     private jlabel label1 = new jlabel("0");     boolean bool = true;      test() {         super("swing example");         setlayout(new gridbaglayout());         setdefaultcloseoperation(jframe.exit_on_close);         buildgui();         c.weightx = 1.0;         c.gridx = 0;         c.gridy = 0;         add(testpanel, c);         pack();         setfocusable(true);         addkeylistener(new customkeylistener());         setvisible(true);         run();     }      private void buildgui() {         c.fill = gridbagconstraints.horizontal;         c.gridx = 0;         c.gridy = 0;         testpanel.add(label1, c);     }      private void run() {         int = 0;         {             i++;             label1.settext(i+"");             repaint();             revalidate();             validate();             try {                 thread.sleep(1000);             } catch (exception e) {              }         } while(i<100 && bool);     }      public static void main(string[] args) {         new test();     }      private class customkeylistener implements keylistener     {         @override         public void keypressed(keyevent e) {             if (e.getkeycode() == keyevent.vk_enter) {                 bool = false;             }         }          @override         public void keyreleased(keyevent e) {          }          @override         public void keytyped(keyevent e) {          }     } } 

however, if working command line or other console @ answer. allow check return key without pausing while loop wait input console


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -