Home Answers Viewqa Java-Beginners How to show the maximum and the minimum in array...

 
 


Mushfiqur Rahman Sajid
How to show the maximum and the minimum in array...
1 Answer(s)      11 months ago
Posted in : Java Beginners

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 Pages:
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
Array - Maximum
maximum (minimum). There is one common variation on this -- sometimes... Java NotesArray - Maximum Finding the maximum is basically the same as finding the minimum; remember the first value, then look through all the other
maximum size of arraylist - java
maximum size of arraylist - java  1.what is the maximum size...? 4.how many access specifier and how many access modifier in java? 5.what... on size of RAM. The theoretical maximum number of elements in an ArrayList is 2
QUE 50 ...please show output also...
maximum and minimum of 3 integers using nested if-else. Read numbers as a command... and prints the maximum and minimum of these numbers using 'for' loops. Q5... matrix multiplication Q20. Write a program to find maximum and minimum
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
array - Java Beginners
array  how to determine both the diagonals of a 2-d array? How to find out the special matrix in case if maximum number of the row is minimum number... the size of 2D array :"); int i=input.nextInt(); int d[][]=new int[i][i
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: Minimum value is = 15 Maximum value is = 550
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
Show Color Components
Show Color Components       This section shows you color components. To show color components, we... int, short, or byte quantity.  The minimum normalized component value must
SQL Aggregate Queries
; SQL Aggregate Queries show you the list of Aggregate Function that include Sum, Count, Average, Maximum, Minimum  etc. Understand with Example... the maximum records value from table stu.  mysql>
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
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
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
Maximum number of catches for a try block
Maximum number of catches for a try block  How many catch statements are allowed for a try statement?   Hello Friend, You can use any number of catch statement for a try statement. Thanks
To insert maximum number of data in database.
To insert maximum number of data in database.  how to change string data type into clob data type in jsp   Hi Friend, Please visit the following link: http://www.roseindia.net/jdbc/insert-clob-example.shtml Thanks
Maximum length of Session ( $_SESSION ) in PHP.
Maximum length of Session ( $_SESSION ) in PHP.  sir, i want to know that how much limit of storing data into session in php. i mean can i store full product descriptions into session. Thank u
Array
Array  how can i use elements of an array in a circular form? that is if the searching of the element reach the last elements of the array, then it will start serching from the begining of the array
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
Array
Array  What if i will not declare the limit index of an array, how will I declare an array and store values with it using loop?   Hi Friend...(System.in); int array[]=new int[5]; System.out.println("Enter Array Elements
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 show popup in javascript
how to show popup in javascript  How to show popup in javascript
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
Array
Array  How do i insert elements into an array up to a limit from...("Enter Range: "); int size=input.nextInt(); int array[]=new int[size]; System.out.println("Enter Array Elements: "); for(int i=0;i<
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
ARRAY TUTORIAL
is automatically allocated. True or False. If false, explain why and show how...ARRAY TUTORIAL  PLEASE HELP ME ANSWER THESE ARRAY TUTORIAL Q1- It is easy to write outsides the bounds of a String or an array in Java: True
ARRAY TUTORIAL
is automatically allocated. True or False. If false, explain why and show how...ARRAY TUTORIAL  PLEASE HELP ME ANSWER THESE ARRAY TUTORIAL Q1- It is easy to write outsides the bounds of a String or an array in Java: True
SWT Spinner
the minimum value and the method setMaximum() sets the maximum value... SWT Spinner       This section illustrates you how to create a spinner in SWT. In SWT, the class
Maximum Solutions - Specialists in Object Orientation
that means, but I am overwhelmed that I had been noticed, considering how small... at the Java In Action conference, and will demonstrate how dynamic... whizz to Java performance fundi and has some great insight into how to make
array
array  Hi i have array like {1,2,3,4,,5,5,6,6} then how can i... is an example that store some integers into an array and find the occurrence of each number from the array. import java.util.*; public class SearchNumberOccurrence
array
array  how to getting values and storing values in array by using datainputstream?   Hi Friend, Try the following code: import java.io....]=Integer.parseInt(br.readLine()); } System.out.println("Array Elements
Array
reserved. Use a one-dimensional array of primitive type Boolean to represent the seating chart of the cinema theater. Initialize all the elements of the array... the corresponding elements of the array to true to indicate that the seat is no longer
PHP Get Array Length
PHP Get Array Length  To count the array length we use count method... is: int count ($var [,int $mode=COUNT_NORMAL]); Where  $var is an array...() will recursively count the array. This option is beneficial
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
array
array  how to delete an element from 1-d arrray
maximum limit of database connections have been reached.
maximum limit of database connections have been reached.  How do I check in my code whether a maximum limit of database connections have been reached
maximum limit of database connections have been reached.
maximum limit of database connections have been reached.  How do I check in my code whether a maximum limit of database connections have been reached
Array
Array  i having problem how to make algorithm for [a][5] [a][10] [b][15] [b][10] [c][5] [c][5] . . . . sum of [a] = 15 [b] = 25 [c] = 10
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 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 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
to show graph
to show graph  by clicking on submit button how to show the graph by fetching data in mysql?   Please visit the following link: JSP graph by fetching data in mysql
Apache POI Excel Maximum Row - JSP-Servlet
Apache POI Excel Maximum Row  I am using Apache POI lib for export jsp results to excel file. I am getting error : Row number must be between 0... row value ? and How can I change that ? Thanks in advance Regards
PHP Array
how it actually works.  PHP Example: Creating an array in PHP Fist of all lets see how to define an array.. Defining an array in php...The meaning of an array is almost same in all programming language

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.