Received: by 10.68.31.34 with SMTP id x2mr1845208pbh.17.1315660973777; Sat, 10 Sep 2011 06:22:53 -0700 (PDT) Path: m9ni3307pbd.0!nntp.google.com!news1.google.com!news.glorb.com!news.netfront.net!not-for-mail From: langrenfengzi Newsgroups: alt.comp.lang.java,comp.lang.java.help,comp.sources.d Subject: Re: Newbie seeking comments on StdinReader class Date: Sat, 10 Sep 2011 21:22:49 +0800 Organization: Netfront http://www.netfront.net/ Lines: 37 Message-ID: References: NNTP-Posting-Host: 119.80.125.81 Mime-Version: 1.0 X-Trace: adenine.netfront.net 1315660973 84290 119.80.125.81 (10 Sep 2011 13:22:53 GMT) X-Complaints-To: news@netfront.net NNTP-Posting-Date: Sat, 10 Sep 2011 13:22:53 +0000 (UTC) User-Agent: Opera Mail/11.51 (Win32) Content-Type: text/plain; charset=gbk; format=flowed; delsp=yes Content-Transfer-Encoding: Quoted-Printable =D4=DA Mon, 05 Sep 2011 22:00:39 +0800=A3=AC =D0= =B4=B5=C0: The key sentence: > I want the class to silently block until the user enters the > correct input. Here is my solution.It's a method named readInt, other methods are simil= ar. import java.util.Scanner; public class StdinReader{ public static void main(String[] args){ Scanner scanner =3D new Scanner(System.in); System.out.println(readInt(scanner)); } public static int readInt(Scanner scanner){ int digit =3D 0; while(true){ try{ digit =3D Integer.parseInt(scanner.next()); break; }catch(Exception e){ } } return digit; } } --- Posted via news://freenews.netfront.net/ - Complaints to n...@netfront.net ---