I\'m trying to solve Excercise Question 17.9 (on page 763 and 764) I have added
ID: 3665948 • Letter: I
Question
I'm trying to solve Excercise Question 17.9 (on page 763 and 764) I have added the code for my two files below, they are separate by ************** I know the second file isn't opening the number.txt file from the first set of code but I'm not sure how to add it. Or if there are any other issues with the code
// Exercise 17.9 : CreateResults.java
// Create poll results and output them to a file.
import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.IllegalFormatException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class CreateResults_Andres
{
private static int getValue()
{
int result = -1;
Scanner scanner = new Scanner( System.in );
// prompt the user for input
System.out.print(
"Enter integer result (1 - 5), -1 to quit: " );
try
{
result = scanner.nextInt();
// ensure input is in the correct range
while ( result != -1 && ( result < 1 || result > 5 ) )
{
System.out.print( "Invalid result, try again: " );
result = scanner.nextInt();
} // end while
} // end try
catch ( NoSuchElementException noSuchElementException )
{
System.err.println( "Error with input." );
System.exit( 1 );
} // end catch
return result;
} // end method getValue
public static void main( String[] args )
{
Formatter pollNumbers = null;
try
{
// create the output stream
pollNumbers = new Formatter( "numbers.txt" );
int pollValue = getValue(); // get a number from the user
// test for the sentinel value
while ( pollValue != -1 )
{
pollNumbers.format( "%d ", pollValue ); // write the value
pollValue = getValue(); // get another value
} // end while
pollNumbers.close(); // close the file
} // end try
catch( SecurityException securityException )
{
System.err.println( "Error opening file." );
} // end catch
catch( FileNotFoundException fileNotFoundException )
{
System.err.println( "Output file cannot be found." );
} // end catch
catch( IllegalFormatException illegalFormatException )
{
System.err.println( "Error with the output's format." );
} // end catch
catch( FormatterClosedException formatterClosedException )
{
System.err.println( "File has been closed." );
} // end catch
finally
{
if ( pollNumbers != null )
pollNumbers.close();
} // end finally
} // end main
} // end class CreateResults
*******************************************************************
import java.util.Scanner;
import java.io.*;
import java.io.File;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.IllegalFormatException;
import java.util.NoSuchElementException;
public class StudentPoll_TijerinaKlarissa {
public static void main( String args[] )
{
Formatter writer = null;
Scanner pollNumbers = null;
try
{
pollNumbers = new Scanner(
new File( "output.txt" ) );
writer = new Formatter( "output.txt" );
writer.format( "%-12s%-12s ", "Rating", "Frequency" );
int[] frequency = null;
// for each answer
// determine element to increment
while ( pollNumbers.hasNext() )
++frequency[ pollNumbers.nextInt() ];
// String output
for ( int rating = 1; rating < frequency.length; rating++ )
writer.format( "%-12d%-12d ", rating, frequency[ rating ] );
} // end try
catch ( Exception ex )
{
System.err.println( "Error: Exception" );
} // end catch
if ( writer != null )
writer.close();
if ( pollNumbers != null )
pollNumbers.close();
} // end main
}// end StudentPoll
Explanation / Answer
To read the text file
import java.io.*;
public class Test
{
public static void main(String [] args)
{
// The name of the file to open.
String fileName = "temp.txt";
// This will reference one line at a time
String line = null;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader =new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null)
{
System.out.println(line);
}
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex)
{
System.out.println("Unable to open file '" + fileName + "'");
}
catch(IOException ex)
{
System.out.println( "Error reading file '" + fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
To write the text file
import java.io.*;
public class Test
{
public static void main(String [] args)
{
// The name of the file to open.
String fileName = "temp.txt";
try
{
// Assume default encoding.
FileWriter fileWriter =new FileWriter(fileName);
// Always wrap FileWriter in BufferedWriter.
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
// Note that write() does not automatically
// append a newline character.
bufferedWriter.write("Hello there,");
bufferedWriter.write(" here is some text.");
bufferedWriter.newLine();
bufferedWriter.write("We are writing");
bufferedWriter.write(" the text to the file.");
// Always close files.
bufferedWriter.close();
}
catch(IOException ex)
{
System.out.println("Error writing to file '"+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}