0

im trying to make a program where only if you say yes to opening an account you can deposit money in it but im not sure on how to do it in my program the scanner inside of the if statemnt isnt working in other word if you said yes to opening an account after some steps i want to ask from user if he wants to deposit money or not

import java.util.Scanner;

public class main{

    public static void main(String[] args) {
        System.out.println(bank.Name);
        System.out.println(bank.location);
        System.out.println("bank id is: " + bank.bankId);

        customer open = factory.getInstance(1);
        customer close = factory.getInstance(2);
        customer deposit = factory.getInstance(3);

        System.out.print("you want to open or close account ");  
        Scanner y= new Scanner(System.in); 
        String str= y.nextLine();                
        if(str.equals("open")  ){
            System.out.println(open.getOpen());
            System.out.print("would you like to deposit money in your account ");
            Scanner isdeposit= new Scanner(System.in); 
            String x= isdeposit.nextLine();
            if(x.equals("yes") ){
                System.out.println(deposit.getDeposit());
            }
        }
        else if(str.equals("close")){
        System.out.println(close.getClose());
        }
        else{
        System.out.println("invlaid input");
        }
      }
    }

enter image description here

SternK
  • 11,649
  • 22
  • 32
  • 46
  • i forgot to say if more info needed on my code please tell me and ill provide you with everything thank you for your help – Kareem Alsayed Sleman Feb 10 '23 at 00:08
  • 1
    To summarise, instead of creating a new instance of scanner inside the if block, only create one scanner and reuse it over the entire program. – Tim Moore Feb 10 '23 at 00:20
  • What problem are you facing. Although I agree to Tim's comment, I could not find issues with your current code except that your open.getOpen() and close.getClose() does not really make sense. Why are you using that? – VinodA Feb 10 '23 at 00:24
  • can you show the error that shows up when you run the program? post a screenshot. Show more context so we know what you did before you encountered the error. – muhammad muzaib Feb 10 '23 at 00:42
  • Read this: https://stackoverflow.com/help/how-to-ask This tells you how to ask questions in a better way. – muhammad muzaib Feb 10 '23 at 00:44
  • Don't create multiple scanners on the same source, use one and only one scanner. – Mark Rotteveel Feb 14 '23 at 16:31

0 Answers0