-1

I keep getting a "cannot find symbol" error here:

    System.out.println ("Are there still more precincts to report? Please enter y or n");
    response = scan.next();
    while (response.equalsIgnoreCase(y))// Initializations
    {
    System.out.print("Enter votes for Polly: ");
   votesForPolly = scan.nextInt();
    System.out.print("Enter votes for Ernest: ");

Here's the full code if the problem lies elsewhere:

import java.util.Scanner;

public class Election
{
    public static void main (String[] args)
    {
    int votesForPolly;  // number of votes for Polly in each precinct
    int votesForErnest; // number of votes for Ernest in each precinct
    int totalPolly;     // running total of votes for Polly
    int totalErnest;    // running total of votes for Ernest
    String response;    // answer (y or n) to the "more precincts" question

    int pollyCarried = 0;
    int ernestCarried = 0;
    int tied = 0;

    Scanner scan = new Scanner(System.in);

    System.out.println ();
    System.out.println ("Election Day Vote Counting Program");
    System.out.println ();

    System.out.println ("Are there still more precincts to report? Please enter y or n");
    response = scan.next();
    while (response.equalsIgnoreCase(y))// Initializations
    {
    System.out.print("Enter votes for Polly: ");
   votesForPolly = scan.nextInt();
    System.out.print("Enter votes for Ernest: ");
    votesForErnest = scan.nextInt();

    totalPolly += votesForPolly;
    totalErnest += votesForErnest;

    if ( votesForPolly > votesForErnest )
    pollyCarried++;
   else if ( votesForErnest > votesForPolly )
      ernestCarried++;
   else
      tied++;

    System.out.println ("Are there still more precincts to report? Please enter y or n");
    response = scan.next();
    }// Loop to "process" the votes in each precinct
    int totalVotes = votesForPolly + votesForErnest;

    System.out.println("Polly got " + totalPolly + " votes carrying " + pollyCarried + " precincts.");// Print out the results
    System.out.println("Ernest got " + totalErnest + " votes carrying " + ernestCarried + " precincts.");
    System.out.println("Polly got " + ((totalPolly/totalVotes)*100));
    System.out.println("Ernest got " + ((totalErnest/totalVotes)*100));
    System.out.println("Polly carried " + pollyCarried + " precincts");
    System.out.println("Ernest carried " + ernestCarried + "precincts");
    System.out.println(tied + " precincts resulted in a tie");

    }
}

I've also tried making a char or string called "affirmative" and assigning it the letter y so that I can use it in my loop but it's no good.

user1740066
  • 141
  • 1
  • 1
  • 8

2 Answers2

2

You have not declared anything that pertains to y

At this line,

while (response.equalsIgnoreCase(y))

The compiler doesn't know what y is.

I'm guessing you meant this?

while (response.equalsIgnoreCase("y"))

Austin
  • 4,801
  • 6
  • 34
  • 54
1

In this line:

while (response.equalsIgnoreCase(y))

y is a variable that is not defined anywhere. Obviously, you want to use it as a string (or character). The line should be:

while (response.equalsIgnoreCase("y"))
Aziz
  • 20,065
  • 8
  • 63
  • 69