Results 1 to 5 of 5
  1. #1
    QJack's Avatar
    QJack is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default How can I determine the type of an input

    What I mean is this :
    I have to write a program that gets an integer and prints it on the screen. Otherwise, an error message should be printed.

    10x in advance. :)

    EDIT :
    So far what I have done is this :
    Java Code:
    import java.util.Scanner;
    
    public class f{
    	public static boolean isItInt(String inp)
    	{
    		char dgts[]={'0','1','2','3','4','5','6','7','8','9'};
    		int i=0,j=0,count=0;
    		for (i=0; i<inp.length(); i++)
    		{
    			for(j=0;j<dgts.length;j++)
    				if (inp.charAt(i)==dgts[j])
    					count++;
    		}
    		if (count==inp.length())
    			return true;
    		else
    			return false;
    	}
    
    	public static void main(String argv[]){
    		Scanner s = new Scanner(System.in);
    		String p=s.next();
    		System.out.println(p);
    		boolean x=isItInt(p);
    		System.out.println(x);
    	}
    }
    But I am sure there is something easier than this, so let me know.
    Last edited by QJack; 03-17-2010 at 02:30 PM.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Integer.parseInt("1234")
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    hi

    Quote Originally Posted by QJack View Post
    But I am sure there is something easier than this, so let me know.

    in java when a number format convertion goes wrong then a exception is thrown. so try this peace of code

    Java Code:
    public class ExceptionExample {
    
    	public static void main(String[] args) {
    		String str = "10000";
    		try {
    			System.out.println(Long.parseLong(str));
    		} catch (NumberFormatException nfe) {
    			System.out.println("NumberFormatException occured ");
    			nfe.printStackTrace();
    		}
    	}
    }
    run the code with different str values to see what happens.

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    when you are accepting vis scanner use this method ...it will accept only integer.No need to have checking method for int.
    nextInt()
    hasNextInt()
    Ramya:cool:

  5. #5
    QJack's Avatar
    QJack is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

Similar Threads

  1. [SELENIUM] Retrieve the value of a hidden input type
    By gecko753 in forum Web Frameworks
    Replies: 1
    Last Post: 11-04-2009, 03:32 PM
  2. retain value of input type file in a jsp file while being dynamically generated
    By nidhi c in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2009, 02:21 AM
  3. Input type Check box
    By jeeva in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 02-17-2009, 02:36 PM
  4. Input type Check box
    By jeeva in forum Advanced Java
    Replies: 1
    Last Post: 02-05-2009, 04:04 PM
  5. determine the type of card hand
    By javaman2 in forum New To Java
    Replies: 7
    Last Post: 01-04-2009, 05:11 PM

Posting Permissions

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