0

I'm working on a method that takes a string time, which contains 3 to 4 numbers representing military time, such as "130" or "1245". The purpose of this method is to assign the contents of the string time to an integer array with two parts: [0] is the hour (such as "1" or "12" from my example above), and [1] represents the minutes ("30" or "45" from my example).

The if(str.length() ==3) is meant to catch strings such as "130", where the hour is less than 10 and the string lacks a leading zero.

When I compile, the errors I get read:

error: incompatible types
                          temp = a[1] + a[2];
                                      ^
required: String
found: int

.

error: incompatible types
                          temp = a[0] + a[1];
                                      ^
required: String
found: int

.

error: incompatible types
                          temp = a[2] + a[3];
                                      ^
required: String
found: int

.

I've been reading up on the char data type, and from what I understand it has a numerical value similar to an integer. I've attempted to typecast with:

temp = (String) a[1] + a[2];

But that gives the following error:

error: inconvertible types
                          temp = (String) a[1] + a[2];
                                           ^
required: String
found: char

At this point I'm not sure what to do. Below is my code:

private int[] convertStringToHoursMins(String time) {
    String str = time;
    String temp;
    char[] a = str.toCharArray();
    int[] hoursMins = new int[2];
    try {
        if (str.length() == 3) {
            hoursMins[0] = a[0];
            temp = a[1] + a[2];
            hoursMins[1] = Integer.parseInt(temp);
        }
        else {
            temp = a[0] + a[1];
            hoursMins[0] = Integer.parseInt(temp);
            temp = a[2] + a[3];
            hoursMins[1] = Integer.parseInt(temp);
        }   
    }
    catch(NumberFormatException e) {
        System.out.println("Invalid time.");
    }
    return hoursMins;
}

Thanks in advance for any help you can offer.

4

8 回答 8

3

我相信你应该能够简单地做到这一点:

temp = "" + a[1] + a[2];

如果添加“”,它会将其转换为字符串,因此您不会收到不匹配错误。

于 2012-09-27T00:15:56.527 回答
1

鉴于您已经拥有aas String,您可以替换它:

temp = a[1] + a[2];

temp = str.substring(1, 3);
于 2012-09-27T00:20:59.317 回答
0

尝试这个:

temp = String.valueOf(a[1]) + String.valueOf(a[2]);

不要做任何java,但听起来像是一个字符串到字符的转换问题:)

于 2012-09-27T00:13:06.520 回答
0

从您的代码中, temp 是 a String, a 是char.

String temp;
char[] a = str.toCharArray();

temp = a[1] + a[2];

首先,您不能char使用加号运算符连接变量。它只会将字符的值转换为整数,然后将它们加在一起。下面的示例打印 97。

    char a = '0';
    char b = '1';
    System.out.println(a+b);

因此,添加两个字符的结果是一个 int。所以 a[1]+a[2] 产生 an int,而 temp 是 a String

其次,Java 不会自动将 int 转换为 String,因此该代码是不合法的。

您想要做的是获取一个子字符串,str然后将其转换为一个整数parseInt

祝你好运。

于 2012-09-27T00:26:12.963 回答
0

您的问题是,当编译器需要整数时,您正试图将两个字符串相加。一个更简单的解决方案是简单地使用 String 类的 substring 方法。所以你的代码看起来像:

private int[] convertStringToHoursMins(String time) {
    int[] hoursMins = new int[2];
    try {
        if (time.length() == 3) {
            hoursMins[0] = Integer.parseInt(time.subString(0,0);
            hoursMins[1] = Integer.parseInt(time.subString(1,2);
        }
        else {
            hoursMins[0] = Integer.parseInt(time.subString(0,1);
            hoursMins[1] = Integer.parseInt(time.subString(2,3);
        }   
    }
    catch(NumberFormatException e) {
        System.out.println("Invalid time.");
    }
    return hoursMins;
}
于 2012-09-27T00:17:41.117 回答
0

一个提示:

hoursMins [0] = Integer.parseInt(time.substring(0, time.length()-2));
//Similar line for minutes
//Magic constraint filtering goes here ( no negative numbers, minutes under 60, hours under 24)

保持程序简短

于 2012-09-27T00:32:10.090 回答
-1

不兼容类型是指您从对象返回的类型与预期类型不同。调试(逐步)并找到从对象返回的类型为必要的类型。

于 2012-09-27T00:13:34.787 回答
-1

您必须先将字符串转换为整数,然后再对它们进行数学运算。

int a = Integer.parseInt(a[0]);
int b = Integer.parseInt(a[1]);

int temp = a+b;
于 2012-09-27T00:14:20.603 回答