简体   繁体   中英

Parse string with delimiter “;” (java)

I have a string = "1.515 53.11 612.1 95.1 ; 0 0 0 0" I'm tring to parse it via this code:

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+"");
            }
        }
    }
}

Bad code :( But I don't know better way to hold the socket connection :)

I always get "java.text.ParseException: Unparseable number". How to fix it?

tokens, tempData, windData are String[]

Apart from what others said, I bet when you do

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

you get

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

and try to parse the first element as Number. Try to do :

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

You don't need to escape the semicolon. Try just doing:

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

I suspect your parse error is because of the trailing space after the 95.1 in your input string. As it is, your tempData array will have 5 values, the last being ''. Trying to parse that as a number will give you that exception.

Hmm, your code (as posted) does not generate this exception. Secondly, "\\\\;" is redundant, you can write ";"

You could use, string tokenizer for that.

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

Try using the \\s in your split for whitespace; eg tempData = tokens[0].split("\\s"); ... it represents a whitespace character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM