Java problem with code, will upvote.
So I have two classes:
import java.io.*; import java.io.RandomAccessFile; import java.util.ArrayList; import java.util.Scanner; import java.io.File; import java.io.IOException; import java.io.FileNotFoundException;
public class AircraftInfo { public static ArrayList loadInfo(String sFileName) { ArrayList aircraftList = new ArrayList(); try { Scanner scanner = new Scanner(new File(sFileName)); while (scanner.hasNextLine()) { String[] line = scanner.nextLine().split(","); aircraftList.add(new Aircraft(line[0], Integer.parseInt(line[1]), line[2])); } scanner.close(); } catch (FileNotFoundException e) { System.out.println("File not found"); } return aircraftList; }
public static void writeAircraftObjects(ArrayList obList, String sFile) { try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(sFile)); for (Aircraft aircraft : obList) { oos.writeObject(aircraft); } oos.close(); } catch (IOException e) { System.out.println("Error writing to file"); } }
public static void writeRAFAircraft(ArrayList obList, String sFileName) { try { RandomAccessFile raf = new RandomAccessFile(sFileName, "rw"); for (Aircraft aircraft : obList) { byte[] name = aircraft.getName().getBytes(); byte[] year = String.valueOf(aircraft.getYear()).getBytes(); byte[] country = aircraft.getCountry().getBytes(); raf.write(name); raf.write(year); raf.write(country); } raf.close(); } catch (IOException e) { System.out.println("Error writing to file"); } }
public static T getRAFRec(String sFile, int nPosition) { try { RandomAccessFile raf = new RandomAccessFile(sFile, "r"); raf.seek(nPosition * 100); byte[] name = new byte[30]; raf.read(name); byte[] year = new byte[4]; raf.read(year); byte[] country = new byte[30]; raf.read(country); raf.close(); return (T) new Aircraft(new String(name), Integer.parseInt(new String(year)), new String(country)); } catch (IOException e) { System.out.println("Error reading from file"); return null; } }
public static void main(String[] args) { String sFile = "Aircraft.csv"; ArrayList aircraftList = AircraftInfo.loadInfo(sFile); System.out.println("Aircrafts loaded from file: " + sFile); for (Aircraft aircraft : aircraftList) { System.out.println(aircraft.toString()); } String objectFile = "AircraftObjects.bin"; AircraftInfo.writeAircraftObjects(aircraftList, objectFile); System.out.println("Aircrafts written to file: " + objectFile); String rafFile = "AircraftRAF.raf"; AircraftInfo.writeRAFAircraft(aircraftList, rafFile); System.out.println("Aircrafts written to file: " + rafFile); for (int i = 0; i }
however when I run AircraftInfo.java I receive this error:
mport java.io.serializable; ousages ublic class Aircraft implements Serializable \{ 2 usages public string name; 2 usages public int year; 2 usages public string country; 2 usages public Aircraft(String name, int year, String country) \{ this.name = name; this.year = year; this. country = country; \} 1 usage public string getName() \{ return name; \} 1 usage public int getyear() \{ return year; \} 1 usage public string getCountry() \{ return country; \} Exception in thread "main" java.lang.NumberFormatException Create breakpoint : For input string: "Year" at java.base/java. Lang. NumberFormatException. for InputString (NumberFormatException. Java: 65) at java.base/java. lang.Integer.parseInt (Inteqer. java:652) at java.base/java. lang. Integer. parseint (integer. java:770) at Aircraftinfo.main( Aircraftinfo. java: 74) Process finished with exit code 1