Reputation: 1
I'm having a problem when I enter the "p" case, after it's finished it repeats the top part of my while loop again where at the end the user must put something in for "input" variable. The problem is the user never gets prompted to put anything in for "input" and then it loops again, re-printing all of the information it previously just printed. The second time around it does allow the user to put something in for "input". This only happens after I run the "p" case.
Someone please help me! Will be much appreciated!
package PayandGoParking;
/**
* Purpose: Input:
*
* Processing done:
*
* Output:
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.Scanner;
import java.util.*;
import java.io.*;
import java.text.DecimalFormat;
public class Assignment2 {
public static void main(String[] args) throws IOException {
Calendar now = Calendar.getInstance();
Scanner k = new Scanner(System.in);
DecimalFormat dm = new DecimalFormat("00");
String input = " ",
cardNumber,
x = "x";
double anyKey;
double currentTime,
currentCharge = 0,
securityCode,
totalCharge = 0;
Calendar paidTill;
int min = 0;
while (input != x) {
System.out.println("Welcome to Pay and Go parking");
System.out.println("+ to add 30 minutes ($1.00 charge)");
System.out.println("p to print the ticket");
System.out.println("r to restart");
System.out.println("x to exit the program");
System.out.print("Please enter your selection: ");
input = k.nextLine();
switch (input) {
case "+":
min += 30;
currentCharge += 1;
paidTill = (Calendar) now.clone();
paidTill.add(Calendar.MINUTE, min);
System.out.println("================================================");
System.out.printf("%s%s%s%s%s%.2s%s%.2s\n",
"Current Time: ", now.get(Calendar.MONTH),
"-", now.get(Calendar.DAY_OF_MONTH), " ", now.get(Calendar.HOUR_OF_DAY), ":",
dm.format(now.get(Calendar.MINUTE)));
System.out.printf("%s%s%s%s%s%.2s%s%.2s\n",
"Paid Till: ", paidTill.get(Calendar.MONTH),
"-", paidTill.get(Calendar.DAY_OF_MONTH),
" ", paidTill.get(Calendar.HOUR_OF_DAY), ":",
dm.format(paidTill.get(Calendar.MINUTE)));
System.out.printf("%s%.2f\n", "Current Charge $", currentCharge);
System.out.println("================================================");
break;
case "p":
System.out.print("Enter Credit Card Number: ");
cardNumber = k.nextLine();
System.out.print("Enter 3 digit security code from back of Credit Card: ");
securityCode = k.nextDouble();
System.out.println("=================================================");
System.out.println("Pay and Go Parking");
System.out.println("Lot Location: 123 Main St,");
System.out.println("Honolulu, Hawaii");
System.out.println("-----------------------------");
totalCharge += currentCharge;
paidTill = (Calendar) now.clone();
paidTill.add(Calendar.MINUTE, min);
System.out.printf("%s%s%s%s%s%.2s%s%.2s\n", "Current Time: ",
now.get(Calendar.MONTH), "-",
now.get(Calendar.DAY_OF_MONTH), " ", now.get(Calendar.HOUR_OF_DAY),
":", dm.format(now.get(Calendar.MINUTE)));
System.out.printf("%s%s%s%s%s%.2s%s%.2s\n",
"Paid Till: ", paidTill.get(Calendar.MONTH),
"-", paidTill.get(Calendar.DAY_OF_MONTH),
" ", paidTill.get(Calendar.HOUR_OF_DAY), ":",
dm.format(paidTill.get(Calendar.MINUTE)));
System.out.printf("%s%.2f\n", "Total charged to Credit Card $", currentCharge);
System.out.println("==========================================");
System.out.println("Tear off receipt and place on dash");
System.out.print("Press any key to continue");
anyKey = k.nextDouble();
System.out.println("================================================");
paidTill = (Calendar) now.clone();
System.out.printf("%s%s%s%s%s%.2s%s%.2s\n", "Current Time: ",
now.get(Calendar.MONTH),
"-", now.get(Calendar.DAY_OF_MONTH), " ",
now.get(Calendar.HOUR_OF_DAY), ":", dm.format(now.get(Calendar.MINUTE)));
System.out.printf("%s%s%s%s%s%.2s%s%.2s\n",
"Paid Till: ", paidTill.get(Calendar.MONTH),
"-", paidTill.get(Calendar.DAY_OF_MONTH),
" ", paidTill.get(Calendar.HOUR_OF_DAY), ":",
dm.format(paidTill.get(Calendar.MINUTE)));
currentCharge = 0;
min = 0;
System.out.printf("%s%.2f\n", "Current Charge $", currentCharge);
System.out.println("================================================");
break;
case "r":
currentCharge = 0;
min = 0;
paidTill = (Calendar) now.clone();
System.out.println("================================================");
System.out.printf("%s%s%s%s%s%.2s%s%.2s\n",
"Current Time: ", now.get(Calendar.MONTH),
"-", now.get(Calendar.DAY_OF_MONTH), " ",
now.get(Calendar.HOUR_OF_DAY), ":",
dm.format(now.get(Calendar.MINUTE)));
System.out.printf("%s%s%s%s%s%.2s%s%.2s\n",
"Paid Till: ", paidTill.get(Calendar.MONTH),
"-", paidTill.get(Calendar.DAY_OF_MONTH),
" ", paidTill.get(Calendar.HOUR_OF_DAY),
":", dm.format(paidTill.get(Calendar.MINUTE)));
System.out.printf("%s%.2f\n", "Current Charge $", currentCharge);
System.out.println("================================================");
break;
case "x":
input = x;
break;
}
}
System.out.println("\nTotals for Pay and Go Parking");
System.out.println("========================");
System.out.printf("%s%12s%7.2f\n", "Totals", "$", totalCharge);
}
}
Upvotes: 0
Views: 127
Reputation: 1129
This is likely because you're using System.out.print without flushing. Flushing means force sending the output buffer that System.out internally uses to the monitor. Only println is guaranteed to send what you printed to the monitor. Regular System.print does not unless there is a newline character somewhere in your string. Try
System.out.print("Please enter your selection: ");
System.out.flush();
In fact try adding System.out.flush() before every attempt to collect user input. Or add a new line in the string you're printing
System.out.print("Please enter your selection: \n");
Upvotes: 0
Reputation: 21
One of the first issues you should fix is on line 44:
while (input != x)
Should be:
while (!input.equals(x))
Using == or != when comparing two Strings does not check if they are equal to the same characters/words but if they are equal to the same String meaning point to the same location in memory.
I can't run the program to test it because I don't have your package "PayandGoParking". And your code is too long to read through to look for more problems. Try to make the code that is giving a problem more concise after you fix this.
Upvotes: 2