Java wyszukiwanie elementu w tablicy na serwerze

1

Mam problem z wyszukiwaniem elementów w tablicy na serwerze (czyli tablica Auta). Mam trudności z przerobieniem kodu który znajduje się obok gwiazdek.

import java.io.*;
import java.net.*;
public class EchoServer {
	
	public static final int PORT=7;
	
	//String tablica[] = {Audi,Mercedes,Fiat};
	public final static String[] Auta={"Mercedes","Audi","Fiat"};

	
	public static void main(String[] args) throws IOException {
		ServerSocket gniazdoSerwera = new ServerSocket(PORT);
		System.out.println("Uruchomiono serwer: " + gniazdoSerwera);
		
		try{
			System.out.println("Oczekiwanie na polaczenie...");
			Socket gniazdo = gniazdoSerwera.accept();
			BufferedReader in = null;
			PrintWriter out = null;
		
			try{
				in = new BufferedReader (
						new InputStreamReader(gniazdo.getInputStream()));
				out = new PrintWriter(new BufferedWriter(
						new OutputStreamWriter(gniazdo.getOutputStream())), true);
				System.out.println("Otwarto polaczenie: " + gniazdo);
		
				
				while(true){
					
					
					
					String tekst = in.readLine();
					if(tekst.equals("END"))
						break;
					System.out.println(">>: " + tekst);
					out.println(tekst);
					
					//************** Nie mogę tego przerobić
					//public static void main(String[] args) {
						int n, x;
						
						//pobierz dane od uzytkownika
					//	System.out.println("Podaj liczbe elementow tablicy");
					//	n = Console.readInt("");
					//	a = new int[n];
						for (int i=0; i<n; i++) {
							System.out.println("Podaj element a[" + i +"]");
							Auta[i] = Console.readint("");
						}
						System.out.println("Podaj element do wyszukania");
						x = Console.readint("");
						
						//szukaj elementu x
						for (int i=0; i<n; i++) {
							if (a[i] == x) {
								//podaj wynik
								System.out.println("Odnaleziono element "+x+" pod indeksem "+i);
								//zakoncz program
								return;
							}
						}
						
						//podaj wynik
						System.out.println("Nie odnaleziono elementu "+x);
						//*********************
				}
			} finally{
				System.out.println("Zamkanie...");
				try{
					if(in!=null) in.close();
					if(out!=null) out.close();
				}catch (Exception e){}
				gniazdo.close();
			}
		}finally{
			gniazdoSerwera.close();
		}
	}
}

// 2 program

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

public class EchoClient {
	public static void main(String[] args) {
		BufferedReader in = null;
		PrintWriter out = null;
		try{
			BufferedReader klawiatura = new BufferedReader(
					new InputStreamReader(System.in));
			
			Socket gniazdo = new Socket("localhost", 7);
			in = new BufferedReader(
					new InputStreamReader(gniazdo.getInputStream()));
			out=new PrintWriter(gniazdo.getOutputStream());
					System.out.println("Nawiązano połączenie z " 
							+gniazdo.getInetAddress());
					
					while (true){
						System.out.print(">");
						String tekst = klawiatura.readLine();
						   out.println(tekst);
						out.flush();
						if(tekst.equals("END"))
							break;
						System.out.println ("echo: " + in.readLine());
					}
		} catch (IOException e){
			System.err.println(e);
		} finally{
			try{
				if (in != null) in.close();
				if(out != null) out.close();
			}catch (IOException e){}
	}
  }
}
0

Moze lepiej napisz, co ten program mial robic, bo niestety nie wynika to zbytnio z kodu. Poczytaj tez o klasie Scanner oraz ewentualnie o Integer.parseInt().

0

Więc:
Mamy klienta oraz serwera. Klient pyta się o marki samochodów (czyli wpisuje daną markę) a serwer sprawdza czy ma to w tablicy. Jeżeli dana marka znajduję się w tablicy to pojawia się odpowiedni komunikat w przeciwnym wypadku również pojawia się komunikat że danego samochodu brak na serwerze (instrukcje warunkowe)

1 użytkowników online, w tym zalogowanych: 0, gości: 1