Search code examples
javaarraylistparseint

Java - One parseInt shows error, the other one doesn't


I've been doing my homework, but I ran into a problem. My code scans some information about books (author, title etc..) from a txt file then prints it. First I tried to print the author, title, ISBN code, the number of the pages, then the price of the books which is 15*number of pages. It works so far, however I wanted to add a number for each book, so it goes like a list. But when I modified the code to add those numbers, the code didn't want to scan them. it says

int cannot be converted to String

I tried to scan the numbers as Strings out of curiosity, but then the error message said that

String cannot be converted to int

My Book class:

    public class Book {
    private int number;
    private String author;
    private String title;
    private String code;
    private int pages;
    private static int multiplier = 15;

    public Book(String author, String title, String code, int pages, int number) {
        this.number = number;
        this.author = author;
        this.title = title;
        this.code = code;
        this.pages = pages;
    }

    @Override
    public String toString() {
        return author + " - " + title + ", ISBN:" + code + ", " + pages + " pages, Price: " + price() + "Ft";
    }

    public int price() {
        return multiplier * pages;
    }

    public static int getMultiplier() {
        return multiplier;
    }

    public static void setMultiplier(int multiplier) {
        Book.multiplier = multiplier;
    }

    public int getNumber() {
        return number;
    }


    public String getAuthor() {
        return author;
    }

    public String getTitle() {
        return title;
    }

    public String getCode() {
        return code;
    }

    public int getPages() {
        return pages;
    }

}

And my "Controller" class:

public class Controller {

void start() {
    scanning();
    printing("Avilable books: ");        


}

private List<Book> books = new ArrayList<>();


private void scanning() {

    try {
        Scanner fajlScanner = new Scanner(new File("books.txt"));
        String row;
        String data[];
        while (fajlScanner.hasNextLine()) {

            row = fajlScanner.nextLine();
            data = row.split(";");
            //1;J.K. Rowling;Harry Potter and the Philosopher's Stone;1782637594826;342
            //this line below gives the error for the data[0]
            books.add(new Book(Integer.parseInt(data[0]), data[1], data[2], data[3], Integer.parseInt(data[4])));

        }

    } catch (FileNotFoundException ex) {
        Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
    }

}

private void printing(String title) {
    System.out.println(title);
    for (Book book : books) {
        System.out.println(book);

    }
}
}

The content of my txt:

1;J.K. Rowling;Harry Potter and the Philosopher's Stone;1782637594826;342
2;J.R.R. Tolkien;The Fellowship of the Ring;1827493762573;431
3;Stephen King;Needful Things;8274653821647;411
4;Eric Knight;Lassie Come-Home;7263845618293;138
5;Molnár Ferenc;A pál utcai fiúk;9283746192846;194
6;Winston Groom;Forrest Gump;0385231342;228
7;Antoine de Saint-Exupéry;The Little Prince;8362748172649;69
8;Stephen King;Cujo;2918467382914;362

I can scan the pages good, but it has some problem with the "number".


Solution

  • your input is: 1;J.K. Rowling;Harry Potter and the Philosopher's Stone;1782637594826;342

    your constructor is:

    public Book(String author, String title, String code, int pages, int number){
    /*.
    .
    .*/
    }
    

    first character of input is "1" which is an integer but in your constructor the first parameter is a String. that's why the error showed up. "1" must be at the end of the input according to the constructor.

    for set a number for each book, use a additional static variable, initialize it to 1, every time you wanna add a book, set the current value of the static variable for the book's number, then ++ it.

    public class Book {
    
    private static int counter=1;
    
    private int number;
    private String author;
    private String title;
    private String code;
    private int pages;
    private static int multiplier = 15;
    
    public Book(String author, String title, String code, int pages, int number) {
        this.number = counter;
        this.author = author;
        this.title = title;
        this.code = code;
        this.pages = pages;
        counter++;
    }
    

    Good Luck