Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package test.population; import java.io.*; import java.text.NumberFormat; import java.util.Map; import java.util.SortedSet; import java.util.TreeSet; import java.util.HashMap; public class PopProcessRead { public static void main(String[] args) { try

package test.population;

import java.io.*; import java.text.NumberFormat; import java.util.Map; import java.util.SortedSet; import java.util.TreeSet; import java.util.HashMap;

public class PopProcessRead { public static void main(String[] args) { try { DataInputStream input = new DataInputStream(new BufferedInputStream(new FileInputStream (new File ("C://code//output.dat")))); PopProcessRead processor = new PopProcessRead(); Map thePopulationMap = processor.readFromFile(input); processor.displayToConsole(thePopulationMap); } catch (Exception error) { System.out.println("ERROR: Error in reading the binary file!"); } } private Map readFromFile(DataInputStream input) throws Exception { System.out.println(" =====================reading output.dat================ "); Map thePopulationMap = new HashMap(); while(input.available() != 0) { String state = input.readUTF(); long totPop = input.readLong(); long childPop = input.readLong(); long childPovertyPop = input.readLong(); PopData popD = new PopData(); popD.setTotPop(totPop); popD.setChildPop(childPop); popD.setChildPovertyPop(childPovertyPop); thePopulationMap.put(state, popD); } return thePopulationMap; // return thePopulationMap;

} private void displayToConsole(Map thePopulationMap) { System.out.println("State\tPopulation\tChild Population\tChild Poverty Population\t%Child Poverty"); SortedSet set = new TreeSet<>(thePopulationMap.keySet()); NumberFormat format = NumberFormat.getInstance(); format.setGroupingUsed(true); for (String state : set) { PopData popD = thePopulationMap.get(state); String line = String.format("%5s%15s%15s%25s%30.2f", state, format.format(popD.getTotPop()), format.format(popD.getChildPop()), format.format(popD.getChildPovertyPop()), ((double) popD.getChildPovertyPop() / (double) popD.getChildPop()) * 100); System.out.println(line); } }

}

Im getting an output of ERROR: Error in reading the binary file!

I am not sure why??

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

Recommended Textbook for

Conceptual Database Design An Entity Relationship Approach

Authors: Carol Batini, Stefano Ceri, Shamkant B. Navathe

1st Edition

ISBN: 0805302441, 978-0805302448

More Books

Students also viewed these Databases questions