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.
Related
I am creating a basic banking app that tracks a user's bank account activities, and I cannot seem to figure out why when I run my code that it is simply running what I have set for the "default" case; so even when I press 1,2,3, or 4, the console states, "Error -- Please choose a valid option."
Thanks in advance!
package Account;
import java.util.Scanner;
public class Account extends Bank {
int Balance;
int Previoustransaction;
int amount;
int amount2;
String Name;
String ID;
Account(String Name,String ID){
}
void deposit(int amount) {
if (amount != 0) {
Balance+=amount;
Previoustransaction=amount;
}
}
void withdraw(int amount) {
if(amount!=0) {
Balance-=amount;
Previoustransaction = -amount;
}
}
void getPrevioustransaction() {
if(Previoustransaction > 0) {
System.out.println("Deposited:" + Previoustransaction);
}
else if(Previoustransaction<0) {
System.out.println("Withdrawn:" + Math.abs(Previoustransaction));
} else {
System.out.println("No transaction occurred.");
}
}
void Menu() {
int choice = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Welcome," + Name + ".");
System.out.println("Your account number is" + ID);
System.out.println("What would you like to do?");
System.out.println("1.Check balance.");
System.out.println("2. Make a deposit.");
System.out.println("3. Make a withrawl.");
System.out.println("4. Show last transaction.");
System.out.println("0. Exit.");
do {
System.out.println("Choose an option.");
choice = scan.nextInt();
System.out.println();
switch(choice) {
case'1':
System.out.println("Balance = $" + Balance);
System.out.println();
break;
case'2':
System.out.println("Enter an amount to deposit.");
int amount = scan.nextInt();
deposit (amount);
System.out.println();
break;
case'3':
System.out.println("Enter an amount to withdrawl.");
int amount2 = scan.nextInt();
withdraw(amount2);
break;
case '4':
getPrevioustransaction();
break;
case '0':
break;
default:
System.out.println("Error -- Please choose a valid option.");
}
} while (choice != 0);
System.out.println("Thank you for using the Bank Account Tracker!");
scan.close();
}
{
}
{
}
}
The reason your program isn't working as you expect is that:
you are prompting for user input
capturing that input as a numeric value; specifically, primitive data type int
comparing that int input against various character values – that is, values of primitive data type ch (such as '1')
Here's a paired down version of what you're doing:
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
switch (choice) {
case '1':
System.out.println("match");
break;
default:
System.out.println("some other input found: " + choice);
}
Here's that same block, but instead of case '1' (which matches on a single character value), I changed it to case 1 (which matches on an integer value):
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
switch (choice) {
case 1: // <-- this is the only edit, use 1 instead of '1'
System.out.println("match");
break;
default:
System.out.println("some other input found: " + choice);
}
So, to fix your program, change your various case statements to use integer values, not characters.
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();
}
Im trying to read text from a file and compare it to user input. But when I run it I can get an error that reads. I don't know what's wrong any help will be appreciated!
Exception in thread "main" java.lang.IllegalStateException: Scanner
closed
at java.base/java.util.Scanner.ensureOpen(Scanner.java:1150)
at java.base/java.util.Scanner.next(Scanner.java:1573)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at test123.main(test123.java:32)
import java.util.Scanner;
import java.io.*;
public class test123 {
public static void main(String[] args) throws IOException {
Scanner sc1 = new Scanner(System.in);
System.out.println("Please enter correct credentials to log in");
System.out.println("Username: ");
System.out.println("Password: ");
String userName = sc1.nextLine();
String passWord = sc1.nextLine();
File inFile = new File("employee.txt");
Scanner sc = new Scanner(inFile);
String uName = sc.nextLine();
String pWord = sc.nextLine();
sc.close();
if (userName.equals(uName) && passWord.equals(pWord)) {
System.out.println("Welcome " + userName + "!");
System.out.println("Menu: ");
System.out.println("\t1) Account");
System.out.println("\t2) Payroll");
System.out.println("\t3) Attendance Report");
System.out.println("\t4) Service Desk");
int answer = sc.nextInt();
switch (answer) {
case 1:
System.out.print("Welcome to Account!");
break;
case 2:
System.out.print("Welcome to Payroll!");
break;
case 3:
System.out.print("Welcome to Attendance Report!");
break;
case 4:
System.out.print("Welcome to Service Desk!");
break;
default:
System.out.print("Enter correct menu option");
}
} else {
System.out.println("Sorry, you entered invalid credentials. Try again. ");
}
}
}
You are closing it:
sc.close();
and then try to read from it:
if(userName.equals(uName) && passWord.equals(pWord))
{
System.out.println("Welcome " + userName + "!");
System.out.println("Menu: ");
System.out.println("\t1) Account");
System.out.println("\t2) Payroll");
System.out.println("\t3) Attendance Report");
System.out.println("\t4) Service Desk");
int answer = sc.nextInt(); //here
switch (answer) {
case 1:
System.out.print("Welcome to Account!");
break;
case 2:
System.out.print("Welcome to Payroll!");
break;
case 3:
System.out.print("Welcome to Attendance Report!");
break;
case 4:
System.out.print("Welcome to Service Desk!");
break;
default:
System.out.print("Enter correct menu option");
}
}
else
{
System.out.println("Sorry, you entered invalid credentials. Try again. ");
}
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;
}
}
}
}
I've set up a "menu" that prints to console. Takes user input, calls according method, and then should return to the menu for further instruction. How should I structure my code so that it outputs the "menu" after it's done doing whatever it's doing?
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
EntryNode n = new EntryNode();
AddressList addressBook = new AddressList();
String menu = " ";
System.out.println("******************************************************************");
System.out.println("Welcome to the Jackie 2000 Address Book");
System.out.println("What do you want to do? ");
System.out.println("[p] View All Entries in Address Book [a] Add New Entry");
System.out.println("[d] Remove An Entry [s] Search for Entry");
System.out.println("[i] Import Address Book [x] Export Address Book");
System.out.println("[z] Exit");
System.out.println();
System.out.println("Please enter your choice: ");
menu = keyboard.next().toLowerCase();
if (menu.equals("p")) {
try {
addressBook.printList();
}
catch (Exception e){
}
}
else if (menu.equals("a")) {
System.out.println("Enter in the first name ");
String firstName = keyboard.next().toUpperCase();
System.out.println("Enter in the last name ");
String lastName = keyboard.next().toUpperCase();
System.out.println("Enter in the phone number");
String phoneNum = keyboard.next().toUpperCase();
System.out.println("Enter in the email");
String email = keyboard.next().toUpperCase();
addressBook.addEntry(firstName,lastName,phoneNum,email);
}
else if (menu.equals("d")) {
EntryNode temp = head;
for (int i = 0; i <addressBook.length(); i++) {
System.out.println(i + " Name: " + temp.getFirstName() + " " + temp.getLastName() + " "
+ temp.getPhoneNum() + " " + temp.getEmail());
temp = temp.getNext();
}
System.out.println(" ");
System.out.println("Please enter the index of the entry you wish to delete ");
int index = keyboard.nextInt();
addressBook.removeEntry(index);
}
else if (menu.equals("s")) {
System.out.println("Do you want to search by email or name? ");
String decision = keyboard.next();
if (decision.equals("email")) {
System.out.println("What email address are you looking for? ");
String email = keyboard.next();
addressBook.searchEmail(email);
}
else if (decision.equals("name")) {
System.out.println("What name are you looking for?");
String name = keyboard.next();
addressBook.searchEntry(name);
}
else System.out.println("Invalid entry. Type in 'email' or 'name'");
}
else if (menu.equals("i")) {
addressBook.importBook();
}
else if (menu.equals("x")) {
addressBook.exportBook();
}
else if (menu.equals("e")) {
System.exit(0);
}
else {
System.out.println("Invalid Entry");
}
}
}
You should definitely take a look at java's switch statement: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
You could have the entire switch-case statement inside a while loop with a boolean for when it should exit. For example:
while(!exit){
switch(input){
case "a": do something;break;
case "d":...
...
case "e": exit = true;
}
}
If you want the same menu to be displayed again after the user entered a choice and the program executed what he had to do, just put the whole process in a while or do...while loop and only exit it when the user choose the exit option.
Put a menu printlns into a separate static method. Call it after each addressBook method call, just before you close else if, except for an exit case.