首页 > 解决方案 > 从 Telnet 传递的未知字符串中提取 4 个

问题描述

我有一个腻子终端,我在 f.ex 中读取了一个字符串。如果我在 4 中打印,我会得到一个看起来像这样的字符串 (���� ����'������4) 有人知道吗,我如何提取这个 4?我用 Pattern 和 Matcher 试过了。但它仍然不起作用。

我正在为餐厅制作带套接字的餐厅服务器。代码有点多,但我希望你不会因为粘贴我的每个课程而讨厌我。

服务器类:

package Ue1SpeiseServer;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class RestaurantTrekNation {

/**
 * The Port for the Server
 */
private static Scanner sc = new Scanner(System.in);
private static int PORT;

/**
 * A Arraylist with all Clients
 */
protected static List<Customer> customers = new ArrayList<>();
protected static int[] tables = new int[5];
public static List<Dish> menu = new ArrayList<>();

static {
    menu.add(new Dish(1,19.99, "hello" , 5));
    menu.add(new Dish(2,0.99, "hello" , 5));
    menu.add(new Dish(3,10.99, "hello" , 5));
    menu.add(new Dish(4,13.45, "hello" , 5));
    menu.add(new Dish(5,12.95, "hello" , 5));
    menu.add(new Dish(6,08.00, "hello" , 5));
    menu.add(new Dish(7,09.99, "hello" , 5));
    menu.add(new Dish(8,14.75, "hello" , 5));
    menu.add(new Dish(9,100.01, "hello" , 5));
}
public static void main(String[] args) {
    System.out.print("Insert port-number for the restaurantServer:");
    PORT = sc.nextInt();

    // listen on port 10023
    try (ServerSocket server = new ServerSocket(PORT)) {
        System.out.println("Chat-Server started on port: " + PORT);

        while (true) {
            customers.add(new Customer(server.accept()));
            System.out.println("Customer geadded");
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}


synchronized static boolean summmonCustomer(int table) {
    boolean status = false;
    status = tables[table] == 0;
    return status;
}

synchronized static void removeCustomer(Customer customer) {

}

synchronized static List<Dish> returnDishList(){
    return menu;
}

}

客户类别:

package Ue1SpeiseServer;

import java.io.*;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Customer extends Thread {

/**
 * If this boolean is true the User is able to use extra functions
 */
private boolean restaurantEmployee;

/**
 * The Customer socket
 */
private Socket socket;
/**
 * The BufferedWriter
 */
private BufferedWriter out;
/**
 * the nickname of the client
 */
private String table;

/**
 * The Control Characters before the string
 */
private static final String STZ_DAVOR = new String(new byte[]{0x0b, 0x1b, 
'[', '1', 'A', 0x1b, '7', 0x1b, '[', '1', 'L', '\r'});

/**
 * The Control Characters after the string
 */
private static final String STZ_DANACH = new String(new byte[]{0x1b, '8', 
0x1b, '[', '1', 'B'});

/**
 * The bill for the customer
 */
private static Map<String, Double> bill = new HashMap<>();

/**
 * Creates a ClientThread and starts it
 *
 * @param socket
 */
Customer(Socket socket) {
    this.socket = socket;
    start();
}


/**
 * gets the socket of the customer
 *
 * @return
 */
public Socket getSocket() {
    return socket;
}

/**
 * @param message
 * @throws IOException
 */
private void sendMessageToMe(String message) throws IOException {
    out.write(STZ_DAVOR + message + STZ_DANACH);
    out.flush();
}

/**
 * runs the customerThread
 */
@Override
public void run() {

    try (
            BufferedReader in = new BufferedReader(new 
InputStreamReader(socket.getInputStream(), Charset.forName("utf-8")));
            BufferedWriter out = new BufferedWriter(new 
OutputStreamWriter(socket.getOutputStream(), Charset.forName("utf-8")))
    ) {
        this.out = out;
        System.out.println("Connection accepted from" + 
socket.getRemoteSocketAddress());
        sendMessageToMe("Hello and welcome in the restaurant TrekNation 
\r\n");
        sendMessageToMe("If you want to leave type /leave \r\n");
        sendMessageToMe("To get a list of our dishes type /list \r\n");
        sendMessageToMe("If you have a complaint type /c *dish number* 
*complaint* \r\n");
        sendMessageToMe("Please insert your table number:");

        while (true) {
            table = in.readLine();

            System.out.println(table.substring(table.length()-1));
            int tableNumber = 
Integer.parseInt(table.substring(table.length()-1));
            if (RestaurantTrekNation.summmonCustomer(tableNumber)) {
                sendMessageToMe("Your Table is ready prepare to order ");
                break;
            } else if (in.readLine().equals("") || in.readLine().contains(" 
")) {
                sendMessageToMe("please us a valid table number");
            } else {
                sendMessageToMe("Table is full please choose another     
table");
            }
        }

        while (true) {

            out.write( "Order>");
            out.flush();

            String input = in.readLine();

            if (input == null) {
                break;
            } else {

                if(input.equals("/list")){
                    synchronized (RestaurantTrekNation.customers) {
                        System.out.println("Avaibility  Name   Number");
                        List<Dish> dishes
=RestaurantTrekNation.returnDishList();
                        for (int i = 0; i < dishes.size(); i++) {
                            System.out.println(dishes.get(i));
                        }
                    }
                }
            }
        }
    } catch (IOException ie) {
        RestaurantTrekNation.removeCustomer(this);
        System.out.println("customer gone");
    }
}
} 

菜品类:

package Ue1SpeiseServer;

public class Dish {

private static int catalogNumber;

private static Double costs;

private static String nameofDish;

public static int availabilityOfDish;

public Dish(int number, Double price, String name, int availability) {
    catalogNumber = number;
    costs = price;
    nameofDish = name;
    availabilityOfDish = availability;
}

public static int getCatalogNumber() {
    return catalogNumber;
}

public static Double getCosts() {
    return costs;
}

public static String getNameofDish() {
    return nameofDish;
}

public static int getAvailabilityOfDish() {
    return availabilityOfDish;
}
}

我能够用 Substring 提取它,但有更好的方法吗?

问候,卢卡斯

标签: javajava-10

解决方案


推荐阅读