public static void book (){
// Arraylist taking in objects. ArrayList<bokInfo> bokInfoArrayList = new ArrayList<>(); File fil = new File("/Users/mac/Desktop/JAVA SERVERS/TRINN6/src/bok.txt"); //Surround with try-catch block, exception E not specific on what type of exception. try { Scanner Info = new Scanner(fil); System.out.println("Entering while loop!");
// While-loop with condition on if scanner contains next token.
while(Info.hasNextLine()) {
//Creating values for every next line. Parsing the int to release the scanner.
String ISBN = Info.nextLine(); String BookName = Info.nextLine(); String Author = Info.nextLine(); int Price = Integer.parseInt(Info.nextLine()); String Genre = Info.nextLine(); //Adding constructor to the arraylist. With the values created. bokInfoArrayList.add(new bokInfo(ISBN, BookName, Author, Price, Genre)); Info.nextLine(); } Info.close(); System.out.println(bokInfoArrayList); System.out.println("Finished reading while loop. --- Arraylist now holds objects"); } catch (Exception e) { e.printStackTrace(); ; } }
https://stackoverflow.com/questions/66809644/arraylist-containing-objects-is-writing-out-wrong-values-the-assignment-is-to-w March 26, 2021 at 09:04AM
没有评论:
发表评论