Reputation: 81
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.
Upvotes: 0
Views: 3923
Reputation: 775
A hint:
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)
Keeps the program short
Upvotes: 0
Reputation: 12883
From your code, temp is a String
, and a is an array of char
.
String temp;
char[] a = str.toCharArray();
temp = a[1] + a[2];
First off, you can't concatenate char
variables with the plus operator. It will just convert the values of the characters to integers and then add them together. The example below prints 97.
char a = '0';
char b = '1';
System.out.println(a+b);
So, the result of adding two characters is an int. So a[1]+a[2] yields an int
, and temp is a String
.
Second, Java will not automatically convert an int to a String, so that code isn't legal.
What it looks like you want to do is to a take a substring of str
and then to convert that to an integer with parseInt
.
Good luck.
Upvotes: 0
Reputation: 159864
Given that you already have a
as a String
, you could replace this:
temp = a[1] + a[2];
with
temp = str.substring(1, 3);
Upvotes: 1
Reputation: 1305
your problem is that you are attempting to add two strings together, when the compiler is expecting ints. A simpler solution would be to simply use the substring method of the String class. So your code would look like:
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;
}
Upvotes: 0
Reputation: 1365
I believe you should be able to simply do this:
temp = "" + a[1] + a[2];
If you add the "", it will convert that into a String, so you will not get the mismatch error.
Upvotes: 3
Reputation: 1
Incompatible type regards the type you return from the object is different from the expected type. Debug (step through) and find the type you return from the object to the necessary type.
Upvotes: -1
Reputation: 3652
You have to cast the strings to integers first, to do math with them afterwards.
int a = Integer.parseInt(a[0]);
int b = Integer.parseInt(a[1]);
int temp = a+b;
Upvotes: -1
Reputation: 113
Try this:
temp = String.valueOf(a[1]) + String.valueOf(a[2]);
Dont do any java but sounds like its a string -to - char conversion problem :)
Upvotes: 0