0

This is my bank account program where users can add, withdraw money and view account details. This data is being passed back and forth between the client and server. I've been getting this NoSuchElementException for this line: temp1 = sScan.nextInt(); Where I accept the menuchoice from the server. Why is this? I'm only using it to get back to the client.

Here's the client code:

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Main {
    public static void main(String args[])  {
        {
            String user_name=null,type,temp;
            type = null;
            int balance=0,tmp=0,temp1=0;
            int withdraw=0,currentBal=0;
            int randNumber = 0; 
randNumber = (int)((Math.random() * 9000)+1000); 

            //create_account use = new create_account("user",0,"savings"); 

                Scanner in = new Scanner(System.in);
                Scanner strng=new Scanner(System.in);
                try{
                    //new socket for communicatio
                Socket s = new Socket("127.0.0.1", 4);
                //scanner for accepting data from serve
                Scanner sScan = new Scanner(s.getInputStream());
                //new printstream for passing values to server
                PrintStream p = new PrintStream(s.getOutputStream());
                 String str = sScan.nextLine();
                 System.out.println(str);


                int userChoice;
                boolean quit = false;
                 System.out.println("Welcome to UNCC Banking, please select from the menu");

                do {
                    //present menu
                      System.out.println("1. Deposit money");
                      System.out.println("2. Withdraw money");
                      System.out.println("3. Check balance");
                      System.out.println("0. to quit: \n");
                      System.out.print("Enter Your Choice : ");
                      //send userChoice to Server?
                      userChoice = in.nextInt();

                      p.println(userChoice);
                      //get number from server?
                       temp1 = sScan.nextInt();
                      switch (userChoice) {



                    case 1: // deposit

                        //p.println(tmp);
                       //temp1 = sScan.nextInt();
                     System.out.print("Enter Amount Of Money : ");
                     balance=in.nextInt();
                     p.println(balance);
                      int money = sScan.nextInt();
                     //user.Acc_Balance=balance;
                     System.out.println("\t Successfully Deposited.");


                       break;

                      case 2: // withdraw money                      
                          p.println("get withdraw");
                            int money2 = sScan.nextInt();

                                 if(money2==0)
                                 System.out.print("Your Account is Empty.");

                                 else{
                                 System.out.print("Enter Amount Of Money : ");   
                                 withdraw=in.nextInt();  
                                 p.println(withdraw);
                                  int money3=sScan.nextInt();

                                 if(withdraw>money2){
                                 System.out.print("Please enter Valid Amout of Money : ");
                                 withdraw=in.nextInt();
                                 p.println(withdraw);
                                  money3=sScan.nextInt();

                                 }
                                 else
                                 //currentBal= user.withdraw(withdraw);

                                 System.out.println("Your Current Balance : "+money3); 
                                 //p.println(currentBal);
                                 }


                            break;

                      case 3: // check balance 
                                p.println("get balance");
                                int mula1 = sScan.nextInt();
                                 System.out.println("Your Current Balance : "+mula1);
                                 //p.println(user.Acc_Balance);

                          break;


                      case 0:
                            quit = true;
                            break;
                      default:
                            System.out.println("Invalid Choice.");
                            break;
                      }
                      System.out.println("\n");
                } while (!quit);
                System.out.println("Thank you for visiting UNCC Banking");

         }

        catch(Exception e){e.printStackTrace();}
    }
    } 

} 

Here's the server :

import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

class BankAccount  {
    String name,acc_type;

    static int Acc_Balance;

    BankAccount(){

    }


        BankAccount(String n,int b,String a_t) {



        name=n;

           Acc_Balance=b;

           acc_type=a_t;


        }
        public static void main(String[] args)
        {
            try{
                 ServerSocket s1 = new ServerSocket(4);
                 Socket s2 = s1.accept();
                 Scanner sc=new Scanner(s2.getInputStream());
                 PrintStream p = new PrintStream(s2.getOutputStream());
                 int initialAmount = 0;
                 System.out.println("Connected");
                 p.println("connected");
                 int menuChoice = sc.nextInt();
                 p.println(menuChoice);

                 switch(menuChoice)
                 {
                 case 1:
                     int money = sc.nextInt();
                     //Acc_Balance = money
                     initialAmount = initialAmount+money;
                     p.println(initialAmount);
                     break;
                 case 2:
                     String getBalance1 = sc.nextLine();
                     p.println(initialAmount);
                     int withD = sc.nextInt();
                     initialAmount = initialAmount-withD;
                     p.println(initialAmount);
                     break;
                 case 3:
                     String getBalance = sc.nextLine();
                     p.println(initialAmount);
                     break;
                 default:
                     break;



                 }


        }
           catch(Exception e)
            {
               e.printStackTrace();
            }

        }


} // end class
Ammanuel
  • 1
  • 3

0 Answers0