9 Digit Validation

Getting Java to only accept 9 digits

Page 1 of 1

5 Replies - 8748 Views - Last Post: 15 August 2009 - 01:19 PM Rate Topic: -----

#1 defiance5050  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-August 09

9 Digit Validation

Posted 15 August 2009 - 06:01 AM

Hi everyone! I have been working on an assignment for a Java programming course and I am pulling out my hair trying to figure out how to work with digit validation. I have been looking all over the internet for the past couple of days, and I have only found help that is too advanced for my current knowledge. I am trying to get a Java program to only accept 9 digits. If the user does not input 9 digits, a while loop will kick it back and the user will have to enter any 9 digit number. This course is the first time I have been exposed to programming so everything I do is a learning experience. The while loop I have been trying to work with is below. Any tips or explanations would be greatly appreciated!

System.out.println("Please enter your serial number:");
		int serialNo;
		serialNo = keyboard.nextInt();
		while (serialNo <= 999999999)
		{ 
			System.out.print("Your input was not valid. Input must be a 9 digit number:" + "\n");
			serialNo = keyboard.nextInt();
		}

This post has been edited by defiance5050: 15 August 2009 - 06:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: 9 Digit Validation

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: 9 Digit Validation

Posted 15 August 2009 - 06:11 AM

View Postdefiance5050, on 15 Aug, 2009 - 12:01 PM, said:

Hi everyone! I have been working on an assignment for a Java programming course and I am pulling out my hair trying to figure out how to work with digit validation. I have been looking all over the internet for the past couple of days, and I have only found help that is too advanced for my current knowledge. I am trying to get a Java program to only accept 9 digits. If the user does not input 9 digits, a while loop will kick it back and the user will have to enter any 9 digit number. This course is the first time I have been exposed to programming so everything I do is a learning experience. The while loop I have been trying to work with is below. Any tips or explanations would be greatly appreciated!

System.out.println("Please enter your serial number:");
		int serialNo;
		serialNo = keyboard.nextInt();
		while (serialNo <= 999999999)
		{ 
			System.out.print("Your input was not valid. Input must be a 9 digit number:" + "\n");
			serialNo = keyboard.nextInt();
		}

you could read a string, check it is 9 characters long and if so convert it to an integer, e.g.
		Scanner in=new Scanner(System.in);
		String s;
		// read a string of 9 characters
		do
		  {
		   s=in.next();
		  }
		while(s.length() != 9);
		// convert 9 characters to an integer
		in = new Scanner(s);
		int result=in.nextInt();
		System.out.println("result " + result);



you probably need to add some error checking in case the user enter non digits in the string
Was This Post Helpful? 0
  • +
  • -

#3 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 89
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: 9 Digit Validation

Posted 15 August 2009 - 11:52 AM

Quote

you probably need to add some error checking in case the user enter non digits in the string


Scanner in=new Scanner(System.in);
		String s;
		// read a string of 9 characters
		do
		  {
			 try{
		   s=in.next();
			   }
			 catch(InputMismatchException ex){
					  System.out.println("Invalid input. Please enter integers.");
					  in.nextLine();
				  }
		  }
		while(s.length() != 9);
		// convert 9 characters to an integer
		in = new Scanner(s);
		int result=in.nextInt();
		System.out.println("result " + result);



Is this code correct?
Was This Post Helpful? 0
  • +
  • -

#4 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: 9 Digit Validation

Posted 15 August 2009 - 12:47 PM

View PostAntonWebsters, on 15 Aug, 2009 - 05:52 PM, said:

Quote

you probably need to add some error checking in case the user enter non digits in the string


Scanner in=new Scanner(System.in);
		String s;
		// read a string of 9 characters
		do
		  {
			 try{
		   s=in.next();
			   }
			 catch(InputMismatchException ex){
					  System.out.println("Invalid input. Please enter integers.");
					  in.nextLine();
				  }
		  }
		while(s.length() != 9);
		// convert 9 characters to an integer
		in = new Scanner(s);
		int result=in.nextInt();
		System.out.println("result " + result);



Is this code correct?

goot try, but you need to check that the nextInt() converts an integer

try
	Scanner in=new Scanner(System.in);
	int result;
	while(true)
	  {
	  try
		{
		String s;
		// read a string of 9 characters
		do
		   {
		   System.out.print("enter a 9 digit number ?");
		   s=in.next();
		   }
		while(s.length() != 9);
		// convert 9 characters to an integer
		Scanner inInt = new Scanner(s);
		result=inInt.nextInt();
		break;
		}
	  catch(InputMismatchException ex){
				System.out.println("Invalid input. Please enter integers.");
				in.nextLine();
		  }
	  }
	System.out.println("result " + result);



This post has been edited by horace: 15 August 2009 - 12:51 PM

Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: 9 Digit Validation

Posted 15 August 2009 - 01:10 PM

Read the input as a String
Trim it
and translate it o an int with error checking

String str = scanner.nextLine();
str = str.trim();
if(str.length() != 9) {
	// errror
}
int value;
try {
   value = Integer.parseInt(str);
}
catch(NumberFormatException e) {
   // not all digits
}


Was This Post Helpful? 1
  • +
  • -

#6 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: 9 Digit Validation

Posted 15 August 2009 - 01:19 PM


	public static String digit(String str){
		
		int l=str.length();
		String line="";
		
				if(l==8)
			
			line=str;
		else
			line="ERROR";
		
		return line;
	}
	
	public static void main(String args[]){
		
		String str;
		Scanner fox=new Scanner(System.in);
		
		System.out.print("Enter 9 Digit number: ");
		str=fox.nextLine();
		
		System.out.println(digit(str));
		
	}
}



this essentially checks if there are 9 digits, but it returns it as a string. if you want to actually convert it to an array of ints, you would have to first probably convert it to an array of chars, then convert them to ints. but if all you want to do is check it there are indeed 9 elements, then the code will do that.

hope that helps :)

This post has been edited by computerfox: 15 August 2009 - 01:50 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1