使用分隔符“;”解析字符串(Java)的

时间:2011-11-25 14:33:46

标签: java string parsing

我有一个字符串=“1.515 53.11 612.1 95.1; 0 0 0 0” 我想通过这段代码解析它:

public class SendThread implements Runnable {
    public void run() 
    {
        socket = null;
        BufferedReader in;


        while (true)
        {
            // Loop until connected to server
            while (socket == null){
                try{
                    socket = new Socket ("192.168.137.1", 808);         
                }
                catch (Exception e) {
                    socket = null;

                    //Log.d("Connection:", "Trying to connect...");
                }
                try {
                Thread.sleep(30);
                } catch (Exception e) {}
            }

            // Get from the server   
            try {

                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                Log.d("Connection: ", "connected");

                String line = null;

                while ((line = in.readLine()) != null) {

                    Log.d("Socket:", line);

                    NumberFormat nf = new DecimalFormat ("990,0");
                    String[] tokens = null;
                    String[] tempData = null;
                    String[] windData = null;

                    try {
                         tokens = line.split(";");
                         tempData = tokens[0].trim().split(" ");
                         windData = tokens[1].trim().split(" ");   
                    } catch (Exception error)
                    {
                         Log.d("Parsing error:", error+"");
                    }



                    for (int i = 0; i < currentTemp.length; i++)
                        currentTemp[i] = (Double) nf.parse(tempData[i]);
                    for (int i = 0; i < currentWind.length; i++)
                        currentWind[i] = (Double) nf.parse(windData[i]);

                    //Toast.makeText(getApplicationContext(), "Received data:", duration)
                    for (int i = 0; i < currentTemp.length; i++){
                        Log.d("Converted data: currentTemp["+i+"] = ", currentTemp[i]+"");
                    }
                    for (int i = 0; i < currentWind.length; i++){
                        Log.d("Converted data: currentWind["+i+"] = ", currentWind[i]+"");
                    }
                }
                socket = null;
                Log.d("Connection: ", "lost.");

            } 
            catch (Exception e) {
                    socket = null;
                    Log.d("Connection: ", "lost.");
                    Log.d("Connection:", e+"");
            }
        }
    }
}

错误代码:(但我不知道更好的方法来保持套接字连接:)

我总是得到“java.text.ParseException:Unparseable number”。如何解决?

标记,tempData,windData是String []

5 个答案:

答案 0 :(得分:3)

除了别人说的话,我打赌你做什么

windData = tokens[1].split(" ");

你得到了

windDate = {"","0","0","0","0"} 

并尝试将第一个元素解析为Number。 尝试做:

try {
     tokens = line.split(";");
     tempData = tokens[0].trim().split(" ");
     windData = tokens[1].trim().split(" ");   
} catch (Exception error)
{
     Log.d("Parsing error:", error+"");
}

答案 1 :(得分:2)

您无需转义分号。试着做:

try {
     tokens = line.split(";");
     tempData = tokens[0].split(" ");
     windData = tokens[1].split(" ");
} catch (Exception error)
{
     Log.d("Parsing error:", error+"");
}

我怀疑你的解析错误是因为输入字符串中95.1之后的尾随空格。实际上,你的tempData数组将有5个值,最后一个是''。试图将其解析为数字会给你这个例外。

答案 2 :(得分:1)

嗯,您的代码(已发布)不会生成此异常。其次,"\\;"是多余的,您可以编写";"

答案 3 :(得分:1)

你可以使用string tokenizer。

    String s  = "1.515 53.11 612.1 95.1 ; 0 0 0 0";

    StringTokenizer tokenizer = new StringTokenizer(s,";");

    while(tokenizer.hasMoreElements()){
        StringTokenizer numberTokenize = new StringTokenizer(tokenizer.nextToken());
        while(numberTokenize.hasMoreElements()) {
            System.out.println(numberTokenize.nextElement());
        }
    }

答案 4 :(得分:0)

尝试在分割中使用\s作为空格;例如tempData = tokens[0].split("\s"); ...它代表一个空白字符。

相关问题