Results 1 to 14 of 14
  1. #1
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Question How to test whether the entered value is numeric or it contains some characters.

    Hi

    How to check whether the entered value is only numerics and doesnot contain characters in it .


    Thanks in advance

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by renu View Post
    Hi

    How to check whether the entered value is only numerics and doesnot contain characters in it .
    Try to convert the String to a number of the appropriate type (int, long, double) and if the conversion fails (throws an Exception) the String didn't have the required format.

    kind regards,

    Jos

  3. #3
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Question how to test the entered string is 8 or 14 digits

    I also need to test whether the entered String is 8 or 14 digits long.
    If it is not , i need to give the error message and ask again .

    XML Code:
    System.out.println("Please enter the DCN #1 :");
    		String dcn1 = scan.next();
    		System.out.println("The given DCN is :"+dcn1);
    		
    		try{  
    			  new java.math.BigInteger(dcn1);
    			  System.out.println("String is numeric");
    			  int countdcn1 = dcn1.length();
    				System.out.println("The countdcn is :"+countdcn1);
    				while(countdcn1 != 8  || countdcn1 != 14){					
    					System.out.println("Please enter the DCN #1 :");
    					 dcn1 = scan.next();
    					System.out.println("The given DCN is :"+dcn1);
    					try{  						 
    						  new java.math.BigInteger(dcn1);
    						  System.out.println("String is numeric");
    						   countdcn1 = dcn1.length();
    							System.out.println("The countdcn is :"+countdcn1);		
    						}catch(NumberFormatException ex) {
    						  System.out.println("String has invalid characters.");
    						}
    				}			
    			}catch(NumberFormatException ex) {
    			  System.out.println("String has invalid characters.");
    			}
    		}
    Pls help me correct the above code. I wrote this code , and didnot know , how to put it in recussive mode, till i get
    8 or 14 digit input and that input should be numeric from the user.
    Pls help me .
    Last edited by renu; 09-22-2010 at 09:47 PM. Reason: for a reply

  4. #4
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Try to convert the String to a number of the appropriate type (int, long, double) and if the conversion fails (throws an Exception) the String didn't have the required format.

    kind regards,

    Jos
    Thank You Jos , for the reply .Your guidance help me write this piece of code .But i need to also test :-
    Q) to test the entered string is 8 or 14 digits
    I also need to test whether the entered String is 8 or 14 digits long.
    If it is not , i need to give the error message and ask again .


    Pls help me correct the above code. I wrote this code , and didnot know , how to put it in recussive mode, till i get
    8 or 14 digit input and that input should be numeric from the user.
    Pls help me .



    System.out.println("Please enter the DCN #1 :");
    String dcn1 = scan.next();
    System.out.println("The given DCN is :"+dcn1);

    try{
    new java.math.BigInteger(dcn1);
    System.out.println("String is numeric");
    int countdcn1 = dcn1.length();
    System.out.println("The countdcn is :"+countdcn1);
    while(countdcn1 != 8 || countdcn1 != 14){
    System.out.println("Please enter the DCN #1 :");
    dcn1 = scan.next();
    System.out.println("The given DCN is :"+dcn1);
    try{
    new java.math.BigInteger(dcn1);
    System.out.println("String is numeric");
    countdcn1 = dcn1.length();
    System.out.println("The countdcn is :"+countdcn1);
    }catch(NumberFormatException ex) {
    System.out.println("String has invalid characters.");
    }
    }
    }catch(NumberFormatException ex) {
    System.out.println("String has invalid characters.");
    }
    }

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Does your code work?

    Look at the logic for the while condition. What if countdcn1 is 14.
    Then it is NOT equal to 8 (the first condition will be true) so you will enter the loop. Is that what you want to happen?

    Students often have problems using AND logic vs OR in conditions.

    Please put your code in code tags to preserve formatting. Info here: Java Forums - BB Code List

  6. #6
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Does your code work?

    Look at the logic for the while condition. What if countdcn1 is 14.
    Then it is NOT equal to 8 (the first condition will be true) so you will enter the loop. Is that what you want to happen?

    Students often have problems using AND logic vs OR in conditions.

    Please put your code in code tags to preserve formatting. Info here: Java Forums - BB Code List
    Yes Sir, I made a mistake their .ANyhow my recurssive logic doesnot work , for what i wrote .
    As the entered value should be numeric and its count should be either 8 or 14 .Pls help me in acheiving it

    System.out.println("Please enter the DCN #1 :");
    String dcn1 = scan.next();
    System.out.println("The given DCN is :"+dcn1);

    try{
    new java.math.BigInteger(dcn1);
    System.out.println("String is numeric");
    int countdcn1 = dcn1.length();
    System.out.println("The countdcn is :"+countdcn1);
    while(countdcn1 != 8 && countdcn1 != 14){
    System.out.println("Please enter the DCN #1 :");
    dcn1 = scan.next();
    System.out.println("The given DCN is :"+dcn1);
    try{
    new java.math.BigInteger(dcn1);
    System.out.println("String is numeric");
    countdcn1 = dcn1.length();
    System.out.println("The countdcn is :"+countdcn1);
    }catch(NumberFormatException ex) {
    System.out.println("String has invalid characters.");
    }
    }
    }catch(NumberFormatException ex) {
    System.out.println("String has invalid characters.");
    }
    }









  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Can you show what is on the screen when you execute your program? Then add some comments describing what is wrong with the output and what it should be.

  8. #8
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Can you show what is on the screen when you execute your program? Then add some comments describing what is wrong with the output and what it should be.
    Situation 1 :-
    Here i give a string of length 8 digits (satisfied as the length should be 8 digits , but failed to get the numeric value and it should prompt the user to give the value again.which it is not asking .)
    Please enter the DCN #1 :
    a1234567
    The given DCN is :a1234567
    String has invalid characters.
    The program ends here. Instead it should go back and prompt the user again for a value which should be numeric and 8 or 14 characters in length .

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    The program ends here. Instead it should go back
    To make the code go back, you need to be in a loop that doesn't exit until you want it to.

    I can't tell from you unformatted code where the loop ends.
    If you can't format it, add comments on the ending } like the following:
    } // end of while() loop handling invalid input

  10. #10
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default


    System.out.println("Please enter the DCN #1 :");
    String dcn1 = scan.next();
    System.out.println("The given DCN is :"+dcn1);
    int countdcn1 = dcn1.length();
    while(countdcn1 != 8 && countdcn1 != 14){
    System.out.println("Please enter the DCN #1 again :");
    dcn1 = scan.next();
    System.out.println("The given DCN is :"+dcn1);
    try{
    new java.math.BigInteger(dcn1);
    System.out.println("String is numeric");
    }catch(NumberFormatException ex) {
    System.out.println("String has invalid characters.");
    }
    }
    try{
    new java.math.BigInteger(dcn1);
    System.out.println("String is numeric");
    countdcn1 = dcn1.length();
    System.out.println("The countdcn is :"+countdcn1);

    }catch(NumberFormatException ex) {
    System.out.println("String has invalid characters.");
    }
    I am sorry for not formatting earlier.here is the formatted code.
    Pls help me where i should write code to go back till i met the 2 conditions
    (1)numeric value
    2)8 or 14 digits length.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Sorry, you should preview before posting. You used the quote tags not the code tags.

    You need to do a little design work with a tool like a flowchart to figure out your logic before you start coding. Or use pseudo code to work out the looping control.

  12. #12
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Question

    Quote Originally Posted by Norm View Post
    Sorry, you should preview before posting. You used the quote tags not the code tags.

    You need to do a little design work with a tool like a flowchart to figure out your logic before you start coding. Or use pseudo code to work out the looping control.
    Sir , I worked out the flowchart and began coding .Here is the code , but it is not doing what i want it to do.
    I am not able to figure out the error . I need help .

    Java Code:
    package Sample;
               
    import java.io.* ;
    import java.util.*;
     
    class Sample_old {
    
    	public static void main(String[]args) 	{ 
    		int count = 0;
    		Scanner scan = new Scanner(System.in);  
    		System.out.println("enter a number: "); 
    		String num = scan.next();   //Scan the input 
    		boolean abc =isIntNumber(num);
    		System.out.println("The value of abc is :"+abc);
    
    		while(abc){
    			System.out.println("The value of abc is :"+abc);
    			System.out.println("PLS ENTER THE VALUE AGAIN USER2: ");
    			num = scan.next();
    			abc = isIntNumber(num);
    			if(abc == false){
    				count = num.length();
    				System.out.println("The count is :"+count);		 
    				if(count != 8 && count != 14){
    					System.out.println("PLS ENTER THE VALUE AGAIN USER1: "); 
    					num = scan.next();
    					abc =isIntNumber(num);
    					System.out.println("The value of abc is :"+abc);
    				}
    //				abc = true;			
    			}
    		}
    	}
    	public static boolean isIntNumber(String num1){
    		try{
    			Integer.parseInt(num1);
    		}catch(NumberFormatException nfe){
    			return true;
    		}
    		return false;
    	}		
    }

    The output is as follows :-
    enter a number:
    ab123456
    The value of abc is :true
    The value of abc is :true
    PLS ENTER THE VALUE AGAIN USER2:
    1234567890
    The count is :10
    PLS ENTER THE VALUE AGAIN USER1:
    q123456k
    The value of abc is :true
    The value of abc is :true
    PLS ENTER THE VALUE AGAIN USER2:
    12345678912345-->Here this number satisfies the count = 14 and is numeric . So it should exit out of while(abc){....} .But it is not doing .It is again asking the value .Pls help me figure out this problem .I am doing this from yesterday and i have to finish a lot of work today .
    Your help is greatly appreciated .

    The value of abc is :true
    PLS ENTER THE VALUE AGAIN USER2:




  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    It is again asking the value
    Why is it asking for the value again? What logic controls that? What value does 'abc' need to have to exit the loop? Where do you give it that value?

    BTW variable names should be selfdocumentating showing what they are used for. 'abc' is useless here.
    A better name would be: loopingUntilGoodNumber which would be set false if you have a good number and don't need to loop any more.
    Last edited by Norm; 09-23-2010 at 05:57 PM.

  14. #14
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Question

    Quote Originally Posted by Norm View Post
    Why is it asking for the value again? What logic controls that? What value does 'abc' need to have to exit the loop? Where do you give it that value?

    BTW variable names should be selfdocumentating showing what they are used for. 'abc' is useless here.
    A better name would be: loopingUntilGoodNumber which would be set false if you have a good number and don't need to loop any more.
    I am trying all ways , it is not working. Can you pls check the code .

    Java Code:
    import java.io.* ;
    import java.util.*;
     
    class Sample_old {
    
    	public static void main(String[]args) 	{ 
    		int count = 0;
    		Scanner scan = new Scanner(System.in);  
    		System.out.println("enter a number: "); 
    		String num = scan.next();   //Scan the input 
    //		boolean loopUntilGoodNumber = false;//default value
    		boolean loopUntilGoodNumber =isIntNumber(num);
    		System.out.println("The value of loopUntilGoodNumber is :"+loopUntilGoodNumber);
    
    //		loopUntilGoodNumber = true;
    //		while(loopUntilGoodNumber){//loopUntilGoodNumber = true
    //			System.out.println("The value of loopUntilGoodNumber is :"+loopUntilGoodNumber);
    //			System.out.println("PLS ENTER THE VALUE AGAIN USER2: ");
    //			num = scan.next();
    //			loopUntilGoodNumber = isIntNumber(num);
    			while(loopUntilGoodNumber == true){//means the value entered is numeric
    				count = num.length();
    				System.out.println("The count is :"+count);		 
    				if(count != 8 && count != 14){//if true  ask the value again
    					System.out.println("PLS ENTER THE VALUE AGAIN USER1: "); 
    					num = scan.next();
    					loopUntilGoodNumber =isIntNumber(num);
    					System.out.println("The value of loopUntilGoodNumber is :"+loopUntilGoodNumber);
    					}
    				if(loopUntilGoodNumber == true){
    					loopUntilGoodNumber = false;
    				}
    				}
    			}
    //		}
    
    	public static boolean isIntNumber(String num1){
    		try{
    			Integer.parseInt(num1);
    		}catch(NumberFormatException nfe){
    			return false;
    		}
    		return true;
    	}		
    }
    Last edited by renu; 09-23-2010 at 08:02 PM.

Similar Threads

  1. Read a single character entered
    By mcmb03 in forum New To Java
    Replies: 2
    Last Post: 01-31-2010, 10:30 PM
  2. JXL, Numeric data issue
    By only2008 in forum Java Servlet
    Replies: 0
    Last Post: 02-27-2009, 11:36 AM
  3. get numeric value from a text field
    By Lehane_9 in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 03:19 AM
  4. calendar of 12months when year is entered
    By veena in forum New To Java
    Replies: 16
    Last Post: 05-06-2008, 05:03 AM
  5. Allowing only numeric values in a TextField
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-01-2008, 10:08 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
  •