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

Student Poll) Figure 7.8 contains an array of survey responses that is hard code

ID: 3547039 • 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

3 import java.io.FileNotFoundException;

4 import java.util.Formatter;

5 import java.util.FormatterClosedException;

6 import java.util.IllegalFormatException;

7 import java.util.NoSuchElementException;

8 import java.util.Scanner;

9

10 public class CreateResults

11 {

12 private int getValue()

13 {

14 int result = -1;

15 Scanner scanner = new Scanner( System.in );

16

17 // prompt the user for input

18 System.out.print(

19 "Enter integer result (1 - 10), -1 to quit: " );

20

21 try

22 {

23 result = scanner.nextInt();

24 } // end try

25 catch ( NoSuchElementException noSuchElementException )

26 {

27 System.err.println( "Error with input." );

28 System.exit( 1 );

29 } // end catch

30

31 return result;

32 } // end method getValue

33

34 private void outputData()

35 {

36 Formatter pollNumbers = null;

37

38 try

39 {

40 // create the output stream

41 pollNumbers = new Formatter( "numbers.txt" );

42

43 int pollValue = getValue(); // get a number from the user

44

45 // test for the sentinel value

46 while ( pollValue != -1 )

47 {

48 // if the number is valid

49 if ( pollValue > 0 && pollValue < 11 )

50

51 // write the value

52 pollNumbers.format( "%d ", pollValue );

53

54 pollValue = getValue(); // get another value

55 } // end while

56

57 pollNumbers.close(); // close the file

58 } // end try

59 catch( SecurityException securityException )

60 {

61 System.err.println( "Error opening file." );

62 } // end catch

63 catch( FileNotFoundException fileNotFoundException )

64 {

65 System.err.println( "Output file cannot be found." );

66 } // end catch

67 catch( IllegalFormatException illegalFormatException )

68 {

69 System.err.println( "Error with the output's format." );

70 } // end catch

71 catch( FormatterClosedException formatterClosedException )

72 {

73 System.err.println( "File has been closed." );

74 } // end catch

75 finally

76 {

77 if ( pollNumbers != null )

78 pollNumbers.close();

79 } // end finally

80 } // end method outputData

81

82 public static void main( String args[] )

83 {

84 CreateResults application = new CreateResults();

85 application.outputData();

86 } // end main

87 } // end class CreateResults