I want to read from a text file which contains information about books. The information about the book should be put in an array. I understand I need to split.
The content in the text file looks like:
ISBN: 9781119846475
category: Computers
author: Andy Rathbone
title: Windows 11 For Dummies
language: English
pages: 464
price: 25
itemsInStock:24
ISBN: 9781118506851
category: Music
author: Alistair Wood
title: Ukulele Exercises For Dummies
language: English
pages: 272
price: 23
itemsInStock:5
I have tried something like this, but it doesn't work and I feel I'm thinking wrong.
BookClass bookArray [] = new BookClass[10];
try {
String bookInfo = "books.txt";
String line ="";
BufferedReader reader = new BufferedReader(new FileReader (bookInfo));
while ((line = reader.readLine()) != null) {
String[] bookText = line.split("//n ");
ISBN = bookText[0].split(": ")[1];
category = bookText[1].split(": ")[1];
author = bookText[2].split(": ")[1];
title = bookText[3].split(": ")[1];
language = bookText[4].split(": ")[1];
pages = bookText[5].split(": ")
price = Integer.parseInt(bookText[7].split(": ")[1]);
itemesInStock = Integer.parseInt(bookText[8].split(": ")[1]);
BookClass bookObj = new BookClass(ISBN, category, author, title, language, pages,
price, itemesInStock);
bookArray[counter] = bookObj;
counter += 1;
}
reader.close();
}
itemsInStockis inconsistent, using one character (COLON) as a delimiter rather than two (COLON & SPACE). Is that correct, or is that a typo?