java - Testing whether an input is a number -
i have program, whenever type , hit enter, prints out in window (it's command prompt). however, due few problems calculator function i'm trying add, if user input number, want totally ignore
input = new jtextfield(); input.seteditable(true); input.setforeground(color.white); input.setcaretcolor(color.white); input.setopaque(false); input.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e) { string text = input.gettext(); if (text.length() >= 1){ print(text + "\n", false); docommand(text); scrollbottom(); input.selectall(); } } });
you check if text number regex that:
text.matches("[1-9][0-9]*");
i hope helps.
Comments
Post a Comment