【问题标题】:Parse string with delimiter ";" (java)使用分隔符“;”解析字符串(爪哇)
【发布时间】:2011-11-25 14:33:45
【问题描述】:

我有一个字符串 = "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”。如何解决?

tokens, tempData, windData 是 String[]

【问题讨论】:

  • 您报告的错误不是由您发布的代码产生的。
  • 你确定这就是你所做的一切吗?我对代码进行了排序,没有错误。此外,您似乎在尝试解析数字时遇到了该异常,而这不在您提供的代码中
  • 你应该发布一个更大的 sn-p as,除了 \\;问题,我们还在猜测中

标签: java string parsing


【解决方案1】:

除了别人说的,我敢打赌你会这样做

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

你得到

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

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

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

【讨论】:

  • 是的!非常感谢。现在它的解析就像它应该的那样。
  • 这只是猜测。正如 Mark Ingram 所说,您可能希望使用 \\s 而不是 " " 来拆分标记。
【解决方案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 个值,最后一个是 ''。尝试将其解析为数字会给您带来异常。

【讨论】:

  • 我在回答中添加了有关异常原因的更多信息。
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    您可以为此使用字符串标记器。

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

    【讨论】:

      【解决方案5】:

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

      【讨论】:

      • 无效的转义序列(有效的是 \b \t \n \f \r \" \' \\ )
      • 试试 \\s. \ 必须在 java 字符串中转义
      猜你喜欢
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      • 1970-01-01
      • 2012-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多