1

Just started java. The main code has no problem in it. The hihestPage and lowestPages both show correct values but im getting null on the bookmaxpage and bookminpage. Trying to get the Title name on both max and min page.

    package Library;

import java.util.Scanner; import java.util.Arrays;

public class Library {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner scan = new Scanner(System.in);
    
    int i,j,number;
    int count=0;
    float highestPrice=0,total=0;
    String bookmaxpage=" ",bookminpage=" ";
    int highestPage=0;
    float averageCost=0;

    String Title[] = new String[20];
    String Author[] = new String[20];
    String Publisher[] = new String[20];
    float Price[] = new float[20];
    int Page[] = new int[20];
    int ISBN[] = new int[20];
    
    System.out.println("Enter the number of books that u want to enter : ");
    number = scan.nextInt();
    
    for(i=0;i<number;i++) {
        
        System.out.println("Enter Details of the book. ");
                    
        System.out.println("Enter the title: ");
        Title[i]=scan.next();
            if(Title[i].equalsIgnoreCase("nomore")) {
                break;
            }
        System.out.println("Enter the author: ");
        Author[i]=scan.next();
        System.out.println("Enter the publisher: ");
        Publisher[i]=scan.next();
        System.out.println("Enter the price: ");
        Price[i]=scan.nextFloat();  
        System.out.println("Enter the pages: ");
        Page[i]=scan.nextInt();
        System.out.println("Enter the ISBN: ");
        ISBN[i]=scan.nextInt();
        
        total=total+Price[i];
        count++;
        
        
    }
    
    for(i=0;i<Price.length;i++) {
        if(Price[i]>highestPrice)
            highestPrice = Price[i];
    }

    
    float lowestPrice= Price[0];
    for(i=0;i<Price.length;i++) {
        if(lowestPrice<Price[i])
            lowestPrice = Price[i];
            
    }
    
    for(i=0;i<Page.length;i++) {
        if(Page[i]>highestPage)
            highestPage = Page[i];
        bookmaxpage=Title[i];
            
    }
    
    int lowestPage= Page[0];
    for(i=0;i<Page.length;i++) {
        if(lowestPage<Page[i])
            lowestPage = Page[i];

        bookminpage=Title[i];
    }
        
    
    averageCost = total / number;
            
    
    
    
    System.out.println("Title \t\t Author \t\t Publisher \t\t Price \t Pages \t ISBN");
    System.out.println("======\t\t ====== \t\t ========= \t\t ===== \t ===== \t ====");
    for(i=0;i<number;i++) {
        
            System.out.println(Title[i] +" \t\t "+ Author[i] +" \t\t\t "+ Publisher[i] +" \t\t\t "+ Price[i] +" \t "+ Page[i] +" \t "+ ISBN[i]);
    }
    

    System.out.println("\n\n\n\nTotals ");
    System.out.println("------------------------------");
    System.out.println("Total number of books : " + count);
    System.out.println("Total cost of books : " + total);
    System.out.println("Maximum cost of a book : " + highestPrice);
    System.out.println("Minimum cost of a book : " + lowestPrice);
    System.out.println(bookmaxpage + " has the highest number of pages :" + highestPage);
    System.out.println(bookminpage + " has the lowest number of pages :" + lowestPage);
    System.out.println("Average Cost of books : " + averageCost);
}

}

Outcome : null has the maximum pages : 280. Looking for : Example has the maximum pages : 280

14
  • 1
    what are the contents of your title and page array. Also please correct any compilation errors in the posted code (bookminpage doesn't seem to be declared) Commented Mar 9, 2021 at 3:21
  • ty for correcting. String Title[] = new String[20]; int Page[] = new int[20]; this is the declaration. Commented Mar 9, 2021 at 3:24
  • 4
    Make sure you edit your post with additions, not just add them in the comments! Commented Mar 9, 2021 at 3:26
  • 2
    Also you must be populating these arrays somewhere? Is so include that as well! Commented Mar 9, 2021 at 3:26
  • 1
    Can you post your full code in your question? It's very difficult to help you with random snippets! Commented Mar 9, 2021 at 3:34

1 Answer 1

2

There are a few issues with this code, but the one you've specifically pointed out is coming from the loops where you assign bookminpage and bookmaxpage.

Because you are initialising your arrays to have 20 elements, the loops are running all the way until i = 19, so unless you input exactly 20 books, you are setting bookminpage = Title[19] = null.

I would recommend initialising your arrays after you've asked how many books there are going to be.


I also think you are missing some braces on your if statements. If you have something like this

if(condition)
    statement1;
statement2;

then only statement1 is in the if block.


As a side note I would research into some Java naming conventions, variable names should almost always be in camelCase.

Sign up to request clarification or add additional context in comments.

9 Comments

so should i change the ....[20] arrays to the number that the user has input in int number?
Exactly, not having the arrays the correct size would even result in a crash if you inputted more than 20 books, fixed size arrays should be used with caution.
Did not know that it would create an error like that. Thank u very much for ur time.
No problem, I've also added another clarification on something you might be misunderstanding?
Wouldn’t putting the ‘bookminpage = title [i]’ inside the if statement fix the null problem? If there is only 15 books, book 18 can’t be bigger, and therefore won’t assign null to bookminpage.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.