2016-11-17 88 views
-2

如果這看起來微不足道,請事先道歉。我們最近開始使用Java .net服務器,並且我似乎正在獲取我無法找到的語法錯誤。我已經運行了大約4或5個文本編輯器,包括。 notepad ++,但無論我做什麼Eclipse似乎都會出現語法錯誤。我最初把所有這些都放在單獨的文件中,並且它們在每個主文件中工作得很好。我們的講師希望他們在一個文件中,因爲生活就像那樣有趣。我以這種方式告終,而且我知道當我找到它的位置時,我可能會打破自己的頭,但我似乎無法找到任何語法問題。Java .net服務器語法問題

儘管如此,線48

「令牌語法錯誤 」;

import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.net.InetAddress; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.util.ArrayList; 
import java.util.concurrent.locks.Lock; 
import java.util.concurrent.locks.ReentrantLock; 

/* 
Assignment 7_2016 
Student name: 
Student number: 
*/ 
public class Assignment7_2016 { 
public static void main(String[] args) { 
    //Server Code here =================================== 

    //==================================================== 

    //Code for clients here ============================== 

     // write 15 cars to the system 
    /* write(new Car("96-D-123456", "Nissan", 35000, 20000, true)); 
     write(new Car("99-G-345356", "Toyota", 3000, 28000, true)); 
     write(new Car("00-D-1456", "Ford", 40000, 10000, true)); 
     write(new Car("05-C-846556", "Hyundai", 65000, 20000, true)); 
     write(new Car("08-D-156", "Ferrari", 555000, 10000, true)); 
     write(new Car("10-D-9478", "Lambourghini", 95000, 20000, false)); 
     write(new Car("01-D-9999", "Maserati", 65000, 70000, true)); 
     write(new Car("06-WW-14656", "Alfa Romeo", 75000, 60000, true)); 
     write(new Car("98-D-1256", "Perodua", 37000, 4000, true)); 
     write(new Car("14-D-15576", "Porsche", 635000, 0, true)); 
     write(new Car("98-G-125656", "Saturn", 35000, 20000, true)); 
     write(new Car("08-D-113456", "Mini", 35000, 20000, true)); 
     write(new Car("99-D-125356", "Skoda", 35000, 20000, true)); 
     write(new Car("01-G-15656", "Lotus", 35000, 20000, true)); 
     write(new Car("92-D-125346", "Volvo", 35000, 20000, true)); */ 

    //==================================================== 

} 
} 

class UpDownCarServer{ 
    final static int portCar = 1235; // any number > 1024 
    Data data = new Data(); 
    try{ 
     ServerSocket servesock = new ServerSocket(portCar); 
     while (true) { 
     // wait for a service request on port portSqrt 
     Socket socket = servesock.accept(); 
     // start thread to service request 
     new CarUpDown(socket,data).start(); 
     } 
    }catch (IOException e) {e.printStackTrace();} 

} 

class Data{ 
    private ArrayList<Car> data = new ArrayList<Car>(); 
    private double totalvalue = 0; 
    private Lock lock = new ReentrantLock(); 

    void add(Car c){ 
     lock.lock(); 
     try{ 
      data.add(c); 
      // Add car value to total 
      totalvalue += c.price(); 
     }finally{lock.unlock();} 
    } 

    boolean search(Car c){ 
     lock.lock(); 
     try{ 
      return data.contains(c); 
     }finally{lock.unlock();} 
    } 

    ArrayList<Car> retrieve(String registration){ 
     lock.lock(); 
     try{ 
      ArrayList<Car> dt = new ArrayList<Car>(); 
      Car c = new Car("",registration,0, 0, false); //use for search 
      for(int j = 0; j < data.size();j++){ 
      Car c1 = data.get(j); 
       if(c1.equals(c)) dt.add(c1); 
      } 
      return dt; 
     }finally{lock.unlock();} 
    } 

    void sell(Car c){ 
     lock.lock(); 
     try{ 
     if(c.forSale()){ 
      this.totalvalue -= c.price(); 
      data.remove(c); 
     } 
     else{ System.out.println("Car not for Sale"); } 
     }finally{lock.unlock();} 
    } 
} 

