3 Replies - 2008 Views - Last Post: 18 April 2010 - 02:55 PM Rate Topic: -----

#1 gymangel812  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 116
  • Joined: 15-February 10

Unchecked or unsafe operations Error

Posted 18 April 2010 - 02:34 PM

I'm getting one errors when I compile the code below:
" Note: ..\Password.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error"
The code is from the book so I've mistyped something(s) I just can't figure out what...
import java.util.*;

public class Password
{
        final static int MIN_SIZE = 6;
        final static int MAX_SIZE = 15;
                  static int maxHistory = 4;
                  static int expiresNotifyLimit = 3;

        private int maxUses = 120;
        private int remainingUses = maxUses;
        private boolean autoExpires = true;
        private boolean expired = false;

        private ArrayList pswdHistory;


        //Constructors for objects of class Password
        public Password(String newPassword) throws Exception
        {
                pswdHistory = new ArrayList(maxHistory);
                set(newPassword);
        }

        public Password(String newPassword, int numMaxUses) throws Exception
        {
                pswdHistory = new ArrayList(maxHistory);
                maxUses = numMaxUses;
                remainingUses = numMaxUses;
                set(newPassword);
        }

        public Password(String newPassword, boolean pswdAutoExpires) throws Exception
        {
                pswdHistory = new ArrayList(maxHistory);
                autoExpires = pswdAutoExpires;
                set(newPassword);
        }

        public Password(String newPassword, int numMaxUses, boolean pswdAutoExpires) throws Exception
        {
                pswdHistory = new ArrayList(maxHistory);
                maxUses = numMaxUses;
                remainingUses = numMaxUses;
                autoExpires = pswdAutoExpires;
                set(newPassword);
        }

        public boolean getAutoExpire()
        {
                return autoExpires;
        }

        public void setAutoExpires(boolean autoExpires)
        {
                this.autoExpires = autoExpires;
                if(autoExpires)
                        remainingUses = maxUses;
  		}

		public boolean isExpired()
		{
				return expired;
		}

		public void setExpired(boolean newExpired)
		{
				expired = newExpired;
		}

		public int getExpiresNotifyLimit()
		{
				return expiresNotifyLimit;
		}

		public void setExpiresNotifyLimit(int newNotifyLimit)
		{
				if(newNotifyLimit >= 2 && newNofityLimit <=20)
						expiresNotifyLimit = newNotifyLimit;
		}

		public int getMaxHistory()
		{
				return maxHistory;
		}

		public void setMaxHistory(int newMaxHistory)
		{
				int overage = 0;
				if(newMaxHistory >= 1 && newMaxHistory <= 10)
				{
						maxHistory = newMaxHistory;
						overage = getHistorySize() - maxHistory;
						if(overage > 0)							// if size > max allowed
						{
								do {
										pswdHistory.remove(0);	// then remove overage number
										overage--;				// of the oldest pswds in the list
								} while(overage > 0);

								pswdHistory.trimToSize();		// resize capacity to max allowed
						}
				}
		}

		public int getRemaingUses()
		{
				return remainingUses;
		}

		public int getHistorySize()
		{
				return pswdHistory.size();
		}

		public boolean isExpiring()
		{
				boolean expiring = false;

				if(autoExpires && remainingUses <= expiresNotifyLimit)
						expiring = true;

				return expiring;
		}

		// sets password to a new value; keeps current & previous values in history up to max number
		public void set(String pswd) throws Exception
		{
				String encryptPswd;
				boolean pswdAdded = true;

				pswd = pswd.trim();				// remove any leading, trailing white space
				verifyFormat(pswd);				// verify password was encrypted properly
				encryptPswd = encrypt(pswd);	// convert to encypted form

				if(!pswdHistory.contains(encryptPswd))		// if pswd not recently used in list
				{
					if(pswdHistory.size() == maxHistory)	// if list is at max size
							pswdHistory.remove(0);			// remove 1st, oldest, pswd from list

					pswdAdded = pswdHistory.add(encryptPswd);	// add new pswd to end of arrayList

					if(!pswdAdded)							// should never happen
							throw new Exception("Internal list error - Password not accepted");

					if(expired)					// if pswd has expired
							expired = false;	// reset to not expired

					if(autoExpires)						// if pswd expires,
							remainingUses = maxUses;	// reset uses to max
				}
				else
						throw new Exception("Password recently used.");
		}

