Java中解析负数的问题

Java中解析负数的问题,java,parsing,split,Java,Parsing,Split,我有我已经解析过的字符串,我可以拆分所有必要的项。我可以接受以下值: “|你在一间大而黑的房间|假1-1-12”, “|你在一个黑暗的大房间|假-10-1-1”, “|你在一个大房间里,非常黑暗|假0 3 0”, “你在一个黑暗的房间里,非常小|真的” 我可以得到描述,真/假值,以及所有分开的数字。但是,当我试图将数字解析为整数值时,会出现数字异常错误 我最初尝试使用整数roomValue和扫描程序roomString将nextint()转换为roomValue。结果惨败。下面的代码将所有内容拆

我有我已经解析过的字符串,我可以拆分所有必要的项。我可以接受以下值: “|你在一间大而黑的房间|假1-1-12”, “|你在一个黑暗的大房间|假-10-1-1”, “|你在一个大房间里,非常黑暗|假0 3 0”, “你在一个黑暗的房间里,非常小|真的” 我可以得到描述,真/假值,以及所有分开的数字。但是,当我试图将数字解析为整数值时,会出现数字异常错误

我最初尝试使用整数roomValue和扫描程序roomString将nextint()转换为roomValue。结果惨败。下面的代码将所有内容拆分,但一旦我尝试分配字符串数组的整数值,就会出现错误

        description = fullDesc.substring(1, fullDesc.indexOf("| "));
        gameRooms[roomNumber] = new Room(description);
        fullDesc = fullDesc.substring(fullDesc.indexOf("| ") + 2);

        if (fullDesc.contains("true")) 
            gameRooms[roomNumber].putGrail();
        fullDesc = fullDesc.substring(fullDesc.indexOf(" "));       
        String[] roomString = fullDesc.split(" ");
        Integer[] adjRoomNum = new Integer[roomString.length];
        for (int i = 0; i < roomString.length; i++) {
            //adjRoomNum[i] = Integer.parseInt(roomString[i]);
            //System.out.println(adjRoomNum[i]);
            System.out.println((roomString[i]));
        }

        /*
        roomValue = roomString.nextInt();
        if ((roomValue) >= 0)
            gameRooms[roomNumber].setAdjacent(0, gameRooms[Integer.valueOf(roomValue)]);
        else if ((roomValue) == -1)
            gameRooms[roomNumber].setAdjacent(0, null);
        roomString  
        roomValue = roomString.nextInt();
        if ((roomValue) >= 0)
            gameRooms[roomNumber].setAdjacent(1, gameRooms[Integer.valueOf(roomValue)]);
        else if ((roomValue) == -1)
            gameRooms[roomNumber].setAdjacent(1, null);
        roomValue = roomString.nextInt();
        if ((roomValue) >= 0)
            gameRooms[roomNumber].setAdjacent(2, gameRooms[Integer.valueOf(roomValue)]);
        else if ((roomValue) == -1)
            gameRooms[roomNumber].setAdjacent(2, null);
        roomValue = roomString.nextInt();
        if ((roomValue) >= 0)
            gameRooms[roomNumber].setAdjacent(3, gameRooms[Integer.valueOf(roomValue)]);
        else if ((roomValue) == -1)
            gameRooms[roomNumber].setAdjacent(3, null);
字符串的输出是


1
-1
-1
2

-1
0
-1
-1

0
0
3
0

0
0
0
0
我想要的是

1
-1
-1
2
-1
0
-1
-1
0
0
3
0
0
0
0
0

我看到有一个空格被读入字符串数组的第一个值,并将解析抛出数字格式异常。我试图删除空白,但没有成功。

如果您的roomString数组包含“1”或“-1”等字符串,则可以执行以下操作

for (int i = 0; i < roomString.length; i++) {
  if(roomString[i].charAt(0) == '-'){
    roomString[i] = roomString[i].substring(1);
    adjRoomNum[i] = Integer.parseInt(roomString[i]) * (-1);
  }else{
    adjRoomNum[i] = Integer.parseInt(roomString[i]);
  }
}
for(int i=0;i
负号和第一个数字之间有空格, 执行拆分后-String[]roomString=fullDesc.split(“”); 第一个字符串是空字符串 所以它在解析时失败了 你应该避免它,或者移除它,或者分割它

if(fullDesc.startWith("")) {
//substring
}

Integer.parseInt()方法将接受有符号(负)整数值的字符串表示形式,如“-1024”,并将该字符串转换为int。事实上,它也会接受类似“+1024”的内容。不会转换的是空、空字符串(“”),或任何包含一个或多个字母字符(包括空格)的数字字符串。显示的错误消息表示有人试图通过Integer.parseInt()方法传递空字符串(

IMHO在执行转换之前验证字符串是一个好主意,这样可以消除生成异常的担忧,或者至少尝试捕获异常并处理它

使用以下方法验证数字字符串:

for (int i = 0; i < roomString.length; i++) {
    // Trim off an possible leading or trailing whitespaces.
    roomString[i] = roomString[i].trim(); 
    try {
        adjRoomNum[i] = Integer.parseInt(roomString[i]);
    }
    catch (NumberFormatException ex) {
        System.err.println("The value " + roomString[i] + " located at " + 
                           "index " + i + " can not be converted to Integer!");
        continue;  // continue processing the remaining array.
    }

    System.out.println(adjRoomNum[i]);
    //System.out.println((roomString[i]));
}
在本例中,该方法与(RegEx)一起用于验证roomString[i]数组元素中包含的字符串确实是有符号或无符号整数值的字符串表示形式这一事实。如果不是,则会向控制台显示一条错误消息,并继续处理下一个数组元素

for (int i = 0; i < roomString.length; i++) {
    // Trim off an possible leading or trailing whitespaces.
    roomString[i] = roomString[i].trim();
    if (!roomString[i].matches("\\+?\\d+|\\-?\\d+")) {
        System.err.println("The value " + roomString[i] + " located at " + 
                           "index " + i + " can not be converted to Integer!");
        continue;  // continue processing the remaining array.
    }
    adjRoomNum[i] = Integer.parseInt(roomString[i]);
    System.out.println(adjRoomNum[i]);
    //System.out.println((roomString[i]));
}
如何获得空字符串(“”)的开头?

当解析基于空格的字符串时,特别是当用户输入的字符串时,总是有可能在某个地方提供了双空格,或者在您的情况下提供了单前导空格(稍后将详细介绍)。要解决此难题,请在拆分字符串之前,始终使用删除前导空格和尾随空格的方法来修剪字符串

解析和拆分特定字符串:

for (int i = 0; i < roomString.length; i++) {
    // Trim off an possible leading or trailing whitespaces.
    roomString[i] = roomString[i].trim(); 
    try {
        adjRoomNum[i] = Integer.parseInt(roomString[i]);
    }
    catch (NumberFormatException ex) {
        System.err.println("The value " + roomString[i] + " located at " + 
                           "index " + i + " can not be converted to Integer!");
        continue;  // continue processing the remaining array.
    }

    System.out.println(adjRoomNum[i]);
    //System.out.println((roomString[i]));
}
通常,解析工作正常,但您会注意到,当运行此代码行时:

fullDesc = fullDesc.substring(fullDesc.indexOf(" "));
fullDesc变量将用前导和尾随空格保存字符串的整数值,例如:
“1-1-12”
。您不需要这样做,因为这将分为:
,“1”,“-1”,“-1”,“2”
。由于前导空格的原因,roomString数组将包含一个空字符串元素,Integer.parseInt()方法将不会处理那些因此引发NumberFormatException的元素。解决方案很简单,只需将1添加到子字符串索引,或将trim()方法添加到代码行的末尾。将上面的代码行修改为:

fullDesc = fullDesc.substring(fullDesc.indexOf(" ") + 1);

                          O R

// Covers almost all the 'just in case' situations.
fullDesc = fullDesc.substring(fullDesc.indexOf(" ")).trim(); 
但是,这不包括在拆分字符串时可能出现的值间距加倍的情况,例如:

    "1  -1 -1  2"
在这里,我们有一个介于1和-1之间的双空格,再次是介于-1和2之间的双空格。当此字符串基于单个空格(
string[]roomString=“1-1-12.split(“”;
)拆分时,您将得到六个数组元素,其中两个是空字符串(
“1”、“1”、“1”、“2”
)以及整数.parseInt()方法在遇到空字符串时将抛出NumberFormatException

在解决方案中,不要在拆分方法中使用
”。改用
“\\s+”
。这个小正则表达式告诉split()方法在一个或多个
空白处拆分字符串

处理字符串数字时,如果希望使用Integer.parseInt()Double.parseDouble(),或者数字字符串中的空格会造成严重破坏,而不管使用的分隔符是什么。以用户在控制台应用程序中输入的逗号(,)分隔的数字字符串为例:

"1, 2, 3,4,5 ,6 ,7,8 , 9 , 10"
此处有四种不同的分隔情况:

1) 1, 2
2) 3,4
3) 5 ,6
4) , 9 ,
如果要拆分此字符串,请说:

String[] values = "1, 2, 3,4,5 ,6 ,7,8 , 9 , 10".split(",");
您的值数组将包含:

["1", " 2", " 3", "4", "5 ", "6 ", "7", "8 ", " 9 ", " 10"]
当您试图通过Integer.parseInt()方法解析这些元素时,其中包含空格的所有数组元素都将导致NumberFormatException。解决方案,为了覆盖所有的基础,可能会这样分割:

String[] values = "1, 2, 3,4,5 ,6 ,7,8 , 9 , 10".split("\\s{0,},\\s{0,}");
String[] gameStrings = {
    "|You're in a large dark room| false 1 -1 -1 2 ",
    "|You're in a dark large room| false -1 0 -1 -1 ",
    "|You're in a large room, very dark| false 0 0 3 0 ",
    "|You're in a dark room, very small| true 0 0 0 0 "
};

for (int s = 0; s < gameStrings.length; s++) {
    String fullDesc = gameStrings[s];
    String description = fullDesc.substring(1, fullDesc.indexOf("| "));
    gameRooms[roomNumber] = new Room(description);
    fullDesc = fullDesc.substring(fullDesc.indexOf("| ") + 2);

    if (fullDesc.contains("true")) {
        gameRooms[roomNumber].putGrail();
    }
    fullDesc = fullDesc.substring(fullDesc.indexOf(" ")).trim();
    String[] roomString = fullDesc.split("\\s+");
    Integer[] adjRoomNum = new Integer[roomString.length];

    for (int i = 0; i < roomString.length; i++) {
        if (!roomString[i].trim().matches("\\+?\\d+|\\-?\\d+")) {
            System.err.println("The value " + roomString[i] + " located at "
                    + "index " + i + " can not be converted to Integer!");
            continue; // Continue processing array elements.
        }
        adjRoomNum[i] = Integer.parseInt(roomString[i]);
        System.out.println(adjRoomNum[i]);
    }
}
这是什么