Question
Java help needed please! I'm trying to figure out how to read to files and produce an output file. I can't seem to figure this
Java help needed please!
I'm trying to figure out how to read to files and produce an output file. I can't seem to figure this out along with formating it.
I've posted the instructions, my code, and a link to github where my work and files can be accessed - thank you in advance for your time and help!
Directions
Create a program that reads the files: places.csv and stuff.csv. The places file is the driver and the stuff file is the transaction file. There are numerous ways to solve the following problem.
The program must include JavaDoc, Junit and be uploaded to GitHub.
Create a report that prints out each country and the number of cities listed in the places file and the number of stuff listed in the stuff file.
-------------------------------------------------------------------------
This is how the file format should resemble
-------------------------------------------------------------------------
Country Cities Stuff
======= ====== =====
Afghanistan 1 2
Andorra 1 2
Angola 2 4
--------------------------------------------------------------------------------------------------------------------------------------------------
Github link --> https://github.com/tempUser00/temp <-------(All of my files, including data files here
--------------------------------------------------------------------------------------------------------------------------------------------------
My code
-------------------------------
Main
package unit1Package; import java.util.HashMap; import java.util.Map; public class Main { //private final static fileInput indata = new fileInput("stuff.csv"); private final static fileInput indata = new fileInput("places.csv"); private final static Map map = new HashMap(); public static void main(String[] args){ String line; String[] words; Object wordFound; while ((line = indata.fileReadLine()) != null){ words = line.split(","); for (String s:words) { wordFound = map.get(s); if (wordFound == null){ map.put(s, new Integer(1)); } else { map.put(s, (int)map.get(s) + 1); } } for (Map.Entry entry : map.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue()); } } public static void printOut( p) { System.out.println(p.getName() + "Test" + p.talk()); outFile.fileWrite(p.getName() + "|" + p.talk()); } } }
File Input
package unit1Package; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class fileInput { //BufferedReader to read from file private BufferedReader in = null; private String fileName; //constructor for fileInput class(Conventionally class name should start with a capital) public fileInput(String fileName) { this.fileName = fileName; try { //FileInputReader used to read from file in = new BufferedReader(new FileReader(fileName)); } catch (FileNotFoundException e){ System.out.println("Error, cannot open file: " + fileName + " " + e); } } public void fileRead() { //This method reads input from file and prints on to the console String line; try { while ((line = in.readLine()) != null){ System.out.println(line); } }catch (Exception e){ System.out.println("Error, cannot open file: " + fileName + " " + e); } } public String fileReadLine() { //This method reads input line by line and returns as a string try{ String line = in.readLine(); return line; } catch (Exception e) { System.out.println("Error, cannot open file: " + fileName + " " + e); return null; } } public void fileClose(){ //This method is used to safely close the opened file for reading if(in != null) { try{ in.close(); } catch (IOException e) { e.printStackTrace(); } } } }
File Output
package unit1Package; import java.io.*; //This class is used to write output to a file public class fileOutput { Writer out = null; private String fileName; public fileOutput(String filename) { this.fileName = filename; try { //FileOutputStream class is used to direct output to a file out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))); } catch (FileNotFoundException e){ System.out.println("Error, cannot open file: " + fileName + " " + e); } } public void fileWrite(String line) { //Writes output to a file line by line try{ out.write(line+" "); } catch(Exception e){ System.out.println("Error, cannot open file: " + fileName + " " + e); } } public void fileClose(){ //To safely close the file if (out != null){ try{ out.close(); } catch (IOException e){ e.printStackTrace(); } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started