Java tip calculator - java

Basically I just need help with a tip calculator program. It should ask how many customers there are, the tip percentage to be used, the bill amount of each customer, the tip amount, and the tip amount distributed evenly to each customer. I'm also trying to get the program to read in as many customers as the user wants to input for bonus to the homework. Here is the code, instead of the program returning "Please enter their bill amount" i keep getting "Is there another customer? y or n?". That is the main problem i'm having. Excuse the bad structuring, i'm basically a complete beginner.
import java.util.Scanner;
public class H3_TipCalc {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println("Please enter your bill amount.");
double bill = input.nextDouble();
{
String multiplecust = ("Y");
//int mc = 1;
while (multiplecust.equalsIgnoreCase("y"))
{
Scanner usrin = new Scanner (System.in);
System.out.println("Is there another customer? y or n?");
multiplecust = usrin.nextLine();
//mc++;
if (usrin.equals("y"))
{
System.out.println("Please enter their bill amount.");
}
}
}
}
}

if (usrin.equals("y"))
{
System.out.println("Please enter their bill amount.");
}
You're comparing the Scanner to "y", which is probably not what you want to compare. Compare multiplecust to "y".

Related

Formatting, Big Decimal, interest to percent please

Can someone please explain the usage not just answer I really would like to learn how to do this. This is my 2nd month using Java and please explain formatting usage in Java. Thank you so much. I really appreciate it. Feel free to ask any other question in regards C++ or python. I am in need of help in formatting, big decimal usage and how to set
import java.util.*;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.math.*;
public class Main{
public static void main(String[] args)
{
Scanner myCalculation = new Scanner(System.in);
System.out.print("Welcome to the Interest Calculator \n\n");
String option = "y";
while (option.equalsIgnoreCase("y"))
{
System.out.print("Enter Loan Amount: ");
//double loanAmount = 520000;
double loanAmount = myCalculation.nextDouble();
//Get Interest rate from user
System.out.print("Enter Interest Rate: ");
// double interRate = .05375;
double interRate = myCalculation.nextDouble();
double interest = loanAmount * interRate;
System.out.printf("%.3f", interest);
System.out.println();
//prompt user to continue? if he enter y then it will Continue
//else it will stop
//System.out.println("Continue? (y:n): ");
Scanner scan = new Scanner(System.in);
boolean stop = false;
while(!stop) {
//do whatever
System.out.println("Would you like to continue? (yes or no)");
String s = scan.nextLine();
if(s.equals("no")) {
stop = true;
}
}
}
}
}
Scanner in = new Scanner(System.in);
System.out.print("enter double");
double d = in.nextDouble(); //doubles
System.out.print("enter int");
int i = in.nextInt(); //ints
System.out.print("enter text");
String text = in.next(); //gets a string up to the first space
in.nextLine();
System.out.print("enter text w/ spaces");
String line = in.nextLine(); //gets a string up to the first space
System.out.println(d);
System.out.println(i);
System.out.println(text);
System.out.println(line);
in.close();
System.out.println(new DecimalFormat("#.###").format(4.567346344634));

Java - Do you want to continue?(Y/N)

I want to write a simple loop for the program to go back and restart it again.
Just a simple 1 question program. Then the system ask if the user want to do it again. If the user inputs Y ... the program will loop it back to the beginning and run the entire program again. If the user inputs N, it exits.
import java.util.Scanner; // show them as code
public class HowToDoLoop {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("How much money do you want to have? ");
double money = input.nextDouble();
System.out.println("Ok, here is yours $" + money);
System.out.println("Do you want to continue y or n");
while(true){
System.out.println("How much money do you want to have? ");
double money = input.nextDouble();
System.out.println("Ok, here is yours $" + money);
System.out.println("Do you want to continue y or n");
String c = input.nextLine();
if(c.equalsIgnoreCase("n")){
break;
}//else continue to loop on any string ;-)
}
String c = "";
do{
System.out.println("How much money do you want to have? ");
double money = input.nextDouble();
System.out.println("Ok, here is yours $" + money);
System.out.println("Do you want to continue y or n");
c = input.nextLine();
}while(c.equalsIgnoreCase("Y"));

While-loop equals to not working properly (java bank account)

So my task is to write a bank-account program that asks wether you want to deposit or withdraw money, it asks for how much and at last if I want to continue or not, and if not it will show my current account balance. Example:
Deposit or withdraw (0-deposit, 1-withdraw):
How much?
Do you want to continue?
Balance: XX.XX
The program should be looped and break when the answer is "Y" on "Do you want to quit?".
package Account;
import java.util.Scanner;
public class bankVisit {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
bankAccount account = new bankAccount();
String respond = "N";
System.out.print("Deposit or withdraw (0-deposit, 1-withdraw): ");
int choice = scan.nextInt();
while (respond.equals("N")) {
if (choice == 0) {
System.out.print("Amount: ");
double total = scan.nextDouble();
account.credit(total);
System.out.print("Do you want to quit? ");
respond = scan.next();
} else if (choice == 1) {
System.out.print("Amount: ");
double total = scan.nextDouble();
account.withdraw(total);
System.out.print("Do you want to quit? ");
respond = scan.next();
}
System.out.print("Deposit or withdraw (0-deposit, 1-withdraw): ");
choice = scan.nextInt();
}
System.out.println("Balance: " + account.getBalance());
scan.close();
}
}
I get it to loop, but I dont get how I should make the loop to quit, and post my balance, it keeps looping even though "respond" not equals "N".. Any ideas? Im very new to Java so anything will do.

