Reading Console Input using the BufferedReader class
The bold
portions of the 2 examples below demonstrate the code necessary to
request entry from the keyboard of both integer and double
values. The Scanner code
is commented out.
If you need a String
value use the input
variable.
String input =
console.readLine();
package pursepkg;
//import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class PurseTest {
public PurseTest() {
}
public static void main(String[] args)throws IOException {
//Scanner in = new Scanner(System.in);
Purse aPurse = new Purse();
aPurse.addNickels(3);
aPurse.addDimes(1);
aPurse.addQuarters(2);
double totalValue = aPurse.calculateTotal();
System.out.println("Total Value = $" + totalValue);
BufferedReader
console = new BufferedReader( new InputStreamReader(System.in));
Purse thePurse = new Purse();
System.out.print("How many pennies do
you have? ");
String
input = console.readLine();
int
count = Integer.parseInt(input); // convert string to int
//int count = in.nextInt();
thePurse.addPennies(count);
System.out.print("How many nickels do
you have? ");
input
= console.readLine();
count =
Integer.parseInt(input); // convert string to int
//count = in.nextInt();
thePurse.addNickels(count);
System.out.print("How many dimes do you
have? ");
input
= console.readLine();
count =
Integer.parseInt(input); // convert string to int
//count = in.nextInt();
thePurse.addDimes(count);
System.out.print("How many quarters do
you have? ");
input
= console.readLine();
count =
Integer.parseInt(input); // convert string to int
//count = in.nextInt();
thePurse.addQuarters(count);
System.out.print("How many dollars do
you have? ");
input
= console.readLine();
count =
Integer.parseInt(input); // convert string to int
//count = in.nextInt();
thePurse.addDollars(count);
System.out.println("Total Value = " +
thePurse.calculateTotal() );
System.out.println("Number of dollars =
" + thePurse.getDollars() );
System.out.println("Number of cents = "
+ thePurse.getCents() );
}
}
package cashierpkg;
//import java.util.Scanner;
import
java.io.BufferedReader;
import
java.io.InputStreamReader;
import
java.io.IOException;
public class CashierTest {
public CashierTest() {
}
public static void
main(String[] args) throws IOException
{
BufferedReader console = new
BufferedReader( new InputStreamReader(System.in));
//Scanner in =
new Scanner(System.in);
Cashier harry
= new Cashier();
// Enter the amount due
System.out.print("Enter the amount due. ");
//double due =
in.nextDouble();
String input = console.readLine();
double due = Double.parseDouble(input);
harry.setAmountDue(due);
//Enter the amount received
System.out.print("Enter the amount received. ");
//double received =
in.nextDouble();
input = console.readLine();
double received = Double.parseDouble(input);
harry.receive(received);
// Testing getAmountDue,
getAmountReceived and toString
System.out.println("Due = " + harry.getAmountDue() );
System.out.println("Received = " + harry.getAmountReceived() );
System.out.println(harry);
// Convert change to number
of dollars, quarters, dimes, nickels and pennies to give back
int dollars =
harry.returnDollars();
int quarters =
harry.returnQuarters();
int dimes =
harry.returnDimes();
int nickels =
harry.returnNickels();
int pennies =
harry.returnPennies();
// Print the change
System.out.println("Dollars = " + dollars);
System.out.println("Quarters = " + quarters);
System.out.println("Dimes = " + dimes);
System.out.println("Nickels = " + nickels);
System.out.println("Pennies = " + pennies);
}
}