Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用空格分隔字符串_Java_String_Split - Fatal编程技术网

Java 用空格分隔字符串

Java 用空格分隔字符串,java,string,split,Java,String,Split,我见过各种分割字符串的方法。我在《邮报》上试过这两种方法 我正在尝试读取并拆分下一个字符串:{2b 00} 我发现最常见的情况是将消息拆分为“:”,但在这种情况下,我的消息由空格分隔 尝试两种方法,使用常规的split()函数或StringTokenizer我得到了一个“nullpointerexception”,这是由于空间原因: private String splitReceivedString(String s) { String[] separated = s.split("

我见过各种分割字符串的方法。我在《邮报》上试过这两种方法

我正在尝试读取并拆分下一个字符串:
{2b 00}

我发现最常见的情况是将消息拆分为“:”,但在这种情况下,我的消息由空格分隔

尝试两种方法,使用常规的
split()
函数或
StringTokenizer
我得到了一个“nullpointerexception”,这是由于空间原因:

private String splitReceivedString(String s) {
    String[] separated = s.split(" ");
    return separated[1];
}
我怎样才能得到这种字符串的值

添加了可能存在问题的代码

在检查了您的一些答案后,我意识到问题来自蓝牙输入流。我从中得到了空值。下面是我用来接收消息的代码:

代码与bluetoothChat示例几乎相同。但是它被修改以适应我的程序,所以我可能有什么问题

我有一个MCU,当我向它发送另一个字符串时,它会返回这个字符串
{2b 00}
。我认为这是在
connectedThread
中完成的:

public class ConnectedThread extends Thread {

   public void run() {
    byte[] buffer = new byte[1024];  // buffer store for the stream
    int bytes; // bytes returned from read()

    /**Keep listening to the InputStream until an exception occurs*/
    while (true) {
        try {
            /**Read from the InputStream*/
            bytes = GlobalVar.mmInStream.read(buffer);

            /**Send the obtained bytes to the UI activity*/
            GlobalVar.mHandler.obtainMessage(GlobalVar.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
因此,这会将字符串发送给主活动中的处理函数:

    public final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case GlobalVar.MESSAGE_STATE_CHANGE:
                //The code here is irelevant
            case  GlobalVar.MESSAGE_WRITE:
                byte[] writeBuf = (byte[]) msg.obj;
                /**construct a string from the buffer*/
                String writeMessage = new String(writeBuf);
                GlobalVar.mCommunicationArrayAdapter.add(writeMessage);
                break;
            case  GlobalVar.MESSAGE_READ:
                byte[] readBuf = (byte[]) msg.obj;
                /**construct a string from the valid bytes in the buffer*/
                String readMessage = new String(readBuf);
                GlobalVar.mCommunicationArrayAdapter.add(readMessage);
                GlobalVar.readString = readMessage;                    
                break;
然后,变量
GlobalVar.readString
就是我在split函数中得到的变量:

    private String splitReceivedString (String s) {

        String[] separated = s.split(" ");
        return separated[1];
    }

    receive1 = splitReceivedString (GlobalVar.readString);

所以,问题是它没有正确读取接收到的字符串,我不知道如何修复它。

如果你得到一个
NullPointerException
,这不可能是由于传递给split函数的字符串
造成的,它肯定不是null。无论出于何种原因,作为
splitReceivedString
方法参数的
字符串s
似乎是
null

插入一些输出(System.out.println或其他)进行调试

  • 你的目标是什么

  • 分离的元素有多少

  • 分隔的内容[0](如果只有一个元素)

尝试使用

s.split("\\s");

以任意空格拆分。

我在控制台应用程序中测试了您的代码。我将双引号改为单引号,并且“Split”方法中存在拼写错误


您希望使用正则表达式拆分字符串。 试试看

String splitString = "2b 00 00";
String delim = "[ ]";
//here we split the string wherever we see a character matching
//the inside of our square brackets. In this case a space
String[] splitResults = splitString.split(delim);
我已经测试过了,结果如下

splitResults[0] = "2b"
splitResults[1] = "00"  
splitResults[2] = "00"

我打赌你正在将
null
传递给该方法。@arshajii你是什么意思?我已经阅读了我想用吐司拆分的字符串,并且非常准确
2b 00 00
@arshajii是正确的,我测试了这个方法,它工作得很好。您传入的字符串与
{2b 00}
不同,或者您传入的字符串为null。显然不是这样,因为如果是这样,您不会得到该异常。请使用调试器确保您传入的内容。是的,可能是这样。我正在开发一个基于bluetoothChat exmample的蓝牙应用程序。我插入的字符串是通过蓝牙接收的字符串,因此我从处理程序的变量
readString
获取它。我认为这是正确的,因为在为这个变量干杯时,我在应用程序中看到字符串
2b 00 00
。那么,我能做什么呢?这是因为使用.split(“”)拆分字符串不会返回任何内容,因为“”不是.split方法所需的正确的正则表达式语法。因此空指针。@KBusc:split永远不会返回
null
。也请参见此处:@chiccodoro,在主要问题中添加了解释问题所在的代码。“s”变量是一个字符串,正如我在@chiccodoro从bluetooth inputstreamTM获得的答案中所说,在主要问题中添加了解释问题所在的代码。如果我设置单个cuotes,则会出现此错误:
方法拆分(字符串)类型中的字符串不适用于参数(Char)
,因为“”不是正则表达式syntax@beaumondo,在主要问题中添加了解释问题所在的代码。这样也没什么。我想我之所以这样做是因为蓝牙输入流中的
readString
变量。在主要问题中添加了解释问题所在的代码。
splitResults[0] = "2b"
splitResults[1] = "00"  
splitResults[2] = "00"