when user logs out the user may choose to log back in again. For Logging back in: If the user get the pin code wrong 3 times, the program terminates.
System.out.println("You have logged out");
System.out.print("Please Enter Pin: ");
pin2 = sc.nextInt();
while (pin != pin2){
while (ctr < 2){
System.out.print("Please Enter Pin: ");
pin2 = sc.nextInt();
ctr++;
}
}
If I understand your problem correctly you will want to have something like that:
while (pin == pin2) {
System.out.println("What would you like to do?");
System.out.println("1 - Check Balance");
System.out.println("2 - Deposite");
System.out.println("3 - Withdraw");
System.out.println("4 - Change Pin");
System.out.println("5 - End Transaction");
sel = sc.nextInt();
switch (sel) {
case 1:
System.out.println("Your current balance is " + bal);
break;
case 2:
System.out.println("How much would you want to deposite? ");
dep = sc.nextInt();
bal = dep + bal;
System.out.println("Your new current balance is " + bal);
break;
case 3:
System.out.println("How much would you want to Withdraw? ");
with = sc.nextInt();
if (with > bal) {
System.out.println("You do not have that amount on your account! Please enter again.");
} else {
System.out.println("You withdrew " + with);
bal = bal - with;
System.out.println("Your new current balance is " + (bal));
}
break;
case 4:
System.out.print("Please enter a new pin: ");
pin = sc.nextInt();
System.out.println("Please verify your new pin: ");
pin2 = sc.nextInt();
break;
case 5:
System.out.println("Please Enter Pin: ");
pin = sc.nextInt();
break;
default:
break;
}
}
Basically, I've deleted the loop label, it is not necessary and I consider it a bad style. I've also changed the while condition, so the program runs as long as user enters exactly the same pin as he confirmed at the beginning. Moreover, I think it is better to read the value of sel after printing instructions, not before as you did.
Like what been said in the comments, avoid using Labels and/or goto rep.
Use something like that :
import java.util.Scanner;
public class AtmMachine {
public static void main(String[] args){
int actualPin = -1;
int sel = 0, pin, pin2, check, ctr = 0, dep, with, bal = 10000;
Scanner sc = new Scanner(System.in);
while(actualPin == -1)
{
System.out.print("Please enter a new pin: ");
pin = sc.nextInt();
System.out.print("Please verify your new pin: ");
pin2 = sc.nextInt();
if(pin == pin2) actualPin = pin;
else System.out.println("Error");
}
boolean logged = false;
while (true){
if(logged){
System.out.println("What would you like to do?");
System.out.println("1 - Check Balance");
System.out.println("2 - Deposite");
System.out.println("3 - Withdraw");
System.out.println("4 - Change Pin");
System.out.println("5 - End Transaction");
sel = sc.nextInt();
switch (sel){
case 1:
System.out.println("Your current balance is "+ bal);
break;
case 2:
System.out.println("How much would you want to deposite? ");
dep = sc.nextInt();
bal= dep+bal;
System.out.println("Your new current balance is "+ bal);
break;
case 3:
System.out.println("How much would you want to Withdraw? ");
with = sc.nextInt();
if(with > bal){
System.out.println("You do not have that amount on your account! Please enter again.");
}
else{
System.out.println("You withdrew "+ with);
bal = bal-with;
System.out.println("Your new current balance is "+ (bal));
}
break;
case 4:
System.out.print("Please enter a new pin: ");
pin = sc.nextInt();
System.out.println("Please verify your new pin: ");
pin2 = sc.nextInt();
if(pin == pin2) actualPin = pin;
else System.out.println("Error");
break;
case 5:
logged = false;
break;
default:
break;
}
else{
System.out.println("Please Enter Pin: ");
sel = sc.nextInt();
logged = actualPin == sel;
}
}
}
}
Related
I am currently making a simple ATM program in java.
I want to write a while loop where when user enters wrong pin it will prompt the user to enter again until the pin is matched. When the pin is matched, it will display the main menu.
I tried by myself, but I don't know how to fix it.
while(userPIN != savedPIN)
{
System.out.print("Please enter your correct PIN : ");
Scanner again = new Scanner(System.in);
int pass = again.nextInt();
break;
}
Remove the `break;' statement and update userPIN with the new pin as follows:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int savedPIN = 4444;
Scanner input = new Scanner(System.in);
System.out.println("Enter password");
int userPIN = input.nextInt();
double withdraw = 0.0, amount = 0.0, deposit = 0.0;
while (userPIN != savedPIN) {
System.out.print("Please enter your correct PIN : ");
Scanner again = new Scanner(System.in);
userPIN = again.nextInt();
}
while (userPIN == savedPIN) {
System.out.println(" 1 - Inquire Balance \n 2 - Withdraw \n 3 - Deposit \n 0 - Quit ");
Scanner inputNum = new Scanner(System.in);
int number = inputNum.nextInt();
switch (number) {
case 1:
System.out.println("The current balance is $" + amount);
break;
case 2:
System.out.println("Enter the amount to withdraw : ");
withdraw = input.nextDouble();
if (amount >= withdraw) {
amount = amount - withdraw;
System.out.println("The current balance is $" + amount);
} else {
System.out.println("Insufficient balance");
}
break;
case 3:
System.out.println("Enter the amount to deposit : ");
deposit = input.nextDouble();
amount = amount + deposit;
System.out.println("The current balance is $" + amount);
break;
case 0:
System.exit(4);
}
}
}
}
Ok 2 errors:
1)you test userPIN != savedPIN but you accept the value into variable pass with which you do nothing.
2)remove the break in the first loop it will always exit without looping.
it should look like :
while(pass!= savedPIN)
{
System.out.print("Please enter your correct PIN : ");
Scanner again = new Scanner(System.in);
int pass = again.nextInt();
}
I am new to Java and learning. I am making a looping menu in Java. But when I select "a" and enter the details it doesn't go back to the menu.
I have done a bit of research and I need to add a Do and While loop here, but I'm confused on how to implement that here. A bit of guidance is extremely appreciated
Heres my code below:
switch(selection) {
case 'A':
case 'a':
System.out.print("Enter a Trip Date: ");
date = input.nextLine();
System.out.print("Enter Trip Point: ");
enter_point = input.nextLine();
System.out.print("Enter Exit Point: ");
exit_point = input.nextLine();
break;
case 'B':
case 'b':
System.out.print("Enter Breakdown Date: ");
breakdown = input.nextLine();
System.out.print("Enter Sector Breakdown Occured in: ");
sector_break = input.nextInt();
System.out.print("Enter Vehicle Recovery Cost: ");
rec_cost = input.nextDouble();
break;
case 'x':
case 'X':
System.out.println("Exiting data entry menu..");
break;
//equivalent to an else
default:
System.out.println("ERROR! - Please Enter a Valid Selection!");
}
while(selection != 'X');
Is this what you needed?
char selection;
do
{
do
{
//Menu
System.out.println("Toll Road Data Entry Menu");
System.out.println("-----------------------------------------");
System.out.println("A - Record Trip");
System.out.println("B - Record Breakdown Incident");
System.out.println("X - Exit");
System.out.print("Enter Your Selection: ");
selection = input.nextChar();
if (selection!='a' || selection!='A' || selection!='b' || selection!='B' || selection!='x' || selection!='X')
{
System.out.println("Selection must be a single character, A,B or X");
continue;
}
else
break;
} while (1);
switch(selection)
{
case 'A':
case 'a':
System.out.print("Enter a Trip Date: ");
date = input.nextLine();
System.out.print("Enter Trip Point: ");
enter_point = input.nextLine();
System.out.print("Enter Exit Point: ");
exit_point = input.nextLine();
break;
case 'B':
case 'b':
System.out.print("Enter Breakdown Date: ");
breakdown = input.nextLine();
System.out.print("Enter Sector Breakdown Occured in: ");
sector_break = input.nextInt();
System.out.print("Enter Vehicle Recovery Cost: ");
rec_cost = input.nextDouble();
break;
case 'x':
case 'X':
System.out.println("Exiting data entry menu..");
break;
//equivalent to an else
default:
System.out.println("ERROR! - Please Enter a Valid Selection!");
}
} while(selection != 'X');
I usually through them in while loops that way it just through me back to main menu when done
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Scanner input = new Scanner(System.in);
boolean menu=true;
int selection,sector_break;
String date,enter_point,exit_point,
breakdown;
double rec_cost;
while(true)
{
System.out.print(String.format("\033[2J"));
System.out.print("Your Menu Title \n\n");
System.out.print("1. Selection A\n");
System.out.print("2. Selection B\n");
System.out.print("3. Selection C\n");
System.out.print("4. Selection D\n");
System.out.print("5. Exit Menu D\n");
selection = Integer.parseInt(input.nextLine());
if(selection<1 || selection>4)
return;
switch(selection){
case 1:
System.out.print("Enter a Trip Date: ");
date = input.nextLine();
System.out.print("Enter Trip Point: ");
enter_point = input.nextLine();
System.out.print("Enter Exit Point: ");
exit_point = input.nextLine();
break;
case 2:
System.out.print("Enter Breakdown Date: ");
breakdown = input.nextLine();
System.out.print("Enter Sector Breakdown Occured in: ");
sector_break = input.nextInt();
System.out.print("Enter Vehicle Recovery Cost: ");
rec_cost = input.nextDouble();
break;
case 3:
// your code here
default:
// your code here
}
}
}
}
Im Trying to put something in case 1 in the switch that when i add the new pin code it will display "*" rather than the actual number
import java.util.Scanner;
public class Main {
public Main() {
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int userChoice;
boolean quit = false;
System.out.println("Welcome to GNBanking");
System.out.println("Please Create Your Account");
String name;
String add;
int contact;
double initialdep;
int pin;
int random = (int )(Math.random() * 999999 + 1);
Scanner reg = new Scanner (System.in);
System.out.println("Please Fill out the information below");
System.out.println("Enter Your Name: ");
name = reg.nextLine();
System.out.println("Enter Your Address: ");
add = reg.nextLine();
System.out.println("Enter Your Contact Number: ");
contact = reg.nextInt();
System.out.println("Enter Your Initial Deposit");
initialdep = reg.nextInt();
System.out.println("Enter Your Pin: ");
pin = reg.nextInt();
System.out.println("Congratulations You Are Now A Member Of GNBanking Please Confirm Your Account");
System.out.println("Name of Member: " +name);
System.out.println("Address: "+ add);
System.out.println("Contact Number:" +contact);
System.out.println("Initial Deposit: "+initialdep);
System.out.println("Pin " + pin);
System.out.println("Is the Information Accurate? Y/N ");
String choice =reg.next();
if(choice.equalsIgnoreCase("Y")){
System.out.println("Excellent!!");
System.out.println("Your Account Number is: "+ random );
System.out.println("Name of Member: " +name);
System.out.println("Address: "+ add);
System.out.println("Contact Number:" +contact);
System.out.println("Initial Deposit: "+initialdep);
System.out.println("Pin: " + pin);
}else if(choice.equalsIgnoreCase("N")){
System.out.println("Do You Want to try again? Y/N");
String secchoi = reg.next();
if(secchoi.equalsIgnoreCase("Y")){
main (null);
}else if (secchoi.equalsIgnoreCase("N")){
System.out.println("Have A Nice Day!");
}
}
do {
System.out.println("[1] Change Pincode");
System.out.println("[2] View Balance");
System.out.println("[3] Deposit Money");
System.out.println("[4] Withdraw ");
System.out.println("[5] Close Account");
userChoice = in.nextInt();
switch (userChoice) {
case 1:
// Here Im trying to create a change pin that wont show the int but rather the character "*" when I type
break;
case 2:
// View Balance
break;
case 3:
// Deposit
break;
case 4:
// Withdraw
break;
case 5:
quit = true;
break;
default:
System.out.println("Wrong choice.");
break;
}
System.out.println();
} while (!quit);
System.out.println("Have A Nice Day");
}
}
*/
Use below code in the programm, If you want to display it after taking input.
int value=1234567780; //Your pincode
String data=String.valueOf(value).replaceAll("[0-9]", "*"); //replacing and storing it into new String variable
System.out.println(data);
This one you can use while taking inputs from the user in password format.
System.out.println("Please Enter your pincode: ");
char[] pinCode= cosole.readPassword();
String data = new String(pinCode);
NOTE: The cosole.readPassword() may not work in IDE , You have to run the programm through console.
I'm not very adept in getting the running totals using Java as I've started recently. I have to display and hold the running total of the bank balance and for some strange reason, it's resetting back to 100, which is what I declared it as to start with. Is there any way for me to stop the bank balance from being reset every time it loops?
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int choice, totBal = 0, totWith = 0, totDep = 0;
double with, remBal = 0, deposit, bankBal = 100;
char reply = 0;
do
{
System.out.println("");
System.out.println("Bank online\n");
System.out.println("1. Withdraw");
System.out.println("2. Deposit");
System.out.println("3. Balance");
System.out.println("4. Account Details");
System.out.println("5. Exit\n");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
if(choice == 1)
{
System.out.print("How much do you wish to withdraw?\n");
with = sc.nextInt();
remBal = bankBal - with;
System.out.println("Your new balance is: " + remBal);
totWith++;
}
else if(choice == 2)
{
System.out.print("How much do you wish to deposit?\n");
deposit = sc.nextInt();
remBal = remBal + deposit;
System.out.println("Your new balance is: " + remBal);
totDep++;
}
else if(choice == 3)
{
System.out.println("Your balance is: " + remBal);
totBal++;
}
else if(choice == 4)
{
System.out.println("You made " + totWith + " withdrawls from your account.");
System.out.println("You made " + totDep + " deposits to your account.");
System.out.println("You made " + totBal + " balance checks on your account.");
}
else if(choice == 5)
{
}
System.out.println("");
System.out.print("Do you want to enter another option?(y/n): ");
reply = sc.next().charAt(0);
}while(reply == 'Y' || reply == 'y');
System.out.println("Thank you and goodbye!");
}
}
Also, I feel that I have WAY too many variables. How can I cut back on these?
Your problem is with following statement:
double with, remBal = 0, deposit, bankBal = 100;
Here you are initialising remBal as 0, while when one deposits amount/checks balance you do:
remBal = remBal + deposit;//you use remBal for blaance check
So on first attempt it will try to add 0 with say $100 which will be 100 while bankBal is 100 it should be 100. So initialize remBal same as bankBal (or use just one variable for bankBalance i.e. either of one).
You set the bankBal value to 100 at the start of the program.
When doing withdrawals, you always do
remBal = bankBal - with
which will always equate to
remBal = 100 - with
since you never change bankBal to reflect the updated balance after each loop.
One approach to solve this is to remove the
bankBal
variable altogether and simply set your
remBal
variable to your desired starting value.
Finally change the withdrawal computation mentioned above to
remBal = remBal - with
One thing you can do is implement switch-cases to call methods specific to Depost, Withdraw, etc. An example of this roughly would be:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please Enter what you would like to do: ");
String enterPrompt = input.next();
int options = 5;
switch (options) {
case 1: enterPrompt = "Deposit";
doDeposit();
break;
case 2: enterPrompt = "Withdrawel";
doWithdrawel();
break;
case 3: enterPrompt = "Balance";
viewBalance();
break;
case 4: enterPrompt = "Account Details";
viewAccount();
break;
case 5: enterPrompt = "Exit";
System.exit(1);
break;
}
public void doDeposit(){
//local variables here
//Do stuff
}
public void doWithdrawel(){
//local variables here
//Do stuff
}
public void viewBalance(){
//local variables here
//Do stuff
}
public void viewAccount(){
//local variables here
//Do stuff
}
}
I'm trying to make a calculator where the person can continue to put in numbers like "2+4*7-1" until they press = and then they will get the answer, and I have no idea how to even start. I know how to make a calculator with just 2 numbers but not how to have the user giving new numbers all the time. If anyone have any tips/code I could look at that would help a lot.
Check this Creating a Calculator using JFrame , and this is a step to step tutorial
yes yes i know that i am replying after 2 years but still maybe it will come in handy to other ppl in the future.
its a simple console code no gui.
So here's how i did it on eclipse
import java.util.Scanner;
public class Adv_calc {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int option;
double num1, num2, result;
result = 0;
do {
System.out.println("Welcome to The Calculator app");
System.out.println("Please Choose an option");
System.out.println("1) Add");
System.out.println("2) Subtract");
System.out.println("3) Multiply");
System.out.println("4) Continue");
System.out.println("5) Exit");
System.out.print("Option :: ");
option = sc.nextInt();
switch (option) {
case 1: {
System.out.println("Addition Calculator");
System.out.print("Kindly Enter the FIRST Number :: ");
num1 = sc.nextDouble();
System.out.print("Kindly Enter the SECOND Number :: ");
num2 = sc.nextDouble();
result = num1 + num2;
System.out.print("The Result is :: ");
System.out.println(result);
break;
}
case 2: {
System.out.println("Subtraction Calculator");
System.out.print("Kindly Enter the FIRST Number :: ");
num1 = sc.nextDouble();
System.out.print("Kindly Enter the SECOND Number :: ");
num2 = sc.nextDouble();
result = num1 - num2;
System.out.print("The Result is :: ");
System.out.println(result);
break;
}
case 3: {
System.out.println("Multiplication Calculator");
System.out.print("Kindly Enter the FIRST Number :: ");
num1 = sc.nextDouble();
System.out.print("Kindly Enter the SECOND Number :: ");
num2 = sc.nextDouble();
result = num1 * num2;
System.out.print("The Result is :: ");
System.out.println(result);
break;
}
case 4: {
System.out.println("Please Choose an option");
System.out.println("1) Add");
System.out.println("2) Subtract");
System.out.println("3) Multiply");
System.out.print("Option :: ");
option = sc.nextInt();
switch (option) {
case 1: {
System.out.println("Addition Calculator");
System.out.print("Kindly Enter the SECOND Number :: ");
num2 = sc.nextDouble();
result = result + num2;
System.out.print("The Result is :: ");
System.out.println(result);
break;
}
case 2: {
System.out.println("Subtraction Calculator");
System.out.print("Kindly Enter the SECOND Number :: ");
num2 = sc.nextDouble();
result = result - num2;
System.out.print("The Result is :: ");
System.out.println(result);
break;
}
case 3: {
System.out.println("Multiplication Calculator");
System.out.print("Kindly Enter the SECOND Number :: ");
num2 = sc.nextDouble();
result = result * num2;
System.out.print("The Result is :: ");
System.out.println(result);
break;
}
}
break;
}
case 5: {
System.out.println("Thank you for using my program :: ");
System.out.println("Program will now exit ");
System.exit(0);
}
}
} while (option != 5);
}
}