I2obiN I2obiN - 22 days ago 6
Java Question

Java .net Server syntax issue

Apologies in advance if this seems trivial. We recently started doing Java .net servers and I seem to be getting syntax errors I can't find. I've run this through about 4 or 5 text editors incl. notepad++ but no matter what I do Eclipse seems to be coming up with syntax errors. I did initially have all these in separate files and they worked fine with a main each. Our lecturer wants them in one file because life is fun like that. I'm at my wits end with this, and I know I'm probably going to hit myself over the head when I find where it is but I just can't seem to find any syntax problems.

Still, line 48

"Syntax error on token ";", { expected after this token"

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);}
}

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

You cannot have executable code (starting with try) outside of a method.