【问题标题】:String split with parseInt in Java在 Java 中使用 parseInt 拆分字符串
【发布时间】:2013-05-22 00:50:17
【问题描述】:

我们被要求为 Stopwatch 构建一个构造函数,该构造函数接受格式为“##:##:###”的字符串并相应地更新分钟、秒和毫秒(私有实例变量)。例如,"1:21:300" 表示 1 分 21 秒 300 毫秒。

所以我尝试使用string.split()parseInt 配对来更新值。但是,该程序不会编译。根据 eclipse,我的构造函数具有正确的语法,但是我正在做的事情有问题。我从未真正使用过splitparseInt,所以我可能100% 错误地使用了这些。谢谢。

    public StopWatch(String startTime){
    String [] timeArray = startTime.split(":");

    if(timeArray.length == 2){
        this.minutes = Integer.parseInt(timeArray[0]);
        this.seconds = Integer.parseInt(timeArray[1]);
        this.milliseconds = Integer.parseInt(timeArray[2]);
    }
    else if(timeArray.length == 1){
        this.minutes =  0;
        this.seconds = Integer.parseInt(timeArray[1]);
        this.milliseconds =    Integer.parseInt(timeArray[2]);              
    }
    else if(timeArray.length == 0){
        this.minutes = 0;
        this.seconds = 0;
        this.milliseconds = Integer.parseInt(timeArray[2]);             
    }
    else{
        this.minutes = 0;
        this.seconds = 0;
        this.milliseconds = 0;
    }
}

附: Junit 测试在尝试执行操作时显示“ComparisonFailue:预期为 0:00:000 但为 20:10:008”:

s = new StopWatch("20:10:8");
assertEquals(s.toString(),"20:10:008");

【问题讨论】:

  • 你的 StopWatch.toString() 是什么?如果你没有定义一个,你就是从 Object 继承一个,它不会做你想要的。
  • 或者如果你有一个,我认为你可能对前导 0 有问题

标签: java split parseint


【解决方案1】:

正如其他答案中提到的,每个长度都减少了 1,但是您在 if 块中使用的索引也关闭了;例如。如果长度为 1,则唯一可用的索引为 0,如果长度为 2,则可用的索引为 0 和 1。

因此你得到一个如下所示的构造函数:

class StopWatch {
    int minutes;
    int seconds;
    int milliseconds;

    public StopWatch(String startTime) {
        String[] timeArray = startTime.split(":");

        if (timeArray.length == 3) {
            this.minutes = Integer.parseInt(timeArray[0]);
            this.seconds = Integer.parseInt(timeArray[1]);
            this.milliseconds = Integer.parseInt(timeArray[2]);
        } else if (timeArray.length == 2) {
            this.minutes = 0;
            this.seconds = Integer.parseInt(timeArray[0]);
            this.milliseconds = Integer.parseInt(timeArray[1]);
        } else if (timeArray.length == 1) {
            this.minutes = 0;
            this.seconds = 0;
            this.milliseconds = Integer.parseInt(timeArray[0]);
        } else {
            this.minutes = 0;
            this.seconds = 0;
            this.milliseconds = 0;
        }
    }
}

【讨论】:

  • 非常感谢!快速而有用的答案。
  • @user2407581 - 我们有点希望提供足够的提示足以让您走上正轨。
【解决方案2】:

将您的 toString() 方法替换为:

public String toString() {
    String paddedMinutes = String.format("%02d", this.minutes);
    String paddedSeconds = String.format("%02d", this.seconds);
    String paddedMilliseconds = String.format("%03d", this.milliseconds);
    return paddedMinutes + ":" + paddedSeconds + ":" + paddedMilliseconds;
}

【讨论】:

  • 我不确定 %02d 是什么。这只是我编程的第二个学期(我在两个学期之间转到了另一所大学,这是一个很大的错误)。不过我会进一步研究,谢谢你的回复。
  • 这是个好建议,但问题在给定的代码中已经很明显了。
  • @user2407581 %..d 将替换为给定数字 (d) 值的字符串表示形式。 2 表示最多两位数,0 告诉处理器该值应该用零填充到最大大小(所以你总是得到两位数)。
  • 当然是return String.format("%02d:%02d:%03d", minutes, seconds, milliseconds);
【解决方案3】:

虽然 Java 数组是从零开始的,但它们的长度只是计算元素的数量。

所以,{1,2,3}.length 将返回 3

现在编写您的代码后,您将得到ArrayOutOfBounds 左右的异常。

【讨论】:

    【解决方案4】:
    if(timeArray.length == 2){
    

    应该是:

    if(timeArray.length == 3){
    

    等等。

    20:10:8 拆分为 : 会给你一个长度 3 ;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-03
      • 2015-03-30
      • 2012-03-21
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多