Lab 19 - History of Computer Science, A File IO Lab
FIRST PART OF CODING LAB:
Step 0 - Getting Starting
In this program, we have two classes, FileIO.java and FileReader.java. FileIO.java will be our “driver” program or the main program that will bring the file reading and analysis together. FileReader.java will create the methods we use in FileIO.java to simply read in our file.
Main in FileIO
For this lab, main() is provided for you in FileIO.java and contains two method calls. One line calls readFile() in the FileIO class with the name of the input file "brief\_history\_of\_cs.txt". This method then calls other methods in both the FileIO and FileReader classes. The next line calls writeFileStats() with the name of the output file "output.txt". The method writes data to the output file in a certain format and uses values from the FileIO class array fileStats.
Here’s a sneak peak at what "brief\_history\_of\_cs.txt" contains…
A Brief History of Computer Science - World Science Festival 2700-2300 B.C. The Sumerian Abacus first appeared. 87 B.C. The Antikythera mechanism, the earliest known analog computer is developed in ancient Greece. 1703 A.D. German mathematician Gottfried Wilhelm Liebnitz introduces the binary number system. 1837 Charles Babbage invents the Analytical Engine. 1843 Ada Lovelace develops the first computer algorithm. 1936 Alan Turing invents the Turing Machine. 1941 Konrad Zuse invents Z3, the first programmable digital computer. 1947 The transistor is invented at Bell Labs. ...
Uncomment the provided lines after readFile() and writeFileStats() have been written.
FileReader.java
We will first need to read in our file before we perform any analysis, so let’s start with the methods in FileReader.java. The method signatures in the FileReader class are written for you. The purpose of the class is to process a file. We have one class variable that is a Scanner object called fileScanner which we will use to read the file.
Step 1 - beginScanning()
This method will initialize our class scanner fileScanner to read from the file we were given.
Try-catch
To do this, you will want to write a try-catch block with the catch just Exception e (you may want to google this to see how to use a try-catch if you are unfamiliar).
File and Scanner
Inside this try-catch block, you will want to create a file object with the fileName as your parameter. You will then want to use this file variable for your scanner. To do this, assign fileScanner to a new scanner with the file variable that you just create as the parameter of the scanner.
Below is an example of how you can create a file and a scanner to read that file…
// Creating File instance to reference text file File text = new File("C:/temp/test.txt"); // Creating Scanner instance to read File Scanner scnr = new Scanner(text);
Throwing out irrelevant data
After you have initialized your class variable fileScanner in your try catch, you will need to immediately “throw out” the first line of the file. The first line of the file is just a header that is the title of the file, A Brief History of Computer Science - World Science Festival. We throw it out because it doesn’t actually hold any data for our statistical analysis.
To “throw out” the first line of the file, you will just need to simply read the next line of the scanner. Don’t store it in any variable, just call .nextLine().
Step 1 - hasMoreLines()
This method will simply check to see if there is more to read from the file.
To do this, we will check to see if our class scanner is not equal to null and can still read the next line. If this is true, then we will return true, otherwise false we will just return false.
Hint: use the scanner method .hasNextLine()
Step 2 - getNextLine()
The method will simply return the next line of the file as a string.
To do this, you will use your class scanner variable fileScanner and use the appropriate scanner method to get the next line in the file.
FILEREADER.JAVA:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class FileReader {
private static Scanner fileScanner;
/**
* This method, called beginScanning, is public, static
and has one parameter, a String
* representing the name of the input file.
*
* Write a try-catch block in order to first create a
File object, and then
* create a Scanner for the File object. Use the
Scanner to read the first
* line of the file but do nothing with that line, as
the header line in
* the file should not contribute to the
statistics.
*
* @param fileName A String; from readFile() of the
FileIO class.
*/
public static void beginScanning(String fileName)
{
// Student TODO
// end Student TODO
}
/**
* This is a public, static method called hasMoreLines
which returns a boolean. This method
* should return true as long as the FileReader class
Scanner object is not
* null and the Scanner object has additional lines in
the input file to read.
*
* @return A boolean; to readFile() in class
FileIO.
*/
public static boolean hasMoreLines() {
// Student TODO
return false;
// end Student TODO
}
/**
* This is a public, static method called getNextLine
which returns a String. This String
* represents one line from the input file.
*
* Use the FileReader class Scanner object to read one
line and return the
* result.
*
* @return A String; to readFile() in FileIO.
*/
public static String getNextLine() {
// Student TODO
return "";
// end Student TODO
}
}
class FileReader {
private static Scanner fileScanner;
/**
* This method, called beginScanning, is public, static and has one
parameter, a
* String representing the name of the input file.
*
* Write a try-catch block in order to first create a File object,
and then
* create a Scanner for the File object. Use the Scanner to read the
first line
* of the file but do nothing with that line, as the header line in
the file
* should not contribute to the statistics.
*
* @param fileName A String; from readFile() of the FileIO
class.
*/
public static void beginScanning(String fileName) {
try {
fileScanner = new Scanner(new File(fileName));
fileScanner.nextLine();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* This is a public, static method called hasMoreLines which returns
a boolean.
* This method should return true as long as the FileReader class
Scanner object
* is not null and the Scanner object has additional lines in the
input file to
* read.
*
* @return A boolean; to readFile() in class FileIO.
*/
public static boolean hasMoreLines() {
return fileScanner != null &&
fileScanner.hasNextLine();
}
/**
* This is a public, static method called getNextLine which returns
a String.
* This String represents one line from the input file.
*
* Use the FileReader class Scanner object to read one line and
return the
* result.
*
* @return A String; to readFile() in FileIO.
*/
public static String getNextLine() {
if (fileScanner != null) {
return fileScanner.nextLine();
}
return "";
}
}
******************
Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.
Lab 19 - History of Computer Science, A File IO Lab FIRST PART OF CODING LAB:...
I need help with my computer science 2 lab. First I am asked to run the following program and check output? public static void main(String []args) { String filename= "output.txt"; File file = new File(fileName); try{ FileWriter fileWriter = new FileWriter(file, true); fileWriter.write("CS2: we finished the lecuter\n"); fileWriter.close(); }catch (Exception e){ system.out.println("your message"+e); } String inputfileName = "output.text"; File fileToRead = new File (inputfileName); try { Scanner = new Scanner (fileToread); while(Scanner.hasNextLine()){ String line = scanner.nextLine(); system.out.println(line); } }catch(Eception...
Finish FormatJavaProgram.java that prompts the user for a file name and assumes that the file contains a Java program. Your program should read the file (e.g., InputFile.java) and output its contents properly indented to ProgramName_Formatted.java (e.g., InputFile_Formatted.java). (Note: It will be up to the user to change the name appropriately for compilation later.) When you see a left-brace character ({) in the file, increase your indentation level by NUM_SPACES spaces. When you see a right-brace character (}), decrease your indentation...
Complete the code: package hw4; import java.io.File; import java.io.IOException; import java.util.LinkedList; import java.util.Scanner; /* * This class is used by: * 1. FindSpacing.java * 2. FindSpacingDriver.java * 3. WordGame.java * 4. WordGameDriver.java */ public class WordGameHelperClass { /* * Returns true if an only the string s * is equal to one of the strings in dict. * Assumes dict is in alphabetical order. */ public static boolean inDictionary(String [] dict, String s) { // TODO Implement using binary search...
I am trying to read from a file with with text as follows and I am not sure why my code is not working. DHH-2180 110.25 TOB-6851 -258.45 JNO-1438 375.95 CS 145 Computer Science II IPO-5410 834.15 PWV-5792 793.00 Here is a description of what the class must do: AccountFileIO 1. Create a class called AccountFileIO in the uwstout.cs145.labs.lab02 package. (This is a capital i then a capital o for input/output.) a. This class will read data from a file...
QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...
Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...
This is a java homework for my java class. Write a program to perform statistical analysis of scores for a class of students.The class may have up to 40 students.There are five quizzes during the term. Each student is identified by a four-digit student ID number. The program is to print the student scores and calculate and print the statistics for each quiz. The output is in the same order as the input; no sorting is needed. The input is...
Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this file, include your assignment documentation code block. After the documentation block, you will need several import statements. import java.util.Scanner; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; Next, declare the Lab12 class and define the main function. public class Lab12 { public static void main (String [] args) { Step 2: Declaring Variables For this section of the lab, you will need to declare...
create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...
Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...