0

This is for a past homework assignment that I wasn't able to complete in time. I am a new programmer struggling with this method of the program CharacterSearch. I'm stuck on which boolean logic to use for my if statement, as well as how to find matches in the phrase using the pre-defined character variable. And example test is: character = "x" , phrase = "Xerox". Whereas X and x are different. The expected output should be count = 1. Edit: This problem should be answered without using arrays or lists.

 /**
 * Counts and returns the number of times the letter for this class 
 * occurs in the phrase. The class is case sensitive. 
 */
public int letterCount(String phrase)
{  
    Scanner jf = new Scanner(phrase);
    count = 0;
    for (int i = phrase.length(); i > 0; i--)
    { 
        jf.findInLine(character);         
        if(jf.hasNext())
        {
            count++;
            jf.next(); 


        }             
    }
    return count;
}
Jacob F
  • 1
  • 2
  • possible duplicate of [Simple way to count character occurrences in a string](http://stackoverflow.com/questions/6100712/simple-way-to-count-character-occurrences-in-a-string) – lodo Apr 01 '15 at 20:16
  • Scratch what you have now and simply convert the input string into a character array, through which you iterate, checking each character against the char represented by the CharacterSearch instance. – Mick Mnemonic Apr 01 '15 at 20:18
  • I haven't learned array's yet. This homework makes the assumption that you are able to solve without using an array. – Jacob F Apr 01 '15 at 20:30

4 Answers4

1

There you go:

/**
 * Counts and returns the number of times the letter for this class 
 * occurs in the phrase. The class is case sensitive. 
 */
public int letterCount(String phrase)
{
    int count = 0;
    // for every character in phrase ...
    for (int i = 0; i < phrase.length(); i++)
    { 
        // ... if it is the right one ...
        if(phrase.charAt(i) == character)
        {
            // ... increment the counter
            count++;
        }
    }
    return count;
}

You don't need any Scanner, and the code is fairly easy, readable and comprehensible.

lodo
  • 2,314
  • 19
  • 31
  • I got a compiler error here: if(phrase[i] == character). Array required, but java.lang.String found. – Jacob F Apr 01 '15 at 20:36
0

Pretty much a duplicate of Simple way to count character occurrences in a string

I can't leave a comment yet because my rep is too low but I wanted to give you a solution you could use.

Community
  • 1
  • 1
Troy
  • 146
  • 4
0
public int letterCount(String phrase)
{
    count = 0;
    for (int i = 0 ; i < phrase.length(); i++)
    {   
        String myLetter = phrase.substring(i, i + 1);
        if (myLetter.equals(character))
        {             
            count++;
        }
    }
    return count;
}

I figured out that I was iterating in the wrong direction, but more importantly I was not declaring a sub-string to check if my character matched the individual letters within the phrase.

Jacob F
  • 1
  • 2
  • 1
    And what is `character`? A `char` or a `String`? I assume it is a `String`, but why? A `char` would make more sense. – Tom Apr 01 '15 at 20:52
0

Use String's substring with a compare().

public int letterCount(String phrase, String match)
{  
    int count = 0;
    for(int i=0;i<phrase.length()-1;i++){  //-1 avoid out-of-bound exception
        String letter = phrase.substring(i, i+1);
        if(letter.compareTo(match) == 0){
            count++;
        }
    }   

    return count;
}
BK Batchelor
  • 457
  • 2
  • 6
  • You have a Java version which has a `#compare` method in the `String` class? – Tom Apr 01 '15 at 21:37
  • `#compareTo` doesn't expect two arguments. If you like to test if two strings are equal, then you should use `equals` instead. This is also easier to read. – Tom Apr 01 '15 at 21:41
  • 1
    compareTo just take one argument..it the end of the day brain is shutting down. – BK Batchelor Apr 01 '15 at 21:43