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. ");
}
Related
In this class of Bank ,there are basic functions of Bank , however the real problem is in the switch case loop.
class Bank{
private int Balance;
private int Withdrawal;
private int Deposit;
private int AccountNumber;
void Transaction(){
Scanner input = new Scanner(System.in);
System.out.println("Enter the account number to whose account you want
to transfer money");
AccountNumber = input.nextInt();
System.out.println("Enter your current Balance");
Balance= input.nextInt();
}
void Display(){
System.out.println("Account number "+AccountNumber);
System.out.println("Balance is "+ Balance);
}
void deposit(){
Scanner input = new Scanner(System.in);
System.out.println("Enter the amount to deposit");
int n =input.nextInt();
Balance+=n;
}
void withdraw(){
Scanner input = new Scanner(System.in);
System.out.println("Enter the amount to withdraw");
int n =input.nextInt();
try {
if(n>Balance)
throw new Exception("The balance you have is insufficient");
}
catch(Exception e){
System.out.println(e);
}
}//void
I am trying to rerun the switch case loop, after inputing one value , i want it to ask user again all the options, i tried to use continue statement , but it shows error of continue outside the loop.Please help
public static void main(String args[])throws Exception{
Bank b1=new Bank();
Scanner input = new Scanner(System.in);
System.out.println("Enter the number corrosponding to your option ");
System.out.println("1 Set ");
System.out.println("2 Display ");
System.out.println("3 Deposit");
System.out.println("4 Withdraw");
System.out.println("5 Exit");
int num=input.nextInt();
switch(num){
case 1:
b1.Transaction();
//int c =input.nextInt();
//wants user to input value again so switch loop works
//again
break;
case 2:
b1.Display();
break;
case 3:
b1.deposit();
break;
case 4:
b1.withdraw();
break;
case 5:
return;
}
}//psvm
}
You could surround the switch in a
do{
int num=input.nextInt();
switch(num){
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
return;
}
}while(num!=some_value_to_exit_switch);
}
}
I'd place the switch content in a while loop with a boolean flag, then it will re-run until you are satisfied with the input~
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.
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 made an ATM program. I have a try catch that will ask for the user to type in their pin number. The pin number must be 5 digits. So the exception will check if it is 5 digits or not but the exception handling is not working. No matter what number I type in, it always says invalid number.
Here is my code the try catch is at the top of the program and the exception handling checkNumber is at the bottom of the program..
import java.util.ArrayList;
import java.util.Scanner;
public class BankMain
{
private double availableBal = 80;
private double totalBal = 100;
private double availableBal2 = 480;
private double totalBal2 = 500;
private double availableBal3 = 80;
private double totalBal3 = 100;
ArrayList<Integer> cardNum = new ArrayList<Integer>();
static Scanner input = new Scanner(System.in);
private String error; // String the error from the exception
{
error = "error";
}
public void cardNumbers()
{
Scanner cards = new Scanner(System.in);
Scanner input = new Scanner(System.in);
Scanner keyboard = new Scanner(System.in);
try
{
System.out.println("Please select a 5 digit card number");
cardNum.add(input.nextInt());
checkNumber();
}
catch (invalidNumber err)
{
System.out.println("Caught Error: " + err.getError());
}
System.out.println("Thank you! You're card number is " + cardNum);
System.out.println("Type 'c' to go back to main menu.");
String value = keyboard.next();
if (value.equalsIgnoreCase("c"))
{
menu();
}
else if (!keyboard.equals('c'))
{
System.out.println("Invalid Entry!");
}
}
public void menu()
{
System.out.println("ATM Menu:");
System.out.println();
System.out.println("1 = Create Account");
System.out.println("2 = Account Login");
System.out.println("3 = Exit ATM");
query();
}
public void startAtm()
{
menu();
}
public void drawMainMenu()
{
AccountMain main3 = new AccountMain();
int selection;
System.out.println("\nATM main menu:");
System.out.println("1 - View account balance");
System.out.println("2 - Withdraw funds");
System.out.println("3 - Add funds");
System.out.println("4 - Back to Account Menu");
System.out.println("5 - Terminate transaction");
System.out.print("Choice: ");
selection = input.nextInt();
switch (selection)
{
case 1:
viewAccountInfo();
break;
case 2:
withdraw();
break;
case 3:
addFunds();
break;
case 4:
AccountMain.selectAccount();
break;
case 5:
System.out.println("Thank you for using this ATM!!! goodbye");
}
}
public void viewAccountInfo()
{
System.out.println("Account Information:");
System.out.println("\t--Total balance: $" + totalBal);
System.out.println("\t--Available balance: $" + availableBal);
drawMainMenu();
}
public void viewAccountInfo2()
{
System.out.println("Account Information:");
System.out.println("\t--Total balance: $" + totalBal2);
System.out.println("\t--Available balance: $" + availableBal2);
drawMainMenu();
}
public void deposit(int depAmount)
{
System.out.println("\n***Please insert your money now...***");
totalBal = totalBal + depAmount;
availableBal = availableBal + depAmount;
}
public void checkNsf(int withdrawAmount)
{
if (totalBal - withdrawAmount < 0)
System.out.println("\n***ERROR!!! Insufficient funds in you accout***");
else
{
totalBal = totalBal - withdrawAmount;
availableBal = availableBal - withdrawAmount;
System.out.println("\n***Please take your money now...***");
}
}
public void addFunds()
{
int addSelection;
System.out.println("Deposit funds:");
System.out.println("1 - $20");
System.out.println("2 - $40");
System.out.println("3 - $60");
System.out.println("4 - $100");
System.out.println("5 - Back to main menu");
System.out.print("Choice: ");
addSelection = input.nextInt();
switch (addSelection)
{
case 1:
deposit(20);
drawMainMenu();
break;
case 2:
deposit(40);
drawMainMenu();
break;
case 3:
deposit(60);
drawMainMenu();
break;
case 4:
deposit(100);
drawMainMenu();
break;
case 5:
drawMainMenu();
break;
}
}
public void withdraw()
{
int withdrawSelection;
System.out.println("Withdraw money:");
System.out.println("1 - $20");
System.out.println("2 - $40");
System.out.println("3 - $60");
System.out.println("4 - $100");
System.out.println("5 - Back to main menu");
System.out.print("Choice: ");
withdrawSelection = input.nextInt();
switch (withdrawSelection)
{
case 1:
checkNsf(20);
drawMainMenu();
break;
case 2:
checkNsf(40);
drawMainMenu();
break;
case 3:
checkNsf(60);
drawMainMenu();
break;
case 4:
checkNsf(100);
drawMainMenu();
break;
case 5:
drawMainMenu();
break;
}
}
public void query()
{
Scanner keyboard = new Scanner(System.in);
double input = keyboard.nextInt();
if (input == 2)
{
BankMainPart2 main2 = new BankMainPart2();
System.out.println("Please enter your 5 digit card number.");
BankMainPart2.loginCard(cardNum);
}
else if (input == 1)
{
cardNumbers();
}
else if (input == 3)
{
System.out.println("Thank you, have a nice day!");
System.exit(0);
}
}
public void checkingMenu()
{
AccountMain main3 = new AccountMain();
int selection;
System.out.println("\nATM main menu:");
System.out.println("1 - View account balance");
System.out.println("2 - Withdraw funds");
System.out.println("3 - Add funds");
System.out.println("4 - Back to Account Menu");
System.out.println("5 - Terminate transaction");
System.out.print("Choice: ");
selection = input.nextInt();
switch (selection)
{
case 1:
viewAccountInfo2();
break;
case 2:
withdraw();
break;
case 3:
addFunds();
break;
case 4:
AccountMain.selectAccount();
break;
case 5:
System.out.println("Thank you for using this ATM!!! goodbye");
}
}
private static void checkNumber() throws invalidNumber // run the check activation exception
{
if (String.valueOf(input).length() != 5)
{
throw new invalidNumber("invalid number");
}
else
{
System.out.println("Works!");
}
}
public static void main(String args[])
{
BankMain myAtm = new BankMain();
myAtm.startAtm();
}
}
This code snippet looks fine:
if (String.valueOf(input).length() != 5)
{
throw new invalidNumber("invalid number");
}
else
{
System.out.println("Works!");
}
As long as you don't realize that input is not the double variable declared locally somewhere:
double input = keyboard.nextInt()
instead it's an instance of java.util.Scanner (!)
static Scanner input = new Scanner(System.in)
And Scanner.toString() is certainly not the PIN you want.
Why you are having three different instances of Scanner class.. That is what making the program confusing..
In the compareNumber() method, you are actually checking the value of input, which is an instance of Scanner.. It's better to use it like this: -
checkNumber(input.nextInt())
And add the number to the list in your checkNumber(int num) method..
Of course I am not saying that it is a good way of coding.. But it will solve your problem for the time being..
Else, there are so many issues with your code..
This is your try-catch block: -
try {
System.out.println("Please select a 5 digit card number");
cardNum.add(input.nextInt());
checkNumber();
} catch (invalidNumber err) {
System.out.println("Caught Error: " + err.getError());
}
And this is your checkNumber() method : -
private static void checkNumber() throws invalidNumber
{
if (String.valueOf(input).length() != 5) {
throw new invalidNumber("invalid number");
}
else {
System.out.println("Works!");
}
}
Now you must see that you are using input as a parameter to String.valueOf(input).
But you have declared 'input` as an instance of Scanner before your try-catch block..
Scanner cards = new Scanner(System.in);
Scanner input = new Scanner(System.in);
Scanner keyboard = new Scanner(System.in);
This code is in your codeNumbers() method..
So, clearly your input can never actually contain a user input, rather a hashcode representing the object new Scanner(System.in).
So, its better that you pass the integer input from user to checkNumber() method..
************ MODIFICATION Needed in Code..
So, your checkNumber()` will be modified as: -
private static void checkNumber(int number) throws invalidNumber
{
if (String.valueOf(number).length() != 5) {
throw new invalidNumber("invalid number");
}
else {
System.out.println("Works!");
}
}
And your call to this method in try-catch block will change to this: -
try {
System.out.println("Please select a 5 digit card number");
int number = input.nextInt();
cardNum.add(number);
checkNumber(number);
} catch (invalidNumber err) {
System.out.println("Caught Error: " + err.getError());
}
Your code checks that String.valueOf(input) has a length of 5 characters. But input is not the number entered by the user. It's the object of type Scanner that is used to parse what the user enters. So the result of String.valueOf(input) is probably something like java.util.Scanner#B09876.
I have a small program I am working on. I Just put a try catch in my code and everything seems to be working except for 1 thing. I will post my code below... As you can see my try catch statement in my code it tells the program to go down to the checkNumber method for exception handling. I keep getting an error on this part...
if (input == 5){
}
The input keeps underlining red and saying "Incompatible operand types scanner and int.
Not sure what the problem is... or how to fix it.. here is my code
import java.util.ArrayList;
import java.util.Scanner;
public class BankMain
{
private double availableBal =80;
private double totalBal =100;
private double availableBal2 =480;
private double totalBal2 =500;
private double availableBal3 =80;
private double totalBal3 =100;
ArrayList<Integer> cardNum = new ArrayList<Integer>();
static Scanner input = new Scanner(System.in);
private String error; //String the error from the exception
{
error = "error";
}
public void cardNumbers(){
Scanner cards = new Scanner(System.in);
Scanner input = new Scanner(System.in);
Scanner keyboard = new Scanner(System.in);
try{
System.out.println("Please select a 5 digit card number");
cardNum.add(input.nextInt());
checkNumber();
}
catch(invalidNumber err){
System.out.println("Caught Error: " + err.getError());
}
System.out.println("Thank you! You're card number is " +cardNum);
System.out.println("Type 'c' to go back to main menu.");
String value = keyboard.next();
if(value.equalsIgnoreCase("c")){
menu();
}
else if (!keyboard.equals('c')){
System.out.println("Invalid Entry!");
}
}
public void menu(){
System.out.println("ATM Menu:");
System.out.println();
System.out.println("1 = Create Account");
System.out.println("2 = Account Login");
System.out.println("3 = Exit ATM");
query();
}
public void startAtm()
{
menu();
}
public void drawMainMenu()
{
AccountMain main3 = new AccountMain();
int selection;
System.out.println("\nATM main menu:");
System.out.println("1 - View account balance");
System.out.println("2 - Withdraw funds");
System.out.println("3 - Add funds");
System.out.println("4 - Back to Account Menu");
System.out.println("5 - Terminate transaction");
System.out.print("Choice: ");
selection =input.nextInt();
switch(selection)
{
case 1:
viewAccountInfo();
break;
case 2:
withdraw();
break;
case 3:
addFunds();
break;
case 4:
AccountMain.selectAccount();
break;
case 5:
System.out.println("Thank you for using this ATM!!! goodbye");
}
}
public void viewAccountInfo()
{
System.out.println("Account Information:");
System.out.println("\t--Total balance: $"+totalBal);
System.out.println("\t--Available balance: $"+availableBal);
drawMainMenu();
}
public void viewAccountInfo2()
{
System.out.println("Account Information:");
System.out.println("\t--Total balance: $"+totalBal2);
System.out.println("\t--Available balance: $"+availableBal2);
drawMainMenu();
}
public void deposit(int depAmount)
{
System.out.println("\n***Please insert your money now...***");
totalBal =totalBal +depAmount;
availableBal =availableBal +depAmount;
}
public void checkNsf(int withdrawAmount)
{
if(totalBal -withdrawAmount < 0)
System.out.println("\n***ERROR!!! Insufficient funds in you accout***");
else
{
totalBal =totalBal -withdrawAmount;
availableBal =availableBal -withdrawAmount;
System.out.println("\n***Please take your money now...***");
}
}
public void addFunds()
{
int addSelection;
System.out.println("Deposit funds:");
System.out.println("1 - $20");
System.out.println("2 - $40");
System.out.println("3 - $60");
System.out.println("4 - $100");
System.out.println("5 - Back to main menu");
System.out.print("Choice: ");
addSelection =input.nextInt();
switch(addSelection)
{
case 1:
deposit(20);
drawMainMenu();
break;
case 2:
deposit(40);
drawMainMenu();
break;
case 3:
deposit(60);
drawMainMenu();
break;
case 4:
deposit(100);
drawMainMenu();
break;
case 5:
drawMainMenu();
break;
}
}
public void withdraw()
{
int withdrawSelection;
System.out.println("Withdraw money:");
System.out.println("1 - $20");
System.out.println("2 - $40");
System.out.println("3 - $60");
System.out.println("4 - $100");
System.out.println("5 - Back to main menu");
System.out.print("Choice: ");
withdrawSelection =input.nextInt();
switch(withdrawSelection)
{
case 1:
checkNsf(20);
drawMainMenu();
break;
case 2:
checkNsf(40);
drawMainMenu();
break;
case 3:
checkNsf(60);
drawMainMenu();
break;
case 4:
checkNsf(100);
drawMainMenu();
break;
case 5:
drawMainMenu();
break;
}
}
public void query(){
Scanner keyboard = new Scanner(System.in);
double input = keyboard.nextInt();
if (input == 2){
BankMainPart2 main2 = new BankMainPart2();
System.out.println("Please enter your 5 digit card number.");
BankMainPart2.loginCard(cardNum);
}
else if (input == 1){
cardNumbers();
}
else if (input == 3){
System.out.println("Thank you, have a nice day!");
System.exit(0);
}
}
public void checkingMenu()
{
AccountMain main3 = new AccountMain();
int selection;
System.out.println("\nATM main menu:");
System.out.println("1 - View account balance");
System.out.println("2 - Withdraw funds");
System.out.println("3 - Add funds");
System.out.println("4 - Back to Account Menu");
System.out.println("5 - Terminate transaction");
System.out.print("Choice: ");
selection =input.nextInt();
switch(selection)
{
case 1:
viewAccountInfo2();
break;
case 2:
withdraw();
break;
case 3:
addFunds();
break;
case 4:
AccountMain.selectAccount();
break;
case 5:
System.out.println("Thank you for using this ATM!!! goodbye");
}
}
private static void checkNumber() throws invalidNumber //run the check activation exception
{
if (input == 5)
{
System.out.println("");
}
else
throw new invalidNumber("invalid number");
}
public static void main(String args[])
{
BankMain myAtm = new BankMain();
myAtm.startAtm();
}
}
Class Bank Main 2
import java.util.ArrayList;
import java.util.Scanner;
public class BankMainPart2 {
public static void loginCard(ArrayList<Integer> cardNum){
BankMain main = new BankMain();
AccountMain main3 = new AccountMain();
Scanner logNum = new Scanner(System.in);
int loginInput = logNum.nextInt();
if (cardNum.contains(loginInput)) {
main3.selectAccount();
}
else {
System.out.println("Sorry, that pin number is incorrect!");
}
}
}
Your variable input is an object of Scanner which is declared globally and will be used for the method checknumber() and 5 is an int. Hence they are incompatible types.
The same statement works for method query() because for query you have locally defined a variable input which is of the type double.
What you should actually write is
if(input.nextInt() == 5){}