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);
}
}