0

This is my first semester doing computer programming and our professor totally bailed on us mid class. But I managed to nearly complete the classwork but for some reason my while statement is getting skipped.

import java.util.Scanner;
import java.text.DecimalFormat;

public class Election
{
    public static void main (String[] args)
        {

            DecimalFormat f = new DecimalFormat("##.00");

            float votesForPolly; 
            float votesForErnest; 
            float totalPolly = 0; 
            float totalErnest = 0; 
            String response; 
            int precinctsforpolly = 0; 
            int precinctsforernest = 0;
            int precinctsties = 0;

            Scanner scan = new Scanner(System.in);
            System.out.println ();      
            System.out.println ("Election Day Vote Counting Program");
            System.out.println ();


            do
            {   
                System.out.println("Do you wish to enter more votes? Enter y:n");
                response = scan.next();
                //this is where it skips from here*********
                while (response == "y")
                {
                    System.out.println("Enter votes for Polly:");
                    votesForPolly = scan.nextInt();
                    System.out.println("Enter votes for Ernest:");
                    votesForErnest = scan.nextInt();

                    totalPolly = totalPolly + votesForPolly;
                    totalErnest = totalErnest +  votesForErnest;

                    System.out.println("Do you wish to add precincts? Enter y:n");
                    response = scan.next();
                    if (response =="y")
                    {   
                        System.out.println("How many precincts voted for Polly: ");
                        precinctsforpolly = scan.nextInt();
                        System.out.println("How many precincts votes for Ernest: ");
                        precinctsforernest = scan.nextInt();
                        System.out.println("How many were ties: ");
                        precinctsties = scan.nextInt(); 
                    }
                }
            }
            while (response == "n");
            //to here*********************************************
            System.out.println("Final Tally");
            System.out.println("Polly received:\t " + totalPolly + " votes\t" + f.format((totalPolly/(totalPolly + totalErnest))*100) + "%\t" + precinctsforpolly + " precincts");
            System.out.println("Ernest received: " + totalErnest + " votes\t" + f.format((totalErnest/(totalPolly + totalErnest))*100) + "%\t" + precinctsforernest + " precincts");
            System.out.println("\t\t\t\t\t" + precinctsties + " precincts tied");

        }
}
devang
  • 5,376
  • 7
  • 34
  • 49
Stifpy
  • 19
  • 4

1 Answers1

3

Strings effectively point to values in Java, and aren't values themselves. Try using

while (response.equals("y"))

instead of

while (response == "y")

In the former case you're telling the runtime to actually compare the values. The latter case is telling the runtime to compare the pointers, which may not actually match.

James Cronen
  • 5,715
  • 2
  • 32
  • 52