Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(Student Poll) Figure 7.8 contains an array of survey responses thats hard coded into the program. Suppose we wish to process survey results that are

(Student Poll) Figure 7.8 contains an array of survey responses thats hard coded into the program. Suppose we wish to process survey results that are stored in a file. This exercise requires two separate programs. First, create an application that prompts the user for survey responses and outputs each response to a file. Use a Formatter to create a file called numbers.txt. Each integer should be written using method format. Then modify the program in Fig. 7.8 to read the survey responses from numbers.txt. The responses should be read from the file by using a Scanner. Use method nextInt to input one integer at a time from the file. The program should continue to read responses until it reaches the end of the file. The results

should be output to the text file "output.txt"

image text in transcribed

I have these codes from the internet (createresults.java and output_survey.java)and I try to compile it with the figure 7.8 but is not working(I also add the code from fig 7.8):

package student;

import java.io.FileNotFoundException;

import java.lang.SecurityException;

import java.util.Formatter;

import java.util.FormatterClosedException;

import java.util.NoSuchElementException;

import java.util.Scanner;

//Create a class CreateResults

public class CreateResults {

//Create an object output for class formatter

private static Formatter output;

//Start main() method

public static void main(String args[])

{

//Call the required functions

openFile() ;

addRecords() ;

closeFile() ;

}

//Declare and define the function openFile()

public static void openFile()

{

//open the file numbers.txt

try

{

output= new Formatter("numbers.txt");

}

/*if unable to write on the file, show error message */

catch(SecurityException securityException)

{

System.err.println("write permission"

+" denied");

System.exit(1) ;

}

//if unable to open the file, show error message

catch(FileNotFoundException fileNotFoundException)

{

System.out.println("Error opening file") ; System.exit(1);

}

}

//Declare and define the function addRecords()

public static void addRecords()

{

/*Create an object of scanner class to take

* user input */

Scanner input=new Scanner(System .in);

//Prompt the user to enter the rating

System. out.printf("Enter the rating ");

int a=input.nextInt();

/* Write the user input to file, if it is in

* range 0 to 9 */

while(a>0&&a

{

try

{

output.format("%d", a);

output.format("");

} catch(FormatterClosedException formatterClosedException)

{

System.err.println("Can't open" +" the file");

break;

} catch(NoSuchElementException elementException)

{

System.err.println("lnvalid input." +" try again");

}

//System.out.println(a);

a=input.nextInt();

}

//close the object input

input.close() ;

}

//close the files

public static void closeFile() {

if(output!=null)

output.close() ;

}

}

//import files

package student;

import java.io. FileNotFoundException ;

import java.io.IOException;

import java.lang.SecurityException;

import java.nio.file.Paths;

import java.util.Formatter;

import java.util.FormatterClosedException;

import java.util.Scanner;

public class Output_survey {

/*Create an object of scanner class to

take user input */

private static Scanner input;

/*Create an object output for class formatter *to write in the file */

private static Formatter output;

//Start main() method.

public static void main(String[] args)

{

//Call the functions

openFile() ;

readRecords() ;

closeFile() ;

}

//Declare and define the function openFile()

public static void openFile()

{

try

{

input=

new Scanner(Paths.get("numbers.txt"));

}

catch(IOException ioException)

{

System.err.println("Can1 open the file");

System .exit(1) ;

}

try

{

output= new Formatter("output.txt");

}

catch (SecurityException securityException)

{

System.err.println("write permission" +" denied");

System.exit(1);

}

catch(FileNotFoundException tileNotFoundException)

{

System.out.println("Error opening file");

System.exit(1);

}

}

//Declare and define the function readRecords()

public static void readRecords() {

//while there is more to to read from file

while(input.hasNext()) {

//write the data to file.

try

{

output.format("%d", input.nextInt());

output.format("");

}

//if unable to write on file, display error

catch(FormatterClosedException formatterClosedException)

{

System.err.println("Can't write:"

+" Error");

break;

}

}

//close the object input

input.close();

}

//close the files.

public static void closeFile()

{

if(output!=null)

output.close() ;

if(input!=null)

input.close() ;

}

}

Figure 7.8 code to copy:

package student;

public class StudentPoll {

// Fig. 7.8: StudentPoll.java

// Poll analysis program.

public static void main(String[] args) {

// student response array (more typically, input at runtime)

int[] responses = { 1, 2, 5, 4, 3, 5, 2, 1, 3, 3, 1, 4, 3, 3, 3, 10, 2, 3, 3, 2, 14 };

int[] frequency = new int[6]; // array of frequency counters

// for each answer, select responses element and use that value

// as frequency index to determine element to increment

for (int answer = 0; answer

try {

++frequency[responses[answer]];

}catch (ArrayIndexOutOfBoundsException e){

System.out.println(e); // invokes toString method

System.out.printf("responses[%d] = %d%n%n", answer, responses[answer]);

}

}

System.out.printf("%s%10s%n", "Rating", "Frequency"); // output each array element's value 32

for (int rating = 1; rating

System.out.printf("%6d%10d%n", rating, frequency[rating]);

}

}

Fig.7.8:S tuden tPo 11.3ava 2/ Pol1 analysis progran 4 public class StudentPo11 6 public static void mainStringl] args) // student response array (more typically, input at runtine) int[) responses1, 2. 5, 43 5, 2. 1, 3.3.1. 4. 333 10 2, 3.3. 2, 14 h intt) frequency new int[6):/ array of frequency counters 12 13 14 15 I6 // for each answer, select responses element and use that value // as frequency iedex to determine element to increment for (int answer . 0; answerresponses. 1ength; answer..) IT try *frequency[respons es [ answer]]: catch (ArrayIndexDut Of BoundsException) System. out-printle(e): / invokes toString method System, out .printf(" responses[M].%dinin", answer, respanses answer]) System.out.printfCsn"Rating. "Frequency 30 // out put each array elenent's value for (int rating 1: rating

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Managing Your Information How To Design And Create A Textual Database On Your Microcomputer

Authors: Tenopir, Carol, Lundeen, Gerald

1st Edition

1555700233, 9781555700232

More Books

Students also viewed these Databases questions

Question

Does it avoid using personal pronouns (such as I and me)?

Answered: 1 week ago

Question

Does it clearly identify what you have done and accomplished?

Answered: 1 week ago