How to show the maximum and the minimum in array...

How to show the maximum and the minimum in array...

I have a bookstore application, but I cannot show the maximum and the minimum price of the book. Please see my codes below.

import java.util.*;
public class Book{
public static void main(String args[]){
int numOfBook;
double totalCost = 0;
String expBookTitle = "";
double expBookPrice = 0;
String cheapBookTitle = "";
double cheapBookPrice = 0;
Scanner input = new Scanner(System.in);
System.out.println("Welcome to SAJID's Book Shop");
System.out.print("How many book would you like to store: ");
numOfBook = input.nextInt();
String[] book = new String[numOfBook];
double[] bookPrice = new double[numOfBook];
for(int i=0;i<book.length;i++)
{
System.out.print("Please enter book name: ");
book[i] = input.next();
System.out.print("Please enter book price: ");
bookPrice[i] = input.nextDouble();
System.out.println();
totalCost += bookPrice[i];
if(bookPrice[i] == expBookPrice)
{
expBookPrice = bookPrice[i];
expBookTitle = book[i];
System.out.println("Expensive book price: " + expBookPrice);
System.out.println("Expensive book title: " + expBookTitle);
if(bookPrice[i] <= expBookPrice)
{
cheapBookPrice = bookPrice[i];
cheapBookTitle = book[i];
System.out.println("Cheap book price: " + cheapBookPrice);
System.out.println("Cheap book title: " + cheapBookTitle);
}
}
}
}
}
View Answers

June 7, 2012 at 3:41 PM

The given code accepts the user to enter the number of books. According to that, user enter the name and book and price. These values will get stored into respective arrays. Then it will determine the expensive book and cheap book. Here is the code:

import java.util.*;
     public class Book{
        public static void main(String args[]){
            int numOfBook;
            double totalCost = 0;
            String expBookTitle = "";
            double expBookPrice = 0;
            String cheapBookTitle = "";
            double cheapBookPrice = 0;
            Scanner input = new Scanner(System.in);
            System.out.println("Welcome to SAJID's Book Shop");
            System.out.print("How many book would you like to store: ");
            numOfBook = input.nextInt();
            String[] book = new String[numOfBook];
            double[] bookPrice = new double[numOfBook];
            for(int i=0;i<book.length;i++)
            {
                System.out.print("Please enter book name: ");
                book[i] = input.next();
                System.out.print("Please enter book price: ");
                bookPrice[i] = input.nextDouble();
                System.out.println();
            }
            double max = bookPrice[0];
            double min = bookPrice[0];
            int a=0,b=0;
            for (int i=1; i<bookPrice.length; i++) {
                if (bookPrice[i] > max) {
                max = bookPrice[i];
                a=i;
                }
                if (bookPrice[i]< min) {
                min = bookPrice[i];
                b=i;
                }
            }
            System.out.println("Expensive book price: " + max);
            System.out.println("Expensive book title: " + book[a]);

            System.out.println();

            System.out.println("Cheap book price: " + min);
            System.out.println("Cheap book title: " + book[b]);
            }
            }









Related Tutorials/Questions & Answers:
How to show the maximum and the minimum in array...
How to show the maximum and the minimum in array...  I have a bookstore application, but I cannot show the maximum and the minimum price of the book... to SAJID's Book Shop"); System.out.print("How many book would you like to store
How to show the maximum and the minimum in array...
How to show the maximum and the minimum in array...  I have a bookstore application, but I cannot show the maximum and the minimum price of the book... to SAJID's Book Shop"); System.out.print("How many book would you like to store
Advertisements
To find first two maximum numbers in an array
To find first two maximum numbers in an array  Java program to find first two maximum numbers in an array,using single loop without sorting array
To find first two maximum numbers in an array,using single loop without sorting array.
To find first two maximum numbers in an array,using single loop without sorting array.  Java program to find first two maximum numbers in an array,using single loop without sorting array
To find first two maximum numbers in an array,using single loop without sorting array.
To find first two maximum numbers in an array,using single loop without sorting array.  Java program to find first two maximum numbers in an array,using single loop without sorting array
How to find maximum value in ArrayList
How to find maximum value in ArrayList  **Sir i am writing below code but its not working.i don't know how to implement logic my requirement.My requirement is find maximum value in userdefined objects and dispaly that object
How to find maximum value in ArrayList
How to find maximum value in ArrayList  **Sir i am writing below code but its not working.i don't know how to implement logic my requirement.My requirement is find maximum value in userdefined objects and dispaly that object
How to find maximum value in ArrayList
How to find maximum value in ArrayList  **Sir i am writing below code but its not working.i don't know how to implement logic my requirement.My requirement is find maximum value in userdefined objects and dispaly that object
How to find maximum value in ArrayList
How to find maximum value in ArrayList  **Sir i am writing below code but its not working.i don't know how to implement logic my requirement.My requirement is find maximum value in userdefined objects and dispaly that object
how to show popup in javascript
how to show popup in javascript  How to show popup in javascript
How to calculate minimum in Hibernate using the min() Function
How to calculate minimum in Hibernate using the min() Function The min... code calculates the minimum of numOfGoodsOrder from the GoodsDealer Entity... will teach you how to use the HQL min() function in your Hibernate based
How to declare String array in Java?
Following example will show you how to declare string array in java. There are many ways to declare an array and initialize them. We can use 'new' keyword to declare an array. However, an array can also be declared without
how to show data in database ?
how to show data in database ?  how to show the data in the database to the user in the textbox so that user can modify it and save it again
How to find maximum value for userdefined objects in ArrayList
How to find maximum value for userdefined objects in ArrayList  Hi sir,I have requirement i.e finding maximum value in userdefined objects... for employe class and write for below sample code ,in this code i can findout maximum
How to find maximum value for userdefined objects in ArrayList
How to find maximum value for userdefined objects in ArrayList  Hi sir,I have requirement i.e finding maximum value in userdefined objects... for employe class and write for below sample code ,in this code i can findout maximum
How to find maximum value for userdefined objects in ArrayList
How to find maximum value for userdefined objects in ArrayList  Hi sir,I have requirement i.e finding maximum value in userdefined objects... for employe class and write for below sample code ,in this code i can findout maximum
HOW TO SHOW TIME SERIES
HOW TO SHOW TIME SERIES  hi................ i require a small code in java swings to display the output as time series. Can u plz help???????? Reply....... thanks in advance
How do I use JGraph to create a MST(minimum spanning tree) using java?
How do I use JGraph to create a MST(minimum spanning tree) using java?  How do I use JGraph to create a MST(minimum spanning tree) using java? thanks for the help
JavaScript Array Contains Method
JavaScript Array Contains Method       In the given JavaScript array example, we are going to show how an array contains method(). As you already know we required the property
How to: generic array creation
How to: generic array creation  How to: generic array creation
how to show dataframe in pyspark
how to show dataframe in pyspark  Hi, I want to display 5 records from dataframe in PySpark program. How to display specific no of records of Data Frame in Python? Thanks (adsbygoogle = window.adsbygoogle || []).push
ModuleNotFoundError: No module named 'minimum'
'minimum' How to remove the ModuleNotFoundError: No module named 'minimum...ModuleNotFoundError: No module named 'minimum'  Hi, My Python... to install padas library. You can install minimum python with following command
How to Prepend Array in PHP
How to Prepend Array in PHP  hi, How can i prepand array in PHP. Please suggest any example of prepand array in PHP program. Thanks,   Hi, The PHP prepand array function in used before adding any elements
How you will Show unique records.
How you will Show unique records.  How you will Show unique records.   Hi, Here is the query which shows the unique records-ADS_TO_REPLACE_1 SELECT DISTINCT columnname FROM tablename; Thanks
How to Convert ArrayList to Array?
How to Convert ArrayList to Array?  Hi, I am trying to learn to Convert ArrayList to Array. How it is possible? How to Convert ArrayList to Array? Thanks   Hi, To Convert ArrayList to array ArrayList.toArray
Example to show Array exception in java
Example to show Array exception in java       Array Exception are the type of exception... is out of declared array range. The program given below describes how
how to create array in r
how to create array in r  Hi, In R Programming how array is created? how to create array in r? Explain me the process of array creation using the examples. Thanks   Hi, In R Programming language array is used
How to declare array in Java?
How to declare array in Java?  Hi, How to declare array in Java? Thanks   Hi, Following is a code of declaration of String array... of declarating integer array: int[] nums = new String[]{1,2, 5, 7}; Check
how to use button in array
how to use button in array  plz tell me how to code for button in array
Find max and min value from Arraylist
to find the maximum and minimum value element from the ArrayList. Java provides direct methods to get maximum and minimum value from any collection class i.e...); } } Output:ADS_TO_REPLACE_1 Minimum value is = 15 Maximum value
how to show random image in ASP.net?
how to show random image in ASP.net?  hello bros i saw in many websites..there is a programming of random image changing.... i want to use this in my ASP.NET websites, can any one suggest me code or any kind of help..so that i
JavaScript Array Count
JavaScript Array Count       In the given JavaScript example we are going to show you, how to count the length of array. For that we have to use the count() method in our code
how to show required fields indicate as * in struts2
how to show required fields indicate as * in struts2  how to show required fields indicate as * in struts2
How to show database values into graph using jsp?
How to show database values into graph using jsp?  How to show database values into graph using jsp
How to show database values into graph using jsp?
How to show database values into graph using jsp?  How to show database values into graph using jsp
how to create array in python
how to create array in python  Hi, how to create array in python? I want to create an array in Python and add some values to it. Thanks   Hi, You can create a new array with the code myArray = [] and after
how to make a program on array
how to make a program on array  When you make a program on array that the element will move downward and upward and when you input twice 0 then thats the time that it will not move. pls. give me a formula...tnx
How to Print Array in JavaScript
How to Print Array in JavaScript In this section you will learn about Array in JavaScript. Array are important part of all programming languages. Array... Now, here is the example how to declare and access array. <!DOCTYPE html>
How to create dynamic array in C?
How to create dynamic array in C?  How to create dynamic array in c programming language?   Dynamic Array in C Tutorial
C Array default values
C Array default values       In this section, you will learn how to set the default values in Array. The example below contains an array arr of integer type. The maximum size
How to move a value in array?
How to move a value in array?   Enter value at the End: 5 Display Array Elements Index at [0]0 Index at [1]0 Index at [2]0 Index at [3]5 Enter value at the End again 8 Display Array Elements Index at [0]0 Index at [1]0 Index
How to move a value in array?
How to move a value in array?   Enter value at the End: 5 Display Array Elements Index at [0]0 Index at [1]0 Index at [2]0 Index at [3]5 Enter value at the End again 8 Display Array Elements Index at [0]0 Index at [1]0 Index
What is the maximum size of a file that can be uploaded using PHP and how can we change this?
What is the maximum size of a file that can be uploaded using PHP and how can we change this?  What is the maximum size of a file that can be uploaded using PHP and how can we change
How to fill double array in java
How to fill double array in java  how to fill double array in java
How to using Binary Search Array Java ?
How to using Binary Search Array Java ?  Hi, I am beginners in Java... functions. The problem is that how to use binary search array in Java. Please give any online reference show that i will implement the binary search array in Java
How to create and use Array in Java?
How to create and use Array in Java?  Hi, How to create and use Array in Java? Write program to construct and use the array. Thanks   Hi, You can initialize the int array in following way: int num[] = new int[10
PHP Get Array Length
PHP Get Array Length  To count the array length we use count method... to COUNT_RECURSIVE (or 1), count() will recursively count the array. This option....   Code:ADS_TO_REPLACE_3 <?php $colorList = array("apple"
SQL Aggregate Queries
; SQL Aggregate Queries show you the list of Aggregate Function that include Sum, Count, Average, Maximum, Minimum  etc. Understand with ExampleADS... the maximum records value from table stu.  mysql>
How you will Show all data from a table.
How you will Show all data from a table.   How you will Show all data from a table.   Hi, following query is used to show all the data of the table-ADS_TO_REPLACE_1 SELECT * FROM tablename; Thanks
How to calculate average of array in Java?
How to calculate average of array in Java?  Hi, I have an integer array and I want to calculate the average of the values in it. How to calculate average of array in Java? Thanks   Hi, You can iterate through

Ads