4 Replies - 754 Views - Last Post: 29 February 2012 - 05:39 PM Rate Topic: -----

#1 dn2371  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-February 12

Having trouble validating name and zip code

Posted 29 February 2012 - 03:03 PM

I assumed that it was going to be really easy for me to do this but I guess I was wrong. I have to validate the name of a person (last, first). It must have at least one space and one comma and I must check that all characters are letters. The zip code has to be exactly 5 digits long. For some reason, they are both not validating correctly. Any help is appreciated. Thank you.

Data Class Definition

public class Account
{
	String name, zipCode;
	
	public boolean setName(String iName)
	{
		if (validateName(iName))
		{
			name = iName;
			return true;
		}
		
		return false;	
	}
	
	public boolean setZipCode(String iZipCode)
	{
		if (validateZipCode(iZipCode))
		{
			zipCode = iZipCode;
			return true;
		}
		
		return false;
	}
	
	public boolean validateName(String iName)
	{		
		if (iName.indexOf(" ") < 0)
			return false;
		if (iName.indexOf(",") < 0)
			return false;
				
		String temp1;
		String temp2;
		
		temp1 = iName.replace(",", " ");
		temp2 = temp1.trim();
		for (int i = 0; i < iName.length(); i++)
			if (!Character.isLetter(iName.charAt(i)))
				return false;
				
		return true;
	}
	
	public boolean validateZipCode(String iZipCode)
	{
		if (iZipCode.length() != 5)
			return false;
		for (int i = 0; i < iZipCode.length(); i++)
			if (!Character.isDigit(iZipCode.charAt(i)))
				return false;
		
		return true;
	}
}



Implementation

import javax.swing.JOptionPane;
public class AccountSetup
{
	public static void main(String[] args)
	{
		do{
			Account account = input();
			JOptionPane.showMessageDialog(null, account.toString());
		}while(JOptionPane.showConfirmDialog(null, "Would you like to set up another account?") == JOptionPane.YES_OPTION);
	}
	
	public static Account input()
	{
		Account acct = new Account();
		while (!acct.setName(JOptionPane.showInputDialog(null, "Enter your name (last, first):")))
			JOptionPane.showMessageDialog(null, "Invalid name - must have 1 comma, and at least one space between the " +
													"first and last name, and can ONLY be letters.");
		while (!acct.setZipCode(JOptionPane.showInputDialog(null, "Enter your zipcode (five digits):")))
			JOptionPane.showMessageDialog(null, "Invalid zipcode - must be only 5 digits, and can ONLY contain digits.");
			
		return acct;
	} 
}



Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble validating name and zip code

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Having trouble validating name and zip code

Posted 29 February 2012 - 03:28 PM

Quote

if (!Character.isLetter(iName.charAt(i)))
  return false;


will always return false. It started out with one space then you turned the comma into another
Was This Post Helpful? 0
  • +
  • -

#3 dn2371  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-February 12

Re: Having trouble validating name and zip code

Posted 29 February 2012 - 04:07 PM

sorry! lines 39-40 is supposed to look like this!
        for (int i = 0; i < temp2.length(); i++)
            if (!Character.isLetter(temp2.charAt(i)))


Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Having trouble validating name and zip code

Posted 29 February 2012 - 04:43 PM

Makes no difference. fyi trim() only removes space at each end of a String, not throughout it.
Was This Post Helpful? 1
  • +
  • -

#5 dn2371  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-February 12

Re: Having trouble validating name and zip code

Posted 29 February 2012 - 05:39 PM

Thanks everyone! I understand now what trim() does.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1