Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I input and output data to a .txt file with this java program? import jav

ID: 3555527 • Letter: H

Question

How do I input and output data to a .txt file with this java program?

import java.util.*;

public class collegeElections
{
   public static void getData(String[] str,int[] votes_received)
   {
       Scanner in = new Scanner(System.in);
      
       for(int i=0; i<5; i++)
       {
           System.out.println("Enter name of Candidate "+(i+1) + " : ");
           str[i] = in.next();

           System.out.println("Enter number of votes received by Candidate "+(i+1) + " : ");
           votes_received[i] = in.nextInt();
       }
   }
  
   public static int get_index_of_winner(int[] votes_received)
   {
       int index = 0;
        for(int i=1; i<5; i++)
        {
            if(votes_received[i] > votes_received[index]) index = i;
        }
        return index;
   }

   public static void main(String[] args)
   {
       String[] str = new String[5];
       int[] votes_received = new int[5];
       getData(str,votes_received);
       int winner_index = get_index_of_winner(votes_received);
       double sum = 0;
      
       for(int i=0; i<5; i++)0
       sum = sum + votes_received[i];

       System.out.println("Candidate Name Number of Votes Received Percentage of Votes.");
      
       for(int i=0; i<5; i++)
      
       System.out.println(str[i] + " " + votes_received[i] + " "+ ((double)100*votes_received[i]/sum));
       System.out.println("Winner is " + str[winner_index]);
   }
}

Explanation / Answer

Make output file with name out.txt.

while compiling use

javac collegeElections.java

java collegeElections > out.txt

Then enter input, when you finish, check out.txt

DONE

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote