Student Poll) Figure 7.8 contains an array of survey responses that is hard code
ID: 3547038 • Letter: S
Question
Student Poll) Figure 7.8 contains an array of survey responses that is 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 of Fig. 7.8 to read the survey responses from numbers.txt. The responses should be read from the file by using a Scanner. MethodnextInt should be used to input one integer at a time from the file. The program should continue to read responses until it reaches the end of file. The results should be output to the text file output.txt. PLEASE DON'T USE J DIALOGUE BOXES JUST USE REGULAR CODE
Example output for the two applications is shown below:
/* Output:
Enter integer result (1 - 10), -1 to quit: 3
Enter integer result (1 - 10), -1 to quit: 6
Enter integer result (1 - 10), -1 to quit: 2
Enter integer result (1 - 10), -1 to quit: 8
Enter integer result (1 - 10), -1 to quit: 4
Enter integer result (1 - 10), -1 to quit: 8
Enter integer result (1 - 10), -1 to quit: 4
Enter integer result (1 - 10), -1 to quit: 5
Enter integer result (1 - 10), -1 to quit: 7
Enter integer result (1 - 10), -1 to quit: 8
Enter integer result (1 - 10), -1 to quit: 9
Enter integer result (1 - 10), -1 to quit: -1
numbers.txt:
3 6 2 8 4 8 4 5 7 8 9 */
/* output.txt:
Rating Frequency
1 0
2 1
3 1
4 2
5 1
6 1
7 1
8 3
9 1
10 0 */
Explanation / Answer
You might have to play with the path depending on where your input file is located. Otherwise, this should work: Save the input program as readScores.java and the output as studentPollOutput.java
///////////////////////////////////////////
//INPUT PROGRAM
////////////////////////////////////////
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class readScores {
public static void main(String [] args){
Scanner keyboard = new Scanner(System.in);
File outputFile = new File("input.txt");
FileWriter fw = null;
try {
fw = new FileWriter(outputFile);
} catch (IOException e) {
e.printStackTrace();
}
int nextInt = 0;
System.out.println("Enter integer result (1 - 10), -1 to quit: ");
while((nextInt = keyboard.nextInt()) != -1){
System.out.println("Enter integer result (1 - 10), -1 to quit: ");
if(nextInt <= 10 && nextInt >= 1){
try {
fw.write(nextInt + " ");
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}else{
System.out.println("input not recorded, number is outside valid range.");
}
}
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
////////////////////////////////
//OUTPUT PROGRAM
////////////////////////////
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class studentPollOutput {
static File inputFile = new File("input.txt");
static File outputFile = new File("output.txt");
static Scanner input = null;
public static void main(String [] args) throws IOException{
input = new Scanner(inputFile);
FileWriter fw = new FileWriter(outputFile);
int[] values = new int[10];
for(int i = 0; i < 10; i++){
values[i] = 0;
}
while(true){
try{
values[(nextInt()-1)]++;
}catch(NoSuchElementException e){
break;
}
}
for(int i = 0; i < 10; i++){
fw.write((i+1) + " " + values[i] + " ");
fw.flush();
}
fw.close();
}
public static int nextInt(){
return input.nextInt();
}
}