Cannot find symbol and I don't know why

I keep getting the error "cannot find symbol symbol : variable input location: class CountNumbers" in my program and I have put it all throughout the program.

import java.util.Scanner;

public class CountNumbers {

    public static void main(String[] args) {
       Scanner input = new Scanner(System.in);

       char[] chars = createArray();

       System.out.println("The numbers are:");
       displayArray(chars);
       int [] counts = countNumbers(chars);

       System.out.println();
       System.out.println("The occurences of each number are:");
       displayCounts(counts);
    }

    public static char[] createArray() {
      char[] chars = new char[100];

      for (int i = 0; i < chars.length; i++)
        chars[i] = input.nextInt();

        return chars;
      }

    public static void displayArray (char[] chars) {
      for (int i = 0; i < chars.length; i++) {
        if ((i + 1) % 20 == 0)
          System.out.println(chars[i]);
        else
          System.out.print(chars[i] + " ");
      }
    }


    public static int[] countNumbers(char[] chars) {
      int[] counts = new int[100];

      for (int i = 0; i < chars.length; i++)
        counts[chars[i] - 'a']++;

      return counts;
    }

    public static void displayCounts(int[] counts) {
      for (int i = 0; i < counts.length; i++) {
        if ((i + 1) % 10 == 0)
          System.out.println(counts[i] + " " + (char)(i + 'a'));
        else
          System.out.print(counts[i] + " " + (char)(i + 'a') + " ");
      }
    }
}

Thanks for the help.

Upvotes: 0

Views: 1428

Answers (1)

Jeff Storey
Jeff Storey

Reputation: 57202

input is a local variable to the main method which you then try to use in the createArray method. If you want input to be accessible in other methods, it needs to be a member or static variable.

Or in your case, since you only use input in createArray, you can move the creation of input to the createArray method.

Upvotes: 2

Related Questions