Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



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

Have Programming Question? Ask it here!
 
 
 


Mushfiqur Rahman Sajid
How to show the maximum and the minimum in array...
1 Answer(s)      2 years and 3 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);
}
}
}
}
}

Advertisement
View Answers

June 7, 2012 at 3:40 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
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
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 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
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 popup in javascript
how to show popup in javascript  How to show popup in javascript
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 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 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 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- SELECT DISTINCT columnname FROM tablename; Thanks
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: generic array creation
How to: generic array creation  How to: generic array creation
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 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
how to move curosr from one text field to another automatically when the first textfield reaches its maximum length
how to move curosr from one text field to another automatically when the first textfield reaches its maximum length  how to move curosr from one text field to another automatically when the first textfield reaches its maximum
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 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 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
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>
how to use button in array
how to use button in array  plz tell me how to code for button in array
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- SELECT * FROM tablename; Thanks
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 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
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
Mysql Date Minimum
Mysql Date Minimum       Mysql Date Minimum return the minimum record value of a date field from a table. Understand with Example The Tutorial understands you to find the minimum
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
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 fill double array in java
How to fill double array in java  how to fill double array in java
How to show all errors in PHP?
How to show all errors in PHP? I this tutorial I will explain you how you can make changes in your code for displaying all the warning and error messages in your PHP program. This is one of the most asked question "How to show
Mysql Minimum Query
Mysql Minimum Query       Mysql Minimum Query is useful when you want to return the minimum value record from table. Understand with Example The Tutorial makes you
Mysql Minimum Query
Mysql Minimum Query       Mysql Minimum Query is useful when you want to return the minimum value... an example from 'Mysql Minimum Query'. To elaborate more on it, we simply create
how to show hyperlink website on the same page
how to show hyperlink website on the same page  dear sir: i need to know how can you show a webiste on the same page. for example i want to show www.roseindia.net by clicking on roseindia in hyperlink that will be displayed
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 in JavaScript is a variable that can hold more than one value. Array is a collection
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
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 from database in textbox in jsp
How to show data from database in textbox in jsp   How to show data from database in textbox in jsp   Here is an example that retrieve the particular record from the database and display it in textbox using JSP. <
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
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 can i hide and show tables
how can i hide and show tables  hai, i am creating a form which the code is included below i want to hide the tables initially and after i click go i want to display the tables with result.so any help is appreciated much <
Advertisements
 

 

 

DMCA.com