Why is my Java for/if/else statement outputting duplicates

I am practicing using loops in Java and I have created this if/else loop to continually ask if there is another customer, if the user input is
"Yes" then it asks for that customers bill.
"No" then I am going to total the bill (I haven't written code for that yet so ignore that part).
The problem I am having is after I enter the initial bill amount the code asks if there is another customer like it should, but then prematurely asks for their bill amount and then asks if there is another customer a second time?
My main question is, did I structure this wrong? How am I able to get the loop to not output something twice and/or prematurely?
Scanner input = new Scanner(System.in);
double total;
System.out.println("Please enter your bill amount.");
double bill0 = input.nextDouble();
for(int c = 0; c > -1; c++) {
System.out.println("Is there another customer?");
String customer = input.nextLine();
if (customer.equalsIgnoreCase("Yes")) {
System.out.println("Please enter their bill.");
double bill = input.nextDouble();
} else {
System.out.println("Okay, let's total your bill amount.");
}
}
for(int c = 0; c > -1; c++)
This loop will (basically) run forever as c will always be greater than -1.
(Actually this loop will run until overflow occurs because the value of c will be too big to fit in the available storage space allocated for this integer. You can refer here for more information: http://en.wikipedia.org/wiki/Integer_overflow)
A cleaner way to structure this would be to do something like:
String answer = "Yes";
while (answer.equals("Yes"))
{
System.out.println("Please enter your bill amount.");
double bill0 = input.nextDouble();
System.out.println("Is there another customer? (Yes or No)");
answer = input.nextLine();
}
Of course, you need to add error handling if the user enters inputs that you are not expecting. Also, this is more pseudocode than a true implementation.
After this while loop is when you would want to total the amount. Also, in the while loop you might want to have a variable keeping the total amount. Something like:
total += bill0;
after the line:
double bill0 = input.nextDouble();
might do the trick.
Matt Jones is correct - your loop (basically) runs forever - (overflow means it doesn't truly run forever, but it's close enough).
It seems you're trying to break the loop once the user enters "No". That means you don't know how many iterations you're going to need, so a while loop is more suited to this task than a for loop. So let's use a while loop to do that.
Scanner input = new Scanner(System.in);
double total;
System.out.println("Please enter your bill amount.");
double bill0 = input.nextDouble();
//loop until the user enters the phrase "No".
while(true) {
System.out.println("Is there another customer?");
String customer = input.nextLine();
if (customer.equalsIgnoreCase("Yes")) {
System.out.println("Please enter their bill.");
double bill = input.nextDouble();
} else if(customer.equalsIgnoreCase("No") {
System.out.println("Okay, let's total your bill amount.");
break; //End the loop
} else{
System.out.println("Sorry, unrecognized input. Please enter yes or no.");
}
}
//Do post-loop totaling and stuff.
I would use a while loop
while (scannerInput.equals("yes"))
//do your thing
}
Once the scanner input doesn't equal "yes" anymore, it will exit the while and do something else
for loops are more to iterate through a set of data, rather than a while, which is waiting for state change.
Scanner input = new Scanner(System.in);
double total;
System.out.println("Please enter your bill amount.");
double bill0 = input.nextDouble();
System.out.println("Is there another customer?");
while((String customer = input.nextLine()).equalsIgnoreCase("Yes")) {
System.out.println("Please enter their bill.");
double bill = input.nextDouble();
}
System.out.println("Okay, let's total your bill amount.");

Tip Calculator statement confusion

Here is the code. Basically after I enter the second customers bill amount it will prematurely read me a tip and asks if there is another customer. I want it to ask me if there is another customer but I don't want it to ask about the tip till I enter "n". Any thoughts?
import java.util.Scanner;
public class H3_TipCalc {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println("Please enter your bill amount.");
double bill = input.nextDouble();
String multiplecust = ("Y");
//int mc=1;
while (multiplecust.equalsIgnoreCase("y"))
{
Scanner usrin = new Scanner (System.in);
System.out.println("Is there another customer? y or n?");
multiplecust = usrin.nextLine();
//mc++;
if (multiplecust.equals("y")){
System.out.println("Please enter their bill amount.");
}
else if (multiplecust.equals("n")){
System.out.println("What tip prcentage would you like to use? Enter as a decimal.");
}
double tip = usrin.nextDouble();
System.out.println("Your tip owed will be " + bill*tip + ".");
}
}
}
Because of the else if, you can either enter the last customer's bill amount or the tip percentage, but not both. Instead, try this logic:
while( there are more individual customer amounts to enter ) {
enter next customer amount
}
get tip percentage
show final bill
Notice that the tip percentage is entered after the while loop, because it's just a one-time entry.
You got your logic twisted.
Better move the complete logic inside the loop and check for exit at the end of it (not the start):
do
read bill amount
read tip amount
read next customer
while (wants to continue)

Categories

Resources