class CarUpDown extends Thread{ 
     Socket socket; 
     Data data; 
     CarUpDown(Socket s, Data d){socket = s; data = d;} 
     public void run() { 
     try{ 
      DataInputStream in = new DataInputStream(socket.getInputStream()); 
      DataOutputStream out = new DataOutputStream(socket.getOutputStream()); 
      int opt = in.readInt(); 
      if(opt == 0){ //upload 
      Car c = new Car(); 
      c.readInputStream(in); 
      data.add(c); 
      out.writeBoolean(true); 
      socket.close(); 
      } 
      else{ //download 
      String registration = in.readUTF(); 
      ArrayList<Car> lst = data.retrieve(registration); 
      out.writeInt(lst.size()); 
      for(int j = 0; j < lst.size();j++){ 
       Car c = lst.get(j); 
       c.writeOutputStream(out); 
      } 
      socket.close(); 
      } 
     } 
     catch (IOException e){} 
    } 
} 

class CarUploadClient { 
    final static int portCar = 1235; 
void write(Car c){ 
    try{ 
    Socket socket; 
    socket = new Socket(InetAddress.getLocalHost(),portCar); 
    DataInputStream in = new DataInputStream(socket.getInputStream()); 
    DataOutputStream out = new DataOutputStream(socket.getOutputStream()); 
    out.writeInt(0); //send upload option 
    c.writeOutputStream(out); 
    boolean ok = in.readBoolean(); 
    if(!ok)System.out.println("Error"); 
    socket.close(); 
    } catch (IOException e) {System.out.println(e);} 
} 
} 

final class Car { 
    private String registration; 
    private String make; 
    private double price; 
    private int mileage; 
    private boolean forSale; 

    public Car(String regn, String m, double pr, int mil, boolean s){ 
    registration = regn; make = m; price = pr; mileage = mil; forSale = s; 
    } 
    public Car(){ 
    registration = null; make = null; price = 0; mileage = 0; forSale = false; 
    } 
    public String registration(){return registration;} 
    public String make(){return make;} 
    public double price(){return price;} 
    public int mileage(){return mileage;} 
    public boolean forSale(){return forSale;} 
    public String toString(){ 
    return " : "+registration+", "+make+", "+price+", "+mileage+", "+forSale+" : "; 
    } 
    public boolean equals(Object ob){ //equality based on registration 
    if(!(ob instanceof Car)) return false; 
    Car c = (Car)ob; 
    return registration.equals(c.registration); 
    } 
    //====================================================== 
    //Methods used to read and write to streams over sockets 
    public void writeOutputStream(DataOutputStream out){ 
    try{ 
    out.writeUTF(registration); 
    out.writeUTF(make); 
    out.writeDouble(price); 
    out.writeInt(mileage); 
    out.writeBoolean(forSale); 
    } 
    catch(IOException e){e.printStackTrace();} 
    } 
    public void readInputStream(DataInputStream in){ 
    try{ 
     registration = in.readUTF(); 
     make = in.readUTF(); 
     price = in.readInt(); 
     mileage = in.readInt(); 
     forSale = in.readBoolean(); 
    } 
    catch(IOException e){e.printStackTrace();} 
    } 
} 

class CarRetrieveClient { 
     final static int portCar = 1235; 
     try{ 
     Socket socket; 
     socket = new Socket(InetAddress.getLocalHost(),portCar); 
      DataInputStream in = new DataInputStream(socket.getInputStream()); 
     DataOutputStream out = new DataOutputStream(socket.getOutputStream()); 
     out.writeInt(1); // send download option 
     int k = in.readInt(); //retrieve number of matches 
     if(k == 0){ 
      System.out.println("No matches found");} 
     else{ 
      Car c = new Car(); 
      for(int j = 0; j < k; j++){ 
      c.readInputStream(in); 
      System.out.println(c); 
      } 
      socket.close(); 
     } 
     } catch (IOException e) {System.out.println(e);} 
} 

回答

1
class UpDownCarServer{ 
    final static int portCar = 1235; // any number > 1024 
    Data data = new Data(); 
    try{ 

不能有可執行代碼(從try)的外側「,{這個令牌之後預期」方法。

+0

謝謝你,真的很難過。這是我們從他那裏得到的代碼,而我現在只是沒有精神上的堅毅去發現這些東西,但我有一百萬件事要做。真誠感謝。 – I2obiN