0

I'm trying to split a string into 3 integers but i'm getting the error "int cannot be dereferenced" on dr java please help

code:

public class ValidDate

{
  public int validYear;
  public int validMonth;
  public int validDay;

  public ValidDate(String validDate )throws Exception
  {
  validYear = Integer.parseInt(validDate).split[1];
 validMonth = Integer.parseInt(validDate).split[2];
    validDay = Integer.parseInt(validDate).split[3];
.....}
  • 1
    `Integer.parseInt(validDate)` return an `int`. `int` is not have `split` function. Tips for you: let's use split function of String to split year, month, day => after that parse these string to int – TuyenNTA Jul 16 '17 at 02:37
  • @TuyenNguyen Look at the code again. With *that* syntax, `split` is not a method. It is a *field* of type `int[]` or `Integer[]`. Notice the use of `[]`, not `()`. – Andreas Jul 16 '17 at 02:57
  • If you want to parse a date string, why don't you just use one of the built-in date parsers ([`DateTimeFormatter`](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html) or [`SimpleDateFormat`](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html))? – Andreas Jul 16 '17 at 03:02
  • @Andreas you should notice that the `Integer.parseInt` return an `int` and the `int` or `Integer` is not have any field or method `split`. Also the `int[]` or `Integer[]` did not have field `split` – TuyenNTA Jul 16 '17 at 03:03
  • @TuyenNguyen I know that. I was just pointing out that you're assuming OP was trying to use `split()` method call, when syntax isn't method call syntax. So, your statement that "`int` is not have `split` function", while correct, is not consistent with syntax. Saying "`int` does not have `split` *field*" would be more on point, with the code shown. – Andreas Jul 16 '17 at 03:06

2 Answers2

0

First split the string, then parse them to int. Like this:

public ValidDate(String validDate )throws Exception
{
    validYear = Integer.parseInt(validDate.split(" ")[0]);
    validMonth = Integer.parseInt(validDate.split(" ")[1]);
    validDay = Integer.parseInt(validDate.split(" ")[2]);
}
Joe Lu
  • 433
  • 1
  • 5
  • 13
0

When looking at the documentation for Integer we see that it is very specific about the type of String that can be fed to parseInt().

Also, it seems that you are trying to access some variable on the int returned from Integer.parseInt(validDate) called split which you believe to be an array. However, you are unable to do that because int is a primitive type. This is the source of the error you are seeing.

  • 1) You could split on "\\D+", since you want result to be all digit values. --- 2) Did you mean `dateValues`, not `validDate`, on the last two lines? --- 3) Did you mean `[2]` on the last line? – Andreas Jul 16 '17 at 03:00
  • @Andreas For your points 2 and 3, that is what I meant. I will edit it so that it is correct. As far as your first point goes, that is true, but as I say in the comment, it really should be up to OP to decide on that. –  Jul 16 '17 at 03:04
  • @Andreas I'm just going to remove the code example. In retrospect, OP did not provide enough information for me to give him a meaningful example to look at. –  Jul 16 '17 at 03:09