Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help writing this code. I have been asked to fill in certain parts and have been giving the tester to make sure that

I need help writing this code. I have been asked to fill in certain parts and have been giving the tester to make sure that it works.

Here is the one that I need filled in:

import java.io.*; import java.util.Scanner; public class FileIO { /** * Write the read method below. * Read from fin, provided it is non-null. * If fin is null, return the String "no input file object present on call to read" * * Using tools provided by fin (which is a Scanner) * check to see if the current line of data contains an int, * if so, read it in and return an *Integer* object. * If the data is not an integer, check to see if it is a double, * if so, read it in and return a *Double* object from the method */ public static Comparable read(final Scanner fin) { return null; }// end read /** * write the writeLine method below * If fout is not null, print the item received, followed by a newline to fout */ public static void writeLine(final PrintStream fout, final Comparable item ) { }// end writeLine }// end FileIO class

Here is the tester:

import java.io.*; import java.util.Scanner; /** * Exercise in parsing an input file and writing to an output file * * DO NOT Change this file */ public class Tester { public static void main ( String[] args ) { String inputfile = "filledFile.txt"; File inf = null; Scanner fin = null; PrintStream fout = null; Object data; System.out.println("Trying null fin"); data = FileIO.read(null); System.out.println(data); System.out.println("Trying null fout"); FileIO.writeLine(null, "Bad fout"); try { inf = FileIOOpen.openInputFile(inputfile); fin = FileIOOpen.open(inf); fout = FileIOOpen.openOutputFile("outputFile.txt"); while (fin.hasNext()) { data = FileIO.read(fin); if(data != null) { FileIO.writeLine(fout, "Value is: " + data); FileIO.writeLine(System.out, "Value is: " + data); }// end if else { FileIO.writeLine(fout, "No more data in input file"); FileIO.writeLine(System.out, "No more data in input file"); }// end else }//end while FileIOClose.closeInputFile(fin); }// end try catch(Exception e) { System.out.println(e.getMessage()); }// end catch FileIOClose.closeOutputFile(fout); System.out.println(); }// end main }// end class

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

Students also viewed these Databases questions