Question

Short Java Program Question...


The Galaxers have stored the votes from the election in a file (called votes.txt), but need some help tabulating them. The lines in their file look like this: Happy to oblige, you whip up a Java program that uses the following methods: public static int countVotes (Scanner s) public static void printVotes (int v) to tally and print out the counts like this: 2891 4287 2272 3264

Please fill out the code:

public class ElectionResuls {

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class VoteTest {

   public static void main(String[] args) throws FileNotFoundException {
       File file = new File("D:\\votes.txt");

       Scanner scanner = new Scanner(file);

       int[] votes = countVotes(scanner);

       printVotes(votes);
   }

   private static void printVotes(int[] votes) {

       for (int vote : votes) {
           if (vote != 0) {
               System.out.println("the votes are " + vote);
           }
       }

   }

   private static int[] countVotes(Scanner scanner) {

       String line = null;
       int[] array = new int[10];
       int count = 0;

       while (scanner.hasNextLine()) {
           line = scanner.nextLine();

           String[] words = line.split("\\s+");// to divide the words based on
                                               // spaces

           for (String word : words) {
               if (word.trim().equals("g1*q!")) {
                   array[count] = 2891;
                   count++;

               }
               if (word.trim().equals("g2*z?")) {
                   array[count] = 4287;
                   count++;

               }
               if (word.trim().equals("g3*x@")) {
                   array[count] = 2272;
                   count++;

               }
               if (word.trim().equals("g4*w#")) {
                   array[count] = 3264;
                   count++;

               }
           }

output

the votes are 2891
the votes are 4287
the votes are 2272
the votes are 3264
the votes are 2891
the votes are 4287
the votes are 2272
the votes are 3264

votes.txt

g1*q! g2*z? g3*x@ g4*w# g1*q! g2*z? g3*x@ g4*w#

       }
       return array;
   }

}

Add a comment
Know the answer?
Add Answer to:
Short Java Program Question... Please fill out the code: public class ElectionResuls { The Galaxers have...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Help with a question in Java: What is the output from the following program? public class...

    Help with a question in Java: What is the output from the following program? public class Methods2 { public static void main(String[] args) { for (int i = 0; i < 3; i++) { for (int j = 0; j <= i; j++){ System.out.print(fun(i, j) + "\t"); } System.out.println(); } } static long fun(int n, int k) { long p = 1; while (k > 0){ p *= n; } return p; } }

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

  • fill in the blanks with java code The following code implements a Java timer that looks...

    fill in the blanks with java code The following code implements a Java timer that looks like the application below. . Once the application starts it keeps counting seconds until it is terminated. Timer (sec): 20 You are given a partial implementation of the program. Fill out the missing code to complete the program. import java.awt.*; import java.awt.event.; import java.util."; import javax.swing. *; import javax.swing. Timer; * This program shows a timer that is updated once per second. public class...

  • Need help with a number guessing game in java 1) You should store prior guessses in...

    Need help with a number guessing game in java 1) You should store prior guessses in a linked list, and the nodes in the list must follow the order of prior guesses. For example, if the prior guesses are 1000, 2111, 3222 in that order, the nodes must follow the same order 2) You should store the candidate numbers also in a linked list, and the nodes must follow the order of numbers (i.e. from the smallest to the largest)....

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

  • //Your programs should not crash under any circumstances. import java.util.Scanner; public class LetterCount { public static...

    //Your programs should not crash under any circumstances. import java.util.Scanner; public class LetterCount { public static int countBs(String word) { for (int i = 0; i < word.length(); i++) { int counter = 0; if ((word.charAt(i) == 'b') || (word.charAt(i) == 'B')) { counter++; } } return counter; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter a word: "); String str = in.next(); int result = countBs(str); System.out.printf("%s contains letter B %d times.\n", str,...

  • Given java code is below, please use it! import java.util.Scanner; public class LA2a {      ...

    Given java code is below, please use it! import java.util.Scanner; public class LA2a {       /**    * Number of digits in a valid value sequence    */    public static final int SEQ_DIGITS = 10;       /**    * Error for an invalid sequence    * (not correct number of characters    * or not made only of digits)    */    public static final String ERR_SEQ = "Invalid sequence";       /**    * Error for...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • ******** IN JAVA ********* I have a program that I need help debugging. This program should...

    ******** IN JAVA ********* I have a program that I need help debugging. This program should ask a user to input 3 dimensions of a rectangular block (length, width, and height), and then perform a volume and surface area calculation and display the results of both. It should only be done using local variables via methods and should have 4 methods: getInput, volBlock, saBlock, and display (should be void). I have most of it written here: import java.util.*; public class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT