DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Whigot Guest

    error checking for int


    hi im wondering how u error check to make sure that the getInt method only
    retrieves integrs and nothign else..eg no alphabets
    here r the methods

    //-------------------------------------------------------------
    public static String getString() throws IOException
    {
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    String s = br.readLine();
    return s;
    }

    //-------------------------------------------------------------
    public static int getInt() throws IOException
    {
    String s = getString();
    return Integer.parseInt(s);
    }

    //--------------------------------------------------------------

  2. #2
    Paul Clapham Guest

    Re: error checking for int

    String s = getString();
    try {
    return Integer.parseInt(s);
    } catch(NumberFormatException nfe) {
    // the input was not an integer
    return 0;
    }

    "Whigot" <whigot90@hotmail.com> wrote in message
    news:3bd29825$1@news.devx.com...
    >
    > hi im wondering how u error check to make sure that the getInt method only
    > retrieves integrs and nothign else..eg no alphabets
    > here r the methods
    >
    > //-------------------------------------------------------------
    > public static String getString() throws IOException
    > {
    > InputStreamReader isr = new InputStreamReader(System.in);
    > BufferedReader br = new BufferedReader(isr);
    > String s = br.readLine();
    > return s;
    > }
    >
    > //-------------------------------------------------------------
    > public static int getInt() throws IOException
    > {
    > String s = getString();
    > return Integer.parseInt(s);
    > }
    >
    > //--------------------------------------------------------------




Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links