check if the input is integer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sang
    New Member
    • Sep 2006
    • 83

    check if the input is integer

    Hi

    I am writing a application program that checks if the input is integer, if it is integer print the integer value only. i have no idea about that please any one help me.

    Thanks
    Sang
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by sang
    Hi

    I am writing a application program that checks if the input is integer, if it is integer print the integer value only. i have no idea about that please any one help me.

    Thanks
    Sang
    Use Exception handling
    as in
    Code:
    String input = ....
    try {
    	int x = Integer.parseInt(input);
    	System.out.println(x);
    }
    catch(NumberFormatException nFE) {
    	System.out.println("Not an Integer");
    }

    Comment

    • sang
      New Member
      • Sep 2006
      • 83

      #3
      Thanks for your reply.

      This coding is print if the input is integer otherwise not printed. I am trying to get only integer values i am not able to do this by using the isNumber() but i am got the error.

      so,please give me the code for

      if the input is like this "There is 3 apples in 1 tree" the output is only 1 & 3

      that is only print the integer.

      Thanks
      Sang

      Comment

      • D_C
        Contributor
        • Jun 2006
        • 293

        #4
        If you are going to search for a number within a string, then print the number, you don't even need to convert it to an integer. If you are using negatives, be sure to modify the if statement to accept minus sign as well as digit.
        Code:
        for each character in the string
        {
          if(character is a digit)
          {
            while(next character is a digit); 
            print the substring of all digits
          }
        }

        Comment

        • sang
          New Member
          • Sep 2006
          • 83

          #5
          Thanks for your advice but i am not able to do that because i am new to java for that reason i am strugle in this.

          The following code is copmlied but it is not correct please correct it and then sent to me

          import java.io.*;
          class str {
          public boolean isNumeric(Strin g input){
          try {
          char[] ch=charArray(in put);
          for( int i=0 ; i<input.length( ) ; i++)
          {
          if(Character.is Number)
          {
          System.out.prin tln(i);
          }
          else { return false;}
          }
          }catch(NumberFo rmatException nfe) {
          System.out.prin tln(nfe);
          }
          }
          public static void main(String arg[]) {
          int c = str.isNumeric(" java 123");
          System.out.prin tln(c);
          }
          }

          Thanks
          Sang

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by sang
            Thanks for your advice but i am not able to do that because i am new to java for that reason i am strugle in this.

            The following code is copmlied but it is not correct please correct it and then sent to me

            import java.io.*;
            class str {
            public boolean isNumeric(Strin g input){
            try {
            char[] ch=charArray(in put);
            for( int i=0 ; i<input.length( ) ; i++)
            {
            if(Character.is Number)
            {
            System.out.prin tln(i);
            }
            else { return false;}
            }
            }catch(NumberFo rmatException nfe) {
            System.out.prin tln(nfe);
            }
            }
            public static void main(String arg[]) {
            int c = str.isNumeric(" java 123");
            System.out.prin tln(c);
            }
            }

            Thanks
            Sang
            An initial attempt would be

            Code:
            public class Numbers {
            	public static void main(String[] args) {
            		String str = "java5.02ds77dfsff";
            		char[] all = str.toCharArray();
            		String numbers = "";
            		for(int i = 0; i < all.length;i++) {
            			if(Character.isDigit(all[i])) {
            				numbers = numbers + all[i];
            			}
            		}
            		System.out.println(numbers);
            
            	}
            }

            Comment

            • sang
              New Member
              • Sep 2006
              • 83

              #7
              Thanks a lot I find out my mistakes i will change it

              Thank you for your guidens.

              Comment

              • kadsoft

                #8
                public static boolean isNumeric(Strin g aStringValue) {
                Pattern pattern = Pattern.compile ( "\\d+" );

                Matcher matcher = pattern.matcher (aStringValue);
                return matcher.matches ();
                }

                Comment

                • abhishek kush
                  New Member
                  • Nov 2011
                  • 1

                  #9
                  import java.io.*;
                  class abhi
                  {
                  public static void main(String args[])throws IOException
                  {
                  DataInputStream d=new DataInputStream (System.in);
                  int i,as,l;
                  String na;
                  na=d.readLine() ;
                  l=na.length();
                  char ch;
                  for(i=0;i<l;i++ )
                  {
                  ch=na.charAt(i) ;
                  as=(int)ch;
                  if(as>=48&&as<= 57)
                  System.out.prin tln(ch);
                  }
                  }
                  }

                  Comment

                  • venkat4u
                    New Member
                    • Mar 2013
                    • 1

                    #10
                    hi,
                    you can do like this way also

                    Code:
                    String st=input
                    
                    Integer i=new Integer(st)
                    
                    if(i instance of Integer){
                    int j=i.intValue();
                    System.out.println(j);}
                    Last edited by Rabbit; Mar 13 '13, 04:15 PM. Reason: Please use code tags when posting code.

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      That won't even compile. Please only suggest things when you are sure you know what you are talking about.

                      Comment

                      • Karidev
                        New Member
                        • Oct 2016
                        • 1

                        #12
                        The best solution is to use a Pattern like kadsoft suggested before.

                        Comment

                        Working...