在客户端和服务器之间传递数据时出现NoSuchElementException

时间:2018-03-19 02:51:15

标签: java sockets exception networking nosuchelementexception

这是我的银行帐户计划,用户可以在其中添加,取款和查看帐户详细信息。此数据在客户端和服务器之间来回传递。我为这一行得到了这个NoSuchElementException:                        temp1 = sScan.nextInt(); 我从服务器接受menuchoice的地方。为什么是这样?我只是用它来回到客户端。

这是客户端代码:

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();}
    }
    } 

} 

这是服务器:

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

0 个答案:

没有答案