Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Niggling Issue in code

  1. #1
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Niggling Issue in code

    I get no errors though don't get right answer to problem.
    In the problem I'm searching through an array of strings in each string if a letter corresponds point to point with the letter in a certain separate string and is the same letter then that string will have a match of one etc. I need to return the index of the string in dictionary which has the highest matches. Should there be multiple strings with same no. of matches then need to choose lowest index. In my program it somehow chooses the highest index when there is a tie instead of lowest index of the two. "matched.get(i) > max)" This is my code where I specific that higher index needs to have a match to be maximum not equality. I get no error by the way.
    import java.util.HashMap;
    import java.util.Map;
     
    public class grafixCorrupt {
     
     
    	public static void main(String[] args){
     
    		String[] dictionary = {"cat", "cab", "lab"};
    		String candidate = "dab";
     
    		System.out.println(selectWord(dictionary, candidate));
     
    	}
     
     
    	public static int selectWord(String[] dictionary, String candidate){
    		int max = 0;
    		Map<Integer, Integer>matched = new HashMap<Integer, Integer>();
     
    		for(int i = 0;i<dictionary.length;i++){
    			matched.put(i, 0);
    		}
     
    		for(int  i = 0;i<dictionary.length;i++){
    			int index = 0;
    			for(int j = 0;j<candidate.length();j++){
    				if(dictionary[i].substring(j,j+1).equals(candidate.substring(j,j+1))){
    					index++;
    				}
    			}
    			matched.put(i, matched.get(i)+index);
    		}
     
     
    		for(int i = 0;i<matched.size();i++){
    		  if(matched.get(i) > max){
    			  max = i;
    		   }
    		     i++;	
    		}
    		if(max == 0){
    			return -1;
    		}
    		return max;
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Niggling Issue in code

    It's really tedious reading uncommented code with no clue what the author is thinking or doing. Why don't you comment your code?

    You're not comparing apples to apples. max does not contain the max number of occurrences, it contains the last index that contained the max value. When you compare the last index that contained the max value (max) to the number of occurrences of an item, matched.get(i), the number of occurrences in this example is always greater, causing the variable max to be set to the current index when it shouldn't be. And I'm not sure what the i++ statement after the if statement is for. That is done by the for() loop.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    keepStriving (November 3rd, 2013)

  4. #3
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Re: Niggling Issue in code

    Sorry for not commenting, definitely something I need to work on. Thanks anyway, I finally got it.

    --- Update ---

    My code for anyone interested.
    import java.util.HashMap;
    import java.util.Map;
     
    public class grafixCorrupt {
     
     
    /*public static void main(String[] args){
     
    		String[] kj = {"double", "bobble", "bubble", "rubble", "hubble", "supple"};
    		String lag  = "dimple";
    		System.out.println(selectWord(kj, lag));
    	}
    	*/
    	public static int selectWord(String[] dictionary, String candidate){
    		int max = 0;
    		boolean condition = false;
    		Map<Integer, Integer>matched = new HashMap<Integer, Integer>();
     
    		for(int i = 0;i<dictionary.length;i++){
    			matched.put(i, 0);
    		}
     
    		for(int  i = 0;i<dictionary.length;i++){
    			int index = 0;
    			for(int j = 0;j<candidate.length();j++){
    				if(dictionary[i].substring(j,j+1).equals(candidate.substring(j,j+1))){
    					index++;
    				}
    			}
    			matched.put(i, matched.get(i)+index);
    		}
     
     
    		for(int i = 0;i<matched.size();i++){
    		if(i==0){
    			if(matched.get(i)>= matched.get(max)&& matched.get(i)>0){
    				max = i;
    				condition = true;
    			}
     
    		}else if(matched.get(i) > matched.get(max)){
    			  max = i;
    			  condition = true;
    		   }
    		}
    		//System.out.println(matched);
    		//System.out.println(condition +":" + max);
    		if(max == 0 && condition==false){
    			return -1;
    		}else if(max==0 && condition==true){
    			return 0;
    		}
    		return max;
    	}
    }
    I think I overcomplicated this problem.

Similar Threads

  1. Help With Flowchart (Non-Code Issue)
    By Sephyncloud in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2013, 02:03 PM
  2. issue with jsp code
    By zulu1900 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 20th, 2013, 01:08 PM
  3. Issue in a code
    By ayyappad in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 3rd, 2013, 07:17 PM
  4. I am having an issue with my code
    By gmamagoodwin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 9th, 2012, 11:36 PM
  5. code issue, should be silple been working on it for four hours
    By Custermd in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 30th, 2011, 01:19 PM