I'm trying to output the max value from a double array using a code sample and I cannot find why the values are being thrown as null, im sorry this is obvious but to my eyes the values have info, or I am misunderstanding something.
I have tried to insert;
int maximum = t[0]; // start with the first value
for (int i=1; i<t.length; i++) {
if (t[i] > maximum) {
maximum = t[i]; // new maximum
}
}
This is my code:
package test;
import java.util.Scanner;
public class projectone {
public static void main(String[] args) {
// Prepares keyboard as input device
Scanner keyboard = new Scanner (System.in);
// Declare variables for user information
String[] title = new String[20];
String[] author = new String[20];
String[] publisher = new String[20];
Double[] price = new Double[20];
int[] pages = new int[20];
String[] isbn = new String[20];
// Declare variables for system information
int index;
index = 0;
int totalbooks;
totalbooks = 0;
String temp;
double sum = 0;
double total= 0;
// Loop to prompt user to enter information
for (index = 0; index < 20; index++)
{
System.out.println("Please enter book details,");
System.out.println("when complete please enter 'nomore' as 'Title'");
System.out.println("Title :");
title[index] = keyboard.nextLine();
if (title[index].contentEquals("nomore"))
{
break; //this ends the loop if the user enters 'nomore' in the title field, should
// this work in every field?
}
System.out.print("Author :");
author[index] = keyboard.nextLine();
System.out.print("Publisher :");
publisher[index] = keyboard.nextLine();
// The price field is converted from a string to a double, then added to the total count of all
// previous books prices
System.out.println("Price :");
temp = keyboard.nextLine();
total = Double.valueOf(temp);
price[index] = Double.valueOf(temp);
sum = sum += total;
System.out.print("Page Count :");
temp = keyboard.nextLine();
pages[index] = Integer.valueOf(temp);
System.out.print("ISBN :");
isbn[index] = keyboard.nextLine();
totalbooks++; // adds +1 to the total number of books after a new entry
} // ends data entry loop
// Prints formatted column names and underlining
System.out.println(String.format("%-20s", "Title")+
String.format("%-20s", "Author")+
String.format("%-20s", "Publisher")+
String.format("%-20s", "Price")+
String.format("%-20s", "Pages")+
String.format("%-20s", "ISBN"));
System.out.println(String.format("%-20s", "====")+
String.format("%-20s", "=====")+
String.format("%-20s", "=========")+
String.format("%-20s", "=======")+
String.format("%-20s","=====")+
String.format("%-20s","====="));
// Loop to display entered information
for (index = 0; index <totalbooks; index++)
{
System.out.print(String.format("%-20s", title[index])+
String.format("%-20s", author[index])+
String.format("%-20s", publisher[index])+
"£" + String.format("%-20s", price[index])+
String.format("%-20s", pages[index])+
String.format("%-20s", isbn[index]));
System.out.println();// line break
}
// line breaks
System.out.println();
System.out.println();
System.out.println();
// data for totals and averages
System.out.println("Totals");
System.out.println("-------------------------------");
System.out.print("Total number of books: ");
System.out.println(totalbooks);
System.out.print("Total cost of books: £");
System.out.println(sum);
double max = price[];
for (int counter = 1; counter < price.length; counter++)
{
if (price[counter] > max)
{
max = price[counter];
}
}
System.out.println("Maximum cost of a book :" + max);
System.out.println("Minimum cost of a book :");
System.out.print("Average cost of a book :");
System.out.println(sum/totalbooks);
}
}
I am trying to output the max price, but it keeps throwing
java.lang.NullPointerException
on the line if (price[counter] > max)
I cant figure this out but its probably very obvious, can anyone help?
Thank you