0

Ok, so I am working on a homework assignment for java. Here is the assignment: Write a method that finds the number of occurrences of a specified character in a string using the following header: public static int count(String str, char a)

Write a test program that prompts the user to enter a string followed by a character and displays the number of occurrences of the character in the string.

The error in my code is this line: char a = input.nextChar; it says "nextChar cannot be resolved or is not a field"

Here is my code:

import java.util.*;

public class CountOccurances {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner (System.in);
        System.out.println("Enter a string: ");
        String str = input.nextLine();

        System.out.println("Enter a character: ");
        char a = input.nextChar;
        int letterCheck = count(str, a);

        System.out.println("The character " + a + "appeared" + letterCheck + "times in" + str);
    }

    public static int count(String str, char a)
    {
        int count = 0;
        for (int i = 0; i < str.length(); i++)
        {
            if (str.charAt(i) == a)
            {
                count++;
            }
        }
        return count;
    }
}
takendarkk
  • 2,949
  • 7
  • 22
  • 35
Tammy
  • 3
  • 1
  • 3

3 Answers3

4
char a = input.nextChar;

needs to be

char a = input.nextChar();

In the first case you are saying that input has a static variable called nextChar which is not what you want. You are trying to call a method on the input object which you do by adding the () at the end.

That being said even this won't work because the Scanner class does not have this method in the first place. What you will want to do is something like this

System.out.println("Enter a string: ");             //Prompt for string
String str = input.nextLine();                      //Get the string
System.out.println("Enter a character: ");          //Prompt for char
String userInput = input.nextLine();                //Get the char AS A STRING
int letterCheck = count(str, userInput.charAt(0));  //Call your method with the first 
                                                    //char entered

When a user enters a char to be counted, you will need to get that input with the nextLine() method which returns a String. You can then call your method by passing in the first char of that string as the second parameter.

takendarkk
  • 2,949
  • 7
  • 22
  • 35
2

You probably meant to say input.nextChar(), since you were trying to call a method. But that won't help, because Scanner doesn't have a nextChar() method.

What you probably need to do is:

String inputLine = input.nextLine();
char a = inputLine.charAt(0);

which inputs an entire line from the user, and then sets a to the first character from the input string. (It will also throw an exception if the user hits Enter without entering a character; charAt will throw the exception if there's no character to return. To be really safe, you'd want something like this:)

char a;
while (true) {
    String inputLine = input.nextLine();
    if (inputLine.length() > 0) {
        a = inputLine.charAt(0);
        break;
    } else {
        System.out.println("Your input string is empty");
    }
}
ajb
  • 29,914
  • 3
  • 49
  • 73
  • 1
    I don't think `String` has a method `getChar()` (from your first code snippet). Also, your second snippet shows you using `getChar()` on the Scanner object `input` which also does not have that method. – takendarkk Nov 26 '14 at 19:57
  • You had my back, I got yours. – takendarkk Nov 26 '14 at 21:16
2
char a = input.nextChar;

You are trying to call a method in the above line and its not the way to call a method. Method should be called like this:

char a = input.nextChar();

But the Scanner class does not have a nextChar() method. So you can use Scanner.nextLine() to read a line and get the first character of the line as shown below.

System.out.println("Enter a character:");
char a = input.nextLine().toCharArray()[0];

Here input.nextLine() reads a line ie a String.

toCharArray() converts the String read to an array of characters. For example:

Consider the read string is "HAI" it will be converted to {'H', 'A', 'I'}.

input.nextLine().toCharArray()[0] will get the first index of that array of characters. In the example it will be 'H'. But in your case array will have only one element as you are reading characters.

Johny
  • 2,003
  • 3
  • 17
  • 31