Question: Java Help!! Display all the names of a given length from names.dat. This file is in binary. Each entry is in the format: name length
Java Help!!
Display all the names of a given length from names.dat. This file is in binary. Each entry is in the format: name length (integer) and name (string).
There are no String reading methods for binary files. You'll have to figure out how to use the length value and readChar method to read all the names
I need this required output:
Enter a length 9ENTER Names of length 9 Ki Burgon Nara Huie Dori Hull 3 results
-----
Enter a length 10ENTER Names of length 10 Alwyn Turl Paco Verty 2 results
----
Enter a length 11ENTER Names of length 11 Lindi Ekins Adriana Bru Linet Greep Rich Yakovl Roy Garrard Alex Fillon Inge Adrain 7 results
This is my code so far, but it is not showing this part of the required output
can someone please fix my coding so that is displays this part of the required output !!!
I keep getting 0 results displayed:
Enter a length 9ENTER Names of length 9 0 results
---
Enter a length 10ENTER Names of length 10 0 results
I need the required output to display:
Ki Burgon Nara Huie Dori Hull 3 results
Alwyn Turl Paco Verty 2 results etc.....
here is my code
import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class Names { public static void main(String[] args) { Scanner kb = new Scanner(System.in); System.out.println("Enter a length"); int length = kb.nextInt(); System.out.println("Names of length " + length); ArrayList names = new ArrayList<>(); try { DataInputStream input = new DataInputStream(new FileInputStream("names.dat")); StringBuilder name = new StringBuilder(); boolean go = true; while (go) { int currentLength=0; try { StringBuffer bf = new StringBuffer(); currentLength = (int) input.readByte(); for(int i=0;i Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