		// validates entered password against most recently saved value
		public void validate(String pswd) throws Exception
		{
				String encryptPswd;
				String currentPswd;
				int currentPswdIndex;

				verifyFormat(pswd);				// verify password was entered properly
				encryptPswd = encrypt(pswd);	// convert to encypted form

				if(!pswdHistory.isEmpty())		// at least 1 password entry is in history
				{
						currentPswdIndex = pswdHistory.size()-1;
						currentPswd = (String)pswdHistory.get(currentPswdIndex);

						if(!encryptPswd.equals(currentPswd))	// if not most recent password
								throw new Exception("Password is invalid");

						if(expired)
								throw new Exception("Password has expired - please change");

						if(autoExpires)
						{
								--remainingUses;
								if(remainingUses <= 0)
										expired = true;
						}
				}
				else
					throw new Exception("No password on file - list corrupted!"); // should never happen


		}

		// verifies password has proper format
		private void verifyFormat(String pswd) throws Exception
		{
				boolean numFound = false;

				if(pswd.length() == 0)
						throw new Exception("No password provided!");

				if(pswd.length() < MIN_SIZE)
						throw new Exception("Password must be at least "+MIN_SIZE+ " characters in length");

				if(pswd.length() > MIN_SIZE)
						throw new Exception("Password connot be greater than "+MIN_SIZE+ " characters in length");

				// scan through password to find if at least 1 number is used
				for(int i=0; i <pswd.length() && !numFound; ++i)
						if(Character.isDigit(pswd.charAt(i)))
								numFound = true;

				if(!numFound)
						throw new Exception("Password is invalid - must have at least one numeric digit");
		}

		// encrypts original password returning new encrypted String
		private String encrypt(String pswd)
		{
				StringBuffer encryptPswd;
				int pswdSize = 0;
				int midpoint = 0;
				int hashCode = 0;

				// swap first and last half of password
				pswdSize = pswd.length();
				midpoint = pswdSize/2;
				encryptPswd = new StringBuffer(pswd.substring(midpoint) // get last half of pswd
								+ pswd.substring(0,midpoint)); // and concatenate first half

				encryptPswd.reverse(); // reverses order of characters in password

				for(int i=0; i < pswdSize; ++i)
						encryptPswd.setCharAt(i, (char)(encryptPswd.charAt(i) & pswd.charAt(i)) );

				hashCode = pswd.hashCode(); // hash code for original password
				encryptPswd.append(hashCode);

				return encryptPswd.toString();
		}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Unchecked or unsafe operations Error

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,273
  • Joined: 27-December 08

Re: Unchecked or unsafe operations Error

Posted 18 April 2010 - 02:49 PM

The reason you are getting this warning is b/c you do not declare the Generic type for your ArrayList: private ArrayList pswdHistory;. As of Java 1.5, Generics was added for things like allowing ArrayLists to store specific types of elements, like ArrayList<Integer>, ArrayList<String>, etc., rather than subclassing ArrayList to store these types. So in Java 1.4, you would have class IntegerList extends ArrayList.
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Re: Unchecked or unsafe operations Error

Posted 18 April 2010 - 02:52 PM

It's telling you that because you didn't tell it what types the ArrayList is holding. That's not necessarily bad, if you know that and are just ignoring it, but it can lead to run-time exceptions if bad casts are made.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,273
  • Joined: 27-December 08

Re: Unchecked or unsafe operations Error

Posted 18 April 2010 - 02:55 PM

With Generics, the ArrayList handles the casting for you when you go to access the elements. While the message may just be a warning, and not an error, it is always good practice to specify Generic type, even if it is just List<Object>.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1