numberformatexception不会修复它

0s7z1bwu  于 2021-07-06  发布在  Java
关注(0)|答案(3)|浏览(235)

嗨,我正试图解决一个kata(编码实践练习)在codewars,这是所谓的“你的命令,请”(有一个很大的机会,我的代码不会解决它,但我真的只是试图摆脱错误..并有一个链接到演习在最后的情况下,你想看看它)
不管怎样,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 (kata的链接)

koaltpgm

koaltpgm1#

numberformatexception在
提供的输入字符串可能为空。示例-

Integer.parseInt(null);

输入字符串可能为空。示例-

Integer.parseInt("");

输入字符串可能有尾随空格。示例-

Integer.parseInt("123 ");

输入字符串可能有前导空格。示例-

Integer.parseInt(" 123");

输入字符串可以是字母数字。例如-

Long.parseLong("b2");

还有其他原因。你试图在parseint中传递无序的[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;
     }
lmyy7pcs

lmyy7pcs2#

只需删除所有非数字字符(使用regex替换),然后将结果值解析为整数。

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];
}
olqngx59

olqngx593#

4of 不是整数字符串,因此不能将其解析为int。可以替换其非数字字符( \D )与 "" 然后你可以把它解析成 int . 从的文档中了解有关regex模式的更多信息 java.util.regex.Pattern .
可通过以下简单步骤解决问题:
把句子分成空格(你已经做过了)。
创建 int[] original 并用结果中嵌入的数值填充, String[] unordered .
创建的克隆 original[] 排序相同。假设这个克隆是 int[] order .
填充 String[] ordered 基于 order[] .
加入 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

相关问题