473,490 Members | 2,486 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Java Programming Help

5 New Member
Hello to all. I am having difficulty trying to do this Java project using Eclipse. The following is what I have to do.

Election Day It's almost election day and the election officials need a program to help tally election results. There are two candidates for office -- Polly Tichen and Ernest Orator. The program's job is to take as input the number of votes each candidate received in each voting precinct and find the total number of votes for each. The program should print out the final tally for each candidate -- both the total number of votes each received and the percent of votes each received. Clearly a loop is needed. Each iteration of the loop is responsible for reading in the votes from a single precinct and updating the tallies. Your code should have the following:

1) Add the code to control the loop. You may use either a while loop or a do...while loop. The loop must be controlled by asking the user whether or not there are more precincts to report (that is, more precincts whose votes need to be added in). The user should answer with the character y or n though your program should also allow uppercase repsonses. The variable response (type char) has already been declared.

2)Add the code to read in the votes for each candidate and find the total votes. Note that variables have already been declared for you to use. Print out the totals and the percentages after the loop.

3) Test your program to make sure it is correctly tallying the votes and finding the percentages AND that the loop control is correct (it goes when it should and stops when it should).

4)The election officials want more information. They want to know how many precincts each candidate carried (won). Add code to compute and print this. You need three new variables: one to count the number of precincts won by Polly, one to count the number won by Ernest, and one to count the number of ties. Test your program after adding this code.

************************************************** **********

This is the skeleton of the code:

// ************************************************** *************
// Election.java
//
// This file contains a program that tallies the results of
// an election. It reads in the number of votes for each of
// two candidates in each of several precincts. It determines
// the total number of votes received by each candidate, the
// percent of votes received by each candidate, the number of
// precincts each candidate carries, and the
// maximum winning margin in a precinct.
// ************************************************** ************
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class Election
  4. {
  5.     public static void main (String[] args)
  6.     {
  7.     int votesForPolly;  // number of votes for Polly in each precinct
  8.     int votesForErnest; // number of votes for Ernest in each precinct
  9.     int totalPolly;     // running total of votes for Polly
  10.     int totalErnest;    // running total of votes for Ernest
  11.     String response;      // answer ('y' or 'n') to the "more precincts" question
  12.  
  13.     System.out.println ();
  14.     System.out.println ("Election Day Vote Counting Program");
  15.     System.out.println ();
  16.  
  17.     // Initializations
  18.  
  19.     // Loop to "process" the votes in each precinct
  20.  
  21.     // Print out the results
  22.     }
  23. }
  24.  
************************************************** **********

And this is the code I have:

************************************************** *************
// Election.java
//
// This file contains a program that tallies the results of
// an election. It reads in the number of votes for each of
// two candidates in each of several precincts. It determines
// the total number of votes received by each candidate, the
// percent of votes received by each candidate, the number of
// precincts each candidate carries, and the
// maximum winning margin in a precinct.
// ************************************************** ************

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class Election
  4. {
  5.     public static void main (String[] args)
  6.     {
  7.     int votesForPolly;  // number of votes for Polly in each precinct
  8.     int votesForErnest; // number of votes for Ernest in each precinct
  9.     int totalPolly;     // running total of votes for Polly
  10.     int totalErnest;    // running total of votes for Ernest
  11.     String response;    // answer (y or n) to the "more precincts" question
  12.  
  13.     Scanner scan = new Scanner(System.in);
  14.  
  15.     System.out.println ();
  16.     System.out.println ("Election Day Vote Counting Program");
  17.     System.out.println ();
  18.     // Initializations
  19.  
  20.     // Loop to "process" the votes in each precinct
  21.     {System.out.println ("Enter Votes? Enter Y or N");
  22.     response=scan.next().toUpperCase();
  23.     if (response.equals("Y"));
  24.     {response=Yes)    
  25.     System.out.println ("Enter votes for Polly:");
  26.     votesForPolly=scan.nextInt();
  27.     totalPolly=totalPolly+ votesForPolly;
  28.     System.out.println ("Enter votes for Ernest:");
  29.     votesForErnest=scan.nextInt();
  30.     totalErnest=totalErnest+ votesForErnest;}
  31.     System.out.println ("Enter another District? Enter Y or N");
  32.     response=scan.next().toUpperCase();    }
  33.     else 
  34.  
  35. {    response=0
  36.  
  37.     while (response =1)    
  38.         }
  39.     // Print out the results
  40.     System.out.println ("Total votes for Polly is: " + totalPolly);
  41.     System.out.println ("Total votes for Ernest is: " + totalErnest);    
  42.     }
  43. }
************************************************** **********

I need help in that I do not have the initialization step missing in my code, and the following errors show up in my code:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Yes cannot be resolved
Syntax error on token ")", ; expected
Syntax error on token "else", delete this token
Syntax error, insert ";" to complete BlockStatements
Type mismatch: cannot convert from int to String
Syntax error on token ")", EmptyStatement expected after this token


Please help me eliminate the errors I have in my code and the steps I need to tame to get my project to work.

Thank you.
Apr 13 '08 #1
1 1989
r035198x
13,262 MVP
Just look at the error messages one at a time. They usually give you some hints as well.
Apr 14 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2995
by: inquirydog | last post by:
Hello- I, the inquirydog, would like to solicit suggestions for a new web page I am making: I am creating a simple website that will translate concepts between windows os's, Linux, and the...
73
7891
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
6
3851
by: Alan Brown | last post by:
As a newbie I would like to ask what the difference in application (not details) is between C++ and Java. In what situations would you use C++ in preference to Java and vice versa? I am not...
55
45909
by: Elijah | last post by:
I have read many of the topics on learning C++ or Java first. It seems like everyone says something different. I would like to know if I should learn C++ or Java. First a little about myself. I...
11
9204
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
1
9582
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
2
6914
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
5
6256
by: r035198x | last post by:
Setting up. Getting started To get started with java, one must download and install a version of Sun's JDK (Java Development Kit). The newest release at the time of writting this article is...
0
6974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7183
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4573
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.