从Telnet提供的未知字符串中提取4个

时间:2018-05-27 10:28:51

标签: java java-10

我有一个Putty终端,我读了一个字符串,f.ex。如果我在4中打印,我得到一个看起来像这样的字符串( ' 4)有人知道,我怎么能提取这个4?我用模式和匹配器尝试了它。但它仍然无效。

我正在制作带插座的餐厅服务器,用于餐厅。代码有点多,但我希望你不会因为粘贴我的每一堂课而讨厌我。

服务器类:

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提取它,但是有更好的方法吗?

此致 卢卡斯

0 个答案:

没有答案