Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company.books;
- import java.awt.image.Raster;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class App {
- public static void main(String[] args) throws FileNotFoundException {
- String fileName = "./Books/GoodReads.txt";
- if (args.length > 0) {
- fileName = args[0];
- }
- Scanner scanner = new Scanner(new File(fileName));
- while (scanner.hasNextLine()){
- readNextBook(scanner);
- }
- }
- private static void readNextBook(Scanner scanner) {
- int bookNumber = scanner.nextInt();
- skipLine(scanner);
- String title = scanner.nextLine();
- skipLine(scanner);
- String author = scanner.nextLine().substring(3);
- String ratingString = scanner.nextLine();
- String[] ratingData = ratingString.split(" avg rating — ");
- String rating = ratingData[0];
- String numberOfRates = ratingData[1];
- skipLines(scanner, 7);
- StringBuilder builder = new StringBuilder()
- .append(String.format("Book #: %d\n", bookNumber))
- .append(String.format("Title: %s\n", title))
- .append(String.format("Author: %s\n", author))
- .append(String.format("Rating: %s\n", rating))
- .append(String.format("Raters: %s\n", numberOfRates))
- ;
- System.out.println(builder.toString());
- }
- static void skipLine(Scanner scanner){
- skipLines(scanner, 1);
- }
- static void skipLines(Scanner scanner, int n){
- for (int i = 0; i < n; i++) {
- scanner.nextLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement