-2

When I try to run my code, this is what I'm getting as error:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Main.main(Main.java:86)

I tried searching online but found nothing, here is my full code: (I believe my code is fully working and has no actual "errors" though)

import java.util.*;

class Account{
    String name,acc_type;
    int Acc_num,Acc_Balance;
    Account(){

    }
        Account(String n,int acc_num,int b,String a_t){
            name=n;
            Acc_num=acc_num;
            Acc_Balance=b;
            acc_type=a_t;
        }
} // end class

class create_account extends Account{
    create_account(String n,int acc_num,int b,String a_t){ // pass name and account type 
            name=n;
            Acc_num=acc_num;
            Acc_Balance=b;
            acc_type=a_t;
    }
    create_account(){
        super();
    }

    void insert(String n,int acc_num,String a_t){ // input user name, account number and type 
        name=n;
        acc_type=a_t;
        Acc_num=acc_num; // generate random number 
        Acc_Balance=0;
    }

    void display_details(){
        System.out.println("Depositor Name :" +name);
        System.out.println("Account Number : "+Acc_num);
        System.out.println("Account Balance : "+Acc_Balance);
        System.out.println("Account Type : "+acc_type);
    }

        void deposite(int acc_num,int money){                 
                Acc_Balance=money;    
        }

        int withdraw(int withd){
                Acc_Balance=Acc_Balance-withd;
                return Acc_Balance;
        }

} // end class 


public class Main {
    public static void main(String args[]){
        String user_name=null,type;
        type = null;
        int balance=0,tmp=0;
        int withd=0,cb=0;
// to generate Random Account Number 
int aNumber = 0; 
aNumber = (int)((Math.random() * 9000)+1000); 

        create_account user = new create_account("user",0,0,"savings"); // initilaize -- name,acc_number,Balance,Type

            Scanner in = new Scanner(System.in);
            Scanner strng=new Scanner(System.in);
            int userChoice;
            boolean quit = false;

            do {
                  System.out.println("1. Create Account");
                  System.out.println("2. Deposit money");
                  System.out.println("3. Withdraw money");
                  System.out.println("4. Check balance");
                  System.out.println("5. Display Account Details");
                  System.out.println("0. to quit: \n");
                  System.out.print("Enter Your Choice : ");
                  userChoice = in.nextInt();
                  switch (userChoice) {

                  case 1:
                        System.out.print("Enter your Name : ");
                        user_name=strng.nextLine(); 
                        System.out.print("Enter Accout Type : ");
                        type=in.next();
                        user.insert(user_name, aNumber, type);  // inserted 
                        System.out.println("\n\tYour Account Details\n\tDont Forget Account Number\n");
                        System.out.println("**************************");                       
                        user.display_details();
                        break;

                case 2: // deposite
                    System.out.print("Enter your account Number : ");
                    tmp=in.nextInt();
                 if(tmp==user.Acc_num){
                 System.out.print("Enter Amount Of Money : ");
                 balance=in.nextInt();
                 user.Acc_Balance=balance;
                 System.out.println("\t Successfully Deposited.");
              }                
                     else
                    System.out.println("Wrong Accoount Number.");          
                   break;

                  case 3: // withdraw money                      
                     System.out.print("Enter your account Number : ");
                      tmp=in.nextInt();

                          if(tmp==user.Acc_num){                         
                             if(user.Acc_Balance==0)
                             System.out.print("Your Account is Empty.");

                             else{
                             System.out.print("Enter Amout Of Money : ");   
                             withd=in.nextInt();  

                             if(withd>user.Acc_Balance){
                             System.out.print("Enter Valid Amout of Money : ");
                             withd=in.nextInt();
                             }
                             else
                             cb= user.withdraw(withd);
                             System.out.println("Your Current Balance : "+cb);   
                             }
                          }
                             else
                             System.out.println("Wrong Accoount Number.");  
                        break;

                  case 4: // check balance 

                      System.out.print("Enter your Account Number : ");
                      tmp=in.nextInt();

                             if(tmp==user.Acc_num){
                             System.out.println("Your Current Balance : "+user.Acc_Balance);
                             }
                             else
                             System.out.println("Wrong Accoount Number.");                         
                      break;

                  case 5: // display all info 

                      System.out.print("Enter your Account Number :");
                      tmp=in.nextInt();                     
                             if(tmp==user.Acc_num){                               
                             user.display_details();                             
                        }else
                             System.out.println("Wrong Accoount Number.");

                      break;
                  case 0:
                        quit = true;
                        break;
                  default:
                        System.out.println("Wrong Choice.");
                        break;
                  }
                  System.out.println("\n");
            } while (!quit);
            System.out.println("Thanks !");

     } //  end main function 

} //  end main class

I hope you guys can help me solve this because I never came across an issue like this and I'm honestly not sure how I can solve this..

Tried using different compilers and such but nothing solved this

I have also tried adding and removing code but nothing has fixed the issue which is why i'm making this thread.. I did not just make this without looking at all the other ones with a similar issue.

Jeff
  • 1
  • 1
  • It is best to only use one `Scanner` object to read from the console – GBlodgett May 23 '18 at 16:41
  • Yes, you have two `Scanner` objects that are reading from the same input stream, so they're interfering with each other. – k_ssb May 23 '18 at 16:44
  • i highly recommend using `userChoice = Integer.parseInt(in.nextLine());` instead of nextInt() since that won't clear the end of line character which may cause problems later down the line. – RAZ_Muh_Taz May 23 '18 at 16:46
  • 1
    _"I tried searching online but found nothing"_ -- Sorry, this cannot be true because there are MANY exact duplicate questions on StackOverflow alone. – Jim Garrison May 23 '18 at 17:29
  • I have tried adding and removing code but nothing has fixed the issue which is why i'm making this thread.. – Jeff May 24 '18 at 16:29
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). Your question can be answered very quickly and easily with your step-debugger. You should always try and solve your problems with a step debugger before coming to StackOverflow. –  May 24 '18 at 16:34

1 Answers1

-1
class Main {
public static void main(String args[]){
    String user_name=null,type;
    type = null;
    int balance=0,tmp=0;
    int withd=0,cb=0;

remove public keyword from public class Main and use class Main only, so your code works properly.

  • I tried doing that and now I'm getting the error: No "public class" found to execute. – Jeff May 24 '18 at 16:21
  • this answer is in no way correct, much less a solution to the problem and just causes more problems –  May 24 '18 at 16:32