读取数据时的InputMismatchException

时间:2018-05-03 04:09:48

标签: java exception networking server client

我正在编写一个内存匹配游戏,用户输入的坐标试图匹配网格上的图块,我试图在客户端和服务器之间来回发送坐标,出于某种原因,我得到了在循环的第二次迭代期间服务器文件中的第124行上的InputMismatchException,其中我从客户端文件读取整数坐标,这对我没有意义,因为服务器文件读取坐标正常而没有异常在第一次迭代期间抛出,我也在第一次读取它时打印坐标写入以显示它。我不熟悉这个例外,我不知道该怎么做。

这是服务器文件

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
import java.util.Scanner;

public class MemoryGame {

    static int[][] cards = new int[4][4];
    static boolean upDown[][] = new boolean[4][4];
    static Scanner s = new Scanner(System.in);

    public static void main(String[] args) {
        try{
            System.out.println("a");
             ServerSocket s1 = new ServerSocket(8080);
             System.out.println("b");
             Socket s2 = s1.accept();
             System.out.println("c");
             Scanner sc=new Scanner(s2.getInputStream());
             System.out.println("d");
             PrintStream p = new PrintStream(s2.getOutputStream());
             //DataInputStream is = new DataInputStream(s2.getInputStream());
             //DataOutputStream os = new DataOutputStream(s2.getOutputStream());
             System.out.println("e");
             int initialAmount = 0;
             System.out.println("Connected");
             p.println("connected");
             //os.writeUTF("connected");
             //String accept = is.readUTF();
             String accept = sc.nextLine();
        //setup();
         for (int i = 0; i < 4; i++) {
             for (int a = 0; a < 4; a++) {
                 upDown[i][a]=false;
             }
         }

         //randomize
         int num[] = {1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
         int[][] cards2 = new int[4][4];
         Random random = new Random();
         int temp, t;
         for (int j = 0; j <= 20; j++) {
             for (int x = 0; x < 16; x++) { //Randomize the card order
                 t = random.nextInt(1000) % 15;
                 temp = num[x];
                 num[x] = num[t];
                 num[t] = temp;

             }
             t = 0;
             for (int r = 0; r < 4; r++) // Cards receive Numbers
             {
                 for (int s = 0; s < 4; s++) {
                     cards2[r][s] = num[t];
                     t = t + 1;
                 }
             }

         }
        cards = cards2;
         //shuffle
         int start[] = {1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
         int cards3[][] = new int[4][4];
         Random ran = new Random();
         int tmp, i;
         for (int s = 0; s <= 20; s++) {
             for (int x = 0; x < 16; x++) //randomize the card placements
             {
                 i = ran.nextInt(100000) % 15;
                 tmp = start[x];
                 start[x] = start[i];
                 start[i] = tmp;
             }
         }
         i = 0;

         for (int r = 0; r < 4; r++) // put values in cards here
         {
             for (int c = 0; c < 4; c++) {
                 cards3[r][c] = start[i];
                 i = i + 1;
             }
         }

        //game(upDown, cards); // calls the game
         int noDownCards = 16;
         while (noDownCards > 0) {
             //displayBoard(upDown, cards);
             //String connected = sc.nextLine();
             System.out.println("     1 2 3 4 ");
             System.out.println("---+---------");
             for (int i1 = 0; i1 < 4; i1++) {
                 System.out.print(" " + (i1 + 1) + " | ");
                 for (int a = 0; a < 4; a++) {
                     if (upDown[i1][a]) {
                         System.out.print(cards[i1][a]);
                         System.out.print(" ");
                     }
                     else
                         System.out.print("* ");
                 }
                 System.out.println();
             }
             System.out.println();
             String s = "Enter co-oridinate 1";
             p.println(s);
             //os.writeUTF(s);

             //int g1x = is.readInt();
             int g1x = sc.nextInt();
             System.out.println(g1x);
             p.println(s);
             //os.writeUTF(s);
             //int g1y = is.readInt();
             int g1y = sc.nextInt();
             System.out.println(g1x);
             System.out.println(g1y);


             System.out.println(cards[g1x][g1y]);
             //os.writeUTF(s);
             p.println(s);

             int g2x = sc.nextInt();

             p.println(s);
             int g2y = sc.nextInt();

             System.out.println(cards[g2x][g2y]);
             if (cards[g1x][g1y] == cards[g2x][g2y]) {
                 System.out.println("You found a match");
                 upDown[g1x][g1y] = true;
                 upDown[g2x][g2y] = true;
                 noDownCards -= 2;
             }
             //os.writeInt(noDownCards);
             p.println(noDownCards);
             //is.readUTF();
             sc.nextLine();
         }
         //displaying the grid
         System.out.println("     1 2 3 4 ");
         System.out.println("---+---------");
         for (int i1 = 0; i1 < 4; i1++) {
             System.out.print(" " + (i1 + 1) + " | ");
             for (int a = 0; a < 4; a++) {
                 if (upDown[i1][a]) {
                     System.out.print(cards[i1][a]);
                     System.out.print(" ");
                 }
                 else
                     System.out.print("* ");
             }
             System.out.println();
         }
         System.out.println();

         System.out.println("You win");
        }

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

    }


}

这是客户端文件

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Client
{
//Start the Game
    public static void main(String[]args) {

        try
        {
        Socket s = new Socket("127.0.0.1", 8080);
        //scanner for accepting data from serve
       Scanner sScan = new Scanner(s.getInputStream());
        Scanner scan = new Scanner(System.in);

        //new printstream for passing values to server
        PrintStream p = new PrintStream(s.getOutputStream());
        //DataInputStream is = new DataInputStream(s.getInputStream());
        //DataOutputStream os = new DataOutputStream(s.getOutputStream());
         //String str = is.readUTF();
         String str = sScan.nextLine();
         System.out.println(str);
         String connect = "Players are connected";
         p.println(connect);
        // os.writeUTF(connect);

        MemoryGame m = new MemoryGame();
        Scanner s1 = new Scanner(System.in);

        int noDownCards = 16;
        while (noDownCards > 0) {

       // String accept = is.readUTF();
            String accept = sScan.nextLine();
            System.out.println("Hello again");
        System.out.println("Enter co-oridinate 1");
        String g1 = scan.next();
        if(g1.length()<2||g1.length()>2)
        {
         System.out.println("Invalid length");

        }
        //int g1x = Integer.valueOf(g1.charAt(0))-1;
        int g1x = Integer.valueOf(g1.substring(0, 1))-1;
        int g1y = Integer.valueOf(g1.substring(1, 2))-1;

        System.out.println(g1x);
        System.out.println(g1y);

        System.out.println("Sending coordinate");
        p.println(g1x);
        //os.writeInt(g1x);

        //String accept2 = is.readUTF();
        String accept2 = sScan.nextLine();
        p.println(g1y);
        //os.writeInt(g1y);

        //String accept3 = is.readUTF();
        String accept3 = sScan.nextLine();
        System.out.println("Enter co-oridinate 2");

        String g2 = scan.next();
        if(g2.length()<2||g2.length()>2)
        {

         System.out.println("Invalid length");

        }
        int g2x = Integer.valueOf(g2.substring(0, 1))-1;
        int g2y = Integer.valueOf(g2.substring(1, 2))-1;

        p.println(g2x);
        //os.write(g2x);

        //String accept4 = is.readUTF();
        String accept4 = sScan.nextLine();
        p.println(g2y);
        //os.write(g2y);

        //noDownCards = is.readInt();
        noDownCards = sScan.nextInt();
        //os.writeUTF(str);
        p.println(str);
        }
    }
        catch(Exception e)
        {
           e.printStackTrace();
        }
        }

}

0 个答案:

没有答案