Results 1 to 3 of 3
  1. #1
    erthbound0 is offline Member
    Join Date
    Dec 2011
    Rep Power

    Default Question about If statements

    I wrote this simple program and I want to know how to write an if statement that allows me to prompt the user with a message if he types in an integer where he should type his name and a String where he should type his age. Here is the program

    Java Code:
    import java.util.Scanner;
    class InputDemo {
    	public static void main(String args[]){
    		String name;
    		int age;
    		System.out.println("Enter name and age");
    		Scanner in = new Scanner(;
    		name = in.nextLine();
    		age = in.nextInt();

  2. #2
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Rep Power

    Default Re: Question about If statements

    Java has a String.isNumeric(string) operator that returns true or false. It will tell you if your name string is a number. As for entering a name where a number should be, the code would throw an exception.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power

    Default Re: Question about If statements

    There is no isNumeric() method in the standard Java libraries (of any class).

    if he types in an integer where he should type his name and a String where he should type his age
    You need to be precise about the checks you are going to do. For instance is "3.14159" a valid name? (it's not an integer) or "42.0"? (not a valid Java int literal, although mathematicians would say it names an integer.) or "B1LL"? Which (if any) of the following are valid ages: "0", "-1", "0x1A", "200", "42."?

    I mention all this not (only) to be a picky b@stard but because such validation takes work, and you want to be sure that it's not wasted effort. More important, perhaps, is why you would bother validating the data in this way. Typically that's because other code will depend on the checks you make. For instance other code may do numerical calculations based on a person's age, and that might fail if they didn't enter a well formed integer numeral. So you validate the data and document precisely the guarantees on which the rest of the code can depend.


    The String class has methods that might be useful. So does the Character class: perhaps the previous poster was thinking of Character.isDigit().

    Then there's the Integer class. It has a method parseInt() which will throw an exception if the it can't interpret (in a precisely documented way) the string argument as an integer value. Generally people are advised not to use exceptions for normal "control of flow", but this is one occasion where you often see it used that way. Other numeric classes - Double, BigInteger etc - offer something similar.

    And there are the numeric operations themselves which might be used to check input for validity.

    And I - deliberately - don't mention the f.*!](n) regexes.


    Come to think of it, there's an embarrassment of riches here. Read about what's possible (especially String, Character and Integer classes), think about what's wanted and try to put the two together.
    Last edited by pbrockway2; 12-08-2011 at 11:37 PM.

Similar Threads

  1. Help with IF Statements and GUI
    By university123 in forum New To Java
    Replies: 8
    Last Post: 10-14-2010, 04:38 AM
  2. if else if statements
    By Allspark in forum New To Java
    Replies: 9
    Last Post: 09-28-2010, 06:50 PM
  3. question about how if statements work
    By gib65 in forum Advanced Java
    Replies: 3
    Last Post: 08-23-2010, 10:45 PM
  4. age: using if statements
    By yasmin k in forum New To Java
    Replies: 2
    Last Post: 10-04-2009, 09:50 PM
  5. Replies: 2
    Last Post: 11-22-2008, 05:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts