我不确定是什么导致我的一种方法出现 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! *");
}
}
}
}