2013-02-27 90 views
-1

我目前正在练习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! *"); 
     } 
    } 
} 
} 
+0

什么错误信息,你得到什么? – 2013-02-27 17:13:11

+0

} //我在这里得到的语法错误是这样的:“令牌上的语法错误”}“,{在我的一个方法之后,在此令牌 之后预期 – Sky 2013-02-27 17:14:18

回答

0

你有一个错位的尝试抓住超出任何方法。我不确定你的意思。在getDateTime方法之后。

0

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

public void someMethod() { 
    try 
    { 
     serverSocket = new ServerSocket(PORT); 
    // rest of your code 
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); 
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); 
0

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

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

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
String date = sdf.format(new Date()); 
相关问题