-3

我不确定是什么导致我的一种方法出现 Java 语法错误。我已经对有错误的地方发表了评论。

import java.io.*;
import java.net.*;
import java.util.*;

import Products.Items;

public class  ItemProcess{
    private static ArrayList<Items> itemList = new ArrayList<Items>();
    ServerSocket serverSocket = null;
    final int PORT = 1234;
    Socket client;
    ClientHandler handler;

    public static void main(String[] args) throws IOException {

        Items[] item = { new Items(123, "ABCDEE fghikop"),
                new Items(180, "hgiuhygihuvg KHJMLOP"), };
        for (int i = 0; i < item.length; i++)
            itemList.add(item[i]);  
        Calendar start = Calendar.getInstance();
        int date = start.get(Calendar.DATE);
        int month = start.get(Calendar.MONTH);
        int year = start.get(Calendar.YEAR);

        Scanner input = new Scanner(System.in);

        System.out.print("Enter finishing time in 24-hr format ");
        System.out.print("(e.g. 17:52) :  ");
        String timeString = input.nextLine();


        String hourString = timeString.substring(0,2);
        int hour = Integer.parseInt(hourString);


        String minString = timeString.substring(3,5);
        int minute = Integer.parseInt(minString);


        Calendar deadline = Calendar.getInstance();

        deadline.set(year,month,date,hour,minute,0);


        System.out.println("\n\nDeadline: "
                            + getDateTime(deadline) + "\n");


        Calendar now = Calendar.getInstance();

        while(now.before(deadline))
        {
            System.out.println(getDateTime(now));

            try
            {
                Thread.sleep(2000);
            }
            catch (InterruptedException intEx)
            {

            }


            now = Calendar.getInstance();
        }
        System.out.println("\n\nDeadline reached!!!\n");
    }

    public static String getDateTime(Calendar dateTime)
    {       


        String hour2Digits =
                String.format("%02d",
                        dateTime.get(Calendar.HOUR_OF_DAY));
        String min2Digits =
                String.format("%02d",
                            dateTime.get(Calendar.MINUTE));

        return(dateTime.get(Calendar.DATE) + "/"
                + (dateTime.get(Calendar.MONTH)+1) + "/"
                + dateTime.get(Calendar.YEAR) + "  "
                + hour2Digits + ":" + min2Digits);
    } //I am getting the syntax error on here which reads like this "Syntax error on token "}",{ expected after this token



        try
        {
            serverSocket = new ServerSocket(PORT);
        }
        catch (IOException ioEx)
        {
            System.out.println("\nUnable to set up port!");
            System.exit(1);
        }


        System.out.println("\nServer running...\n");

        do
        {
            client = serverSocket.accept();
            //Wait for client.
            System.out.println("\nNew client accepted.\n");
            handler = new ClientHandler(client);
            handler.start();
        }while (true);
    }


class ClientHandler extends Thread
{
    private Socket client;
    private Scanner input;
    private PrintWriter output;

    public ClientHandler(Socket socket) throws IOException
    {
        client = socket;

        input = new Scanner(client.getInputStream());
        output = new PrintWriter(
                        client.getOutputStream(),true);
    }

    public void run()
    {
        String received;

        do
        {
            received = input.nextLine();
            output.println("ECHO: " + received);
        }while (!received.equals("QUIT"));

        try
        {
            System.out.println("Closing down connection...");
            client.close();
        }
        catch(IOException ioEx)
        {
            System.out.println("* Disconnection problem! *");
        }
    }
}
}
4

5 回答 5

2

你应该正确缩进你的代码,它会帮助你找到这种类型的错误。此代码不在方法内,Java 中不允许这样做:

    try
    {
        serverSocket = new ServerSocket(PORT);
    }
    catch (IOException ioEx)
    {
        System.out.println("\nUnable to set up port!");
        System.exit(1);
    }


    System.out.println("\nServer running...\n");

    do
    {
        client = serverSocket.accept();
        //Wait for client.
        System.out.println("\nNew client accepted.\n");
        handler = new ClientHandler(client);
        handler.start();
    }while (true);
于 2013-02-27T17:14:18.577 回答
0

您有一个不适合任何方法的错误尝试捕获。我不确定你的意思。就在getDateTime方法之后。

于 2013-02-27T17:14:06.973 回答
0

方法之后的代码getDateTime()应该在方法块内。目前它不在任何方法内。将其放在适当的方法块中。

 public void someMethod() {
    try
    {
        serverSocket = new ServerSocket(PORT);
   // rest of your code
于 2013-02-27T17:14:17.653 回答
0

}结束了您的getDateTime方法,之后您有一堆搁浅的代码(如下)需要以某种方式封装在一个块中。

    try
    {
        serverSocket = new ServerSocket(PORT);
    }
    catch (IOException ioEx)
    {
        System.out.println("\nUnable to set up port!");
        System.exit(1);
    }


    System.out.println("\nServer running...\n");

    do
    {
        client = serverSocket.accept();
        //Wait for client.
        System.out.println("\nNew client accepted.\n");
        handler = new ClientHandler(client);
        handler.start();
    } while (true);
于 2013-02-27T17:15:06.620 回答
0

这是什么??我相信,在您的评论之后开始的代码没有任何功能。这是不可能的。你应该把它放在任何函数中。

此外,您可以使用 SimpleDateFormat 将您的日期转换为指定格式的字符串,例如:

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String date = sdf.format(new Date());
于 2013-02-27T17:23:58.993 回答