尝试使用 parseInt() 和 trim() 时的 NumberFormatException 没有修复它

NumberFormatException when trying to use parseInt()and trim() does not fix it

您好,我正在尝试解决 CodeWars 中的 Kata(编码练习),称为“请按您的顺序”(我的代码很可能无法解决它,但我真的只是想摆脱错误..最后有一个link练习,以防你想检查它)

无论哪种方式,Kata 基本上都会为您提供一个字符串,例如

"4of Fo1r pe6ople g3ood th5e the2" 

并且您必须通过获取 int 并以正确的顺序返回来对单词进行排序,所以

"Fo1r the2 g3ood 4of th5e pe6ople"

现在我编写的代码应该遍历每个元素并获取数字然后对其进行排序,所以我尝试使用 parseInt 但它没有用。我在另一篇文章中读到 trim() 会摆脱...

java.lang.NumberFormatException: For input string: "4of" //trim did not fix it

我不确定我是否没有正确实施 trim() 或 parseInt() 或有什么问题,非常感谢任何帮助,感谢您花时间阅读本文。事不宜迟,这里是代码。

public class Kata {
public static String order(String words) {
    String[] unordered = words.split(" ");
    String[] order = new String[unordered.length];
    System.out.println(unordered.length);
    
    for(int i = 0; i < unordered.length; i++){
        int correctIndex = (Integer.parseInt(unordered[i].trim())) -1;
        order[correctIndex] = unordered[i];
        System.out.println(unordered[i]);
    }
    
    return "I will return order concatenated";
  }

  public static void main(String[] args) {
      System.out.println(order("4of Fo1r pe6ople g3ood th5e the2"));
  }

}

错误...(6 是它之前的输出)

6
Exception in thread "main" java.lang.NumberFormatException: For input string: "4of"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.base/java.lang.Integer.parseInt(Integer.java:652)
    at java.base/java.lang.Integer.parseInt(Integer.java:770)
    at Kata.order(Kata.java:8)
    at Kata.main(Kata.java:17)

https://www.codewars.com/kata/55c45be3b2079eccff00010f/train/java (link 到卡塔)

只需删除所有非数字字符(通过使用正则表达式替换),然后将结果值解析为整数。

for (int i = 0; i < unordered.length; i++){
    String wordNum = unordered[i].trim().replaceAll("\D+", "");
    int correctIndex = (Integer.parseInt(wordNum)) - 1;
    order[correctIndex] = unordered[i];
}

4of 不是整数字符串,因此您不能将其解析为 int。您可以将其非数字字符 (\D) 替换为 "",然后您可以将其解析为 int。从 java.util.regex.Pattern.

的文档中了解有关正则表达式模式的更多信息

问题可以通过以下简单步骤解决:

  1. 拆分 space 上的句子(您已经完成了)。
  2. 创建一个 int[] original 并用结果 String[] unordered.
  3. 中的嵌入数值填充它
  4. 创建 original[] 的克隆并对其进行排序。假设这个克隆是 int[] order.
  5. 根据order[]填充一个String[] ordered
  6. 在 space 上加入 ordered[] 的元素。

演示:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String words = "4of Fo1r pe6ople g3ood th5e the2";
        String[] unordered = words.split(" ");
        String[] ordered = new String[unordered.length];
        int[] original = new int[unordered.length];

        // Populate order with embedded numeric values
        for (int i = 0; i < unordered.length; i++) {
            original[i] = Integer.parseInt(unordered[i].replaceAll("\D", ""));
        }

        // Create a clone of original[] and sort it
        int[] order = original.clone();
        Arrays.sort(order);

        // Populate ordered[] based on order[]
        for (int i = 0; i < order.length; i++) {
            for (int j = 0; j < original.length; j++) {
                if (order[i] == original[j]) {
                    ordered[i] = unordered[j];
                    break;
                }
            }
        }

        // Join the elements of ordered[] on space
        String result = String.join(" ", ordered);

        System.out.println(result);
    }
}

输出:

Fo1r the2 g3ood 4of th5e pe6ople

时抛出 NumberFormatException
  1. 提供的输入字符串可能为空。示例-

     Integer.parseInt(null);
    
  2. 输入的字符串可能为空。示例-

     Integer.parseInt("");
    
  3. 输入字符串可能有尾随 space。示例-

     Integer.parseInt("123 ");
    
  4. 输入字符串可能有前导 space。示例-

     Integer.parseInt(" 123");
    

输入的字符串可能是alphanumeric.Example-

    Long.parseLong("b2");

还有其他原因。你试图在 parseInt 中传递 unordered[i]。

int correctIndex = (Integer.parseInt(unordered[i].trim())) -1;

它是一个字母数字字符串。所以编译器给出 NumberFormatException.

尝试使用此函数来计算索引。

    //Method to find the correctIndex
    static int findIndex(String s)
    {
        char ch;
   
        //Access all the characters of the String and the find the digit
        for (int i = 0;i < s.length();i++)
        {
            ch = s.charAt(i);
        
            if (Character.isDigit(ch))
            {
                return ch-49;     //Convert the character to index
            }
        }
    
        return -1;
     }