Results 1 to 12 of 12
  1. #1
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Exclamation help with a logic error

    Cant figure out why my isFormal() it keep setting boolean voteValue of all votes to false even legit ones. To check if the vote is legit i compare the string array elements to ArrayList<String> elements if they are in arrayList removes the element from the arrayList.Its suppose to help me find dupicate in the string array[]. Is this a good way of checking for dupicates. Any help would be much appreciated thank you in advance.
    ps sorry if this does not make any sense.

    Java Code:
    import java.util.*;
    
    
    public class Vote 
    {
    	//private attributes
    	private int numOfCandidates; //number of candidates running for a seat
    	private String [] voteTaken;// The votes will be read into this array
    	private boolean isVoteFormal;// Name of a test value to see if a vote is formal or not
    	private String vote; //Use to store the vote numbers
    	private boolean voteValue;//this value is used
    
    	
    	//class constructors
    	public Vote(String vote,int numOfCandidates)
    	{
    		setNumOfCandidates(numOfCandidates);
    		voteTaken = new String[getNumOfCandidates()];
    		setVoteTaken(voteTaken);
    		
    		setVote(vote);
    		isFormal();
    		if(getIsVoteFormal() == true)
    		{
    			setVoteValue(true);
    		}
    		if(getIsVoteFormal() == false)
    		{
    			setVoteValue(false);
    		}
    	 }
    	//-----------------------------
    	public Vote()/*blank constructor its purpose is so i can access the 
    	class methods directly*/
    	{
    		
    	}
    	//---------------------------------------
    	//Vote methods
    	public void isFormal()
    	{
    		
    		ArrayList<String> dupiChecker = new ArrayList<String>(getNumOfCandidates());
    		int trueOrFalse = 0; //num 0 indicating true num 1 indicating false 
    		String [] tempArray = new String[getNumOfCandidates()];
    		boolean value = false;
    		try
    		{
    			
    		//adding vote to temp array
    		for(int i = 0; i < tempArray.length;i++)
    		{
    			tempArray[i] = getVote();
    			dupiChecker.add(String.valueOf(i+1));
    		}
    		
    		//now checking for dupicates in array
    		while(!value == true)
    		{
    			int k = 0;
    			int j = 0;
    			
    				while(!dupiChecker.isEmpty())
    				{	
    					if(tempArray[k].equals(dupiChecker.get(j)))/* checks if tempArray element
    					vote is equal to dupiChecker arrayList element if it is it removes
    					the vote from dupiChecker arrayList and then goes to next element
    					in temp array.*/
    					{
    						dupiChecker.remove(tempArray[k]);
    						k++;
    					}
    					if(!tempArray[k].equals(dupiChecker.get(j)))/*checks to see if the vote in tempArray != to dupiCheckerVote 
    					then continue cycling through dupiChecker ArrayList*/
    					{
    						j++;
    					}
    					if(!dupiChecker.contains(tempArray[k]))/*if the string 
    					is not in there at all it exit the while loop*/
    					{
    							trueOrFalse = 1;
    							break;
    					}	
    					
    				}//end of inner while
    			switch(trueOrFalse)/*This switch set the value of private variable 
    			isVoteFormal had to use a int for this switch as they dont take
    			boolean argument :( */
    			{
    				case 0: setIsVoteFormal(true);
    						/*This for statement copies tempArray to voteTakenArray*/
    						for(int i = 0;i < getVoteTaken().length;i++)
    						{
    							getVoteTaken()[i] = tempArray[i];
    						}
    						value = true;
    						break;
    			
    				case 1:	setIsVoteFormal(false);
    						/*this*/
    						value = true;
    						break;
    				
    				default : System.out.printf("Class Vote Switch not working exiting system\n");
    						  System.exit(0);
    			}
    			
    			if(value == true)//test if value is equal to true
    			{
    				break;
    			}
    		}//end of outer while	
    		}//end of try
    		catch(ArrayIndexOutOfBoundsException aioobe)
    		{
    			System.out.printf("Array out of bounds in class Vote " +
    			"isFormal method\n");
    		}
    		catch(Exception e)
    		{
    			System.out.printf("Error in class Vote isFormal method\n");
    		}
    		
    	}//end of isFormal method
    	//----------------------------------------
    	
    	//get & set methods
    	public void setNumOfCandidates(int numOfCandidates) 
    	{
    		this.numOfCandidates = numOfCandidates;
    	}
    	public int getNumOfCandidates() 
    	{
    		return this.numOfCandidates;
    	}
    	//-------------------------------------------
    	private void setVoteTaken(String [] voteTaken) 
    	{
    		this.voteTaken = voteTaken;
    	}
    	public String [] getVoteTaken() 
    	{
    		return this.voteTaken;
    	}
    	//-------------------------------------
    	public void setIsVoteFormal(boolean isVoteFormal) 
    	{
    		this.isVoteFormal = isVoteFormal;
    	}
    	public boolean getIsVoteFormal() 
    	{
    		return this.isVoteFormal;
    	}
    	//---------------------------------------
    	public void setVote(String vote) 
    	{
    		this.vote = vote;
    	}
    	public String getVote() 
    	{
    		return this.vote;
    	}
    	//----------------------------------------
    	public void setVoteValue(boolean voteValue) 
    	{
    		this.voteValue = voteValue;
    	}
    	public boolean getVoteValue() 
    	{
    		return voteValue;
    	}
    	
    	//-----------------------------------------
    	public String toString()//show Class vote variables in String format.
    	{
    		return String.format("The vote:%s,The voteValue:%s,The voteValue%s,%d\n",getVote(),getVoteValue(),getIsVoteFormal(),getNumOfCandidates());
    	}
    	//----------------------------------------
    	public boolean equals(Object theObject)/*equals method for class Vote*/
    	{
    		if(theObject == null)
    		{
    			return false;
    		}
    		if(getClass() != theObject.getClass())
    		{
    			return false;
    		}
    		Vote aVote = (Vote)theObject;
    		return (getVoteTaken().equals(aVote.getVoteTaken()));
    		
    	}
    	
    }//end of class vote

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    You have a lot of these:
    if (getIsVoteFormal() == true)

    That "== true" is totally unecessary since the thing is already a boolean.
    It won't correct your problem, of course.

    I really don't understand what you're trying to check or validate here. I assume Vote represents a single vote? Is it a candidate rating type of vote, hence the candidate array? So what are you actually checking...in English, rather than Java. What is the idea?

  3. #3
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Exclamation

    Yes vote is a representation of a single vote , no candidates is the number of candidates participating in the election which i use to set the size of my array . im trying to validate a String array and to make sure its a legit vote i have to check that the array DOES NOT have any duplicate strings of the same number in the array but the strings numbers can be in any order.
    eg

    legitimate vote
    {1,2,3,4,5,6}
    {5,6,3,4,2,1}
    {3,5,4,1,6,2}

    bad vote
    {1,1,2,4,5,2}
    {NODAM,X,X, ,y}
    {2,2,3,4,4,6}

    to make sure the vote didn't have any duplicates i used an arrayList of integer
    to check against the numbers in the array. Then it would remove the element from the arrayList. so if it were a legit vote then there would be no element in the arrayList if that makes any sense.The reason why i set boolean values is because it would be easier to filter and separate the good vote from the bad votes. sorry that the best explanation i can give

    ps
    It is base off the Australian preferential voting system

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    Right...*ping* as the light goes on.

    So you're checking they've filled in the ballot correctly, essentially.

    OK, so your isFormal() wants to create an ArrayList<Integer>. I'd use that rather than String, since they are Integers. Fill it with 1 to numCandiates as you are now.

    Now, where you're going wrong is setting the tempArray[] elements to getVote, which is simply setting them all to the value of vote. I'm guessing here, but is vote supposed to be a String with all the numbers in it? Is the format as you've given?
    ie "{2,3,4,5,6,1}"?

    If so then you need to turn that into an int[] (in the constructor possibly?). See if you can figure that bit out. Because that will do the initial validation (it's only numbers, and not letters, and there are numCandidate numbers in there).

    Then all you do is cycle through the int[], removing the numbers from the ArrayList. If it fails to remove an element (look at the remove() method) then you know there was a duplicate.

  5. #5
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Default

    mad s*** i give it a try thanks:D

  6. #6
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Default

    oh crap now i got this error
    Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at Vote.<init>(Vote.java:22)
    at Seat.voteFileReadIn(Seat.java:117)
    at ConsoleMain.main(ConsoleMain.java:14)


    i also for got to add that im reading in from a file.
    my class vote construtor
    Java Code:
    public Vote(String [] vote,int numOfCandidates)
    	{
    		try
    		{
    			
    			setNumOfCandidates(numOfCandidates);
    			voteTaken = new int[getNumOfCandidates()];
    			for(int i = 0;i < getNumOfCandidates();++i)
    			{
    				voteTaken[i] = Integer.parseInt(vote[i]);
    			}
    			isFormal();
    			setVoteTaken(voteTaken);
    		
    		}
    		catch(MissingFormatArgumentException mfae)
    		{
    			setIsVoteFormal(false);
    		}
    	}
    my file read in method.
    Java Code:
    public void voteFileReadIn()
    	{
    		try
    		{
    			Scanner votFileInput = new Scanner(new FileInputStream(getSeatName()+".vot"));
    			
    			/*check if file has anything to input*/
    			if(!votFileInput.hasNext())
    			{
    				System.out.printf("FATAL ERROR Nothing to read in from %s.vot exiting System\n",getSeatName());
    				System.exit(1);
    			}
    			/*set variable voteCast*/
    			setVoteCast(Integer.parseInt(votFileInput.next()));
    			votFileInput.next();
    			//debug
    			System.out.printf("%d\n", getVoteCast());
    			Pattern p = Pattern.compile(":");
    			
    			int voteInFile = 0;
    			/*reading in file*/
    			while(votFileInput.hasNext())
    			{
    				String votLine = votFileInput.nextLine();
    				String [] parts = votLine.split("");
    				Matcher matcher = p.matcher(votLine);
    				if(matcher.matches())
    				{
    					votFileInput.skip(p);
    				}
    				getSvl().add(new Vote(parts,getNumOfCand()));
    				++voteInFile;
    			}
    			
    			if(voteInFile < getVoteCast())
    			{
    				System.out.printf("FATAL ERROR Insuffcient votes in %s.vot exiting System\n",getSeatName());
    				System.exit(1);
    			}
    			//test if vote file read in properly
    			for(Vote v : getSvl())
    			{
    				System.out.printf("%s", v.toString());
    			}
    		}
    		catch(FileNotFoundException fnfe)
    		{
    			System.out.printf("File not found in class Seat " +
    			"partyFileReadIn()\n");
    			System.out.println(System.getProperty("user.dir"));
    			System.exit(1);
    		}
    		/*catch(Exception e)
    		{
    			System.out.printf("Something wrong in class Seat " +
    			"votFileReadIn()\n");
    			System.exit(1);
    		}*/
    File looks likes
    Java Code:
    17 6
    4 2 1 3 6 5
    5 1 3 4 2 6
    
      :    :
    
    1 2 6 6 5 3
    NO DAMS
    6 4 5 2 1 3
    1
    X X X 1 X X
    1 2 3
    
       :  :
    
    5 3 4 2 1 6
    5 3 4 2 1 6
    1 2 6 6 5 3
    i think i understand the problem. Its that it cant convert "" to a number for obvious reason.how do i get around this problem:confused: its driving me insane in the membrane:eek:
    Last edited by ShinTec; 05-01-2010 at 03:02 PM.

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Your code should be catching that NumberFormatException, but instead it is trying to catch a MissingFormatArgumentException, which Integer.parseInt() does not throw.

    -Gary-

  8. #8
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Exclamation

    Alright okay now i got a new problem

    it says that i can do this
    Java Code:
    public static String [] anArray(String [] theArray,int size,String thingToAdd)
    	{
    		int lowest = 0;
    		int length = size;
    		String [] parts = thingToAdd.split(" ");
    		theArray = new String[length];
    		
    		
    		while(lowest < length && n < length)
    		{
    			theArray[lowest] = parts[0];
    			++lowest;		
    		}
    		
    		return theArray;
    	}
    not allowed to do this
    Java Code:
    public static String [] anArray(String [] theArray,int size,String thingToAdd)
    	{
    		int lowest = 0;
    		int length = size;
    		String [] parts = thingToAdd.split(" ");
    		theArray = new String[length];
    		int  n = 0;
    		
    		while(lowest < length && n < length)
    		{
    			theArray[lowest] = parts[n];
    			++lowest;
    			++n;
    		}
    		
    		return theArray;
    	}
    can someone tell me why

  9. #9
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    I don't understand "it says" and "not allowed" -- are you talking about compiler errors? What are they? Where does "n" come from in the first example?

    In both cases you are being passed a String array theArray, and you are throwing it away by assigning a new String array to the variable. And what is the point of the length variable? Why not just use size?

    But to answer your question directly, I don't know why declaring and incrementing another int variable should cause any problem with your code. If you are having problems there, my guess is that they are being caused by code you are not showing us.

    -Gary-

  10. #10
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Default

    sorry ignore that n < length in the first one that was a mistake. My question is why am i not allowed to loop through the parts array using int n and assign the value of parts[n] to theArray[lowest] it always throw an Array index out of bounds Error when i do this:confused:

    does it have to do with me splitting the string

  11. #11
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Default

    or how ive read in the file

  12. #12
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    First, it is difficult to know what you are trying to do, because
    1. You haven't told us
    2. There are no comments in your code, and
    3. "anArray()" as a method name doesn't help us


    My guess is that you are trying to split thingToAdd into words, and append them to the end of theArray. But that is not what you are doing. You are throwing away theArray and creating a new one. Then you are trying to add size words to your new array, but I don't think you know whether thingToAdd has size words in it, and if thingToAdd has fewer than size words, you will certainly get an ArrayIndexOutOfBoundsException.

    I don't know where size comes from or why you think you need it. (But again, I am guessing at what you are trying to accomplish.) If you do want to split a String into words and append them to an array, you need to do this:
    Java Code:
            split the String into words (do you know for sure that " " is the only possible separator?)
            create a new array with size existingarray.length + number of words
            put the elements of the existing array into the new array
            put the words from the string at the end of the new array
            return the new array
    If that is what you are trying to do, give it a try and show us what you come up with. If it is not what you are trying to do, please give us a hint.

    -Gary-

Similar Threads

  1. Logic Error: simulated accuracy
    By Kaito in forum New To Java
    Replies: 3
    Last Post: 10-27-2009, 01:39 PM
  2. Logic to generate a pattern
    By vijay_2008 in forum Advanced Java
    Replies: 3
    Last Post: 11-23-2008, 02:40 AM
  3. Logic Error: Not Writing To File
    By JDCAce in forum Advanced Java
    Replies: 6
    Last Post: 10-21-2008, 02:13 AM
  4. Cant get the logic right
    By jermaindefoe in forum New To Java
    Replies: 4
    Last Post: 03-11-2008, 12:22 AM
  5. iterate HashMap with logic
    By Heather in forum Web Frameworks
    Replies: 2
    Last Post: 07-03-2007, 09:47 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
  •