3

beginner here I have searched anywhere but I can't seem to find any solution or problems related to mine. I've just started learning java and Here is my problem:

I have a working code where the input is split by "/":

Scanner d = new Scanner(System.in);
System.out.println("Enter your birthday (mm/dd/yyyy): ");
String birthday = d.next();

String[] bdayArr = birthday.split("/", 3);

int current = 2022;
int age = current - Integer.parseInt(bdayArr[2]);

now it was just a solution for the meantime as the date format I want to needs to be seperated with spaces (ex, January 21, 1899) and the purpose of me splitting is i need to get the year to use in a code block that computes the age of the person based on the birth year.

However, when i try to split it by whitespace I can't seem to access the array anymore. Error codes ranging from out of bounds to a NumberFormatException when i input (ex: January 21, 1989) (code below)

String[] bdayArr = birthday.split("\\s+", 3);

I don't know what i'm doing wrong, hopefully someone can help. (I know the question is too long and not straight to the point I wanted to provide as much context)

Wazu Wazu
  • 47
  • 5
  • If you split `"January 21, 1989"` by whitespaces you'll end up with `"January"`, `"21,"` and `"1989"`. Does your code handle that dangling comma after the 21? – OH GOD SPIDERS Jan 10 '22 at 14:27
  • I haven't come across exception handling(? sorry really still new to this thing), so in short nope. :< however one thing i may add is even if i input 21 and 1989 it says it is out of bounds for some reason (Index 2 out of bounds for length 1) so im guessing it's not reading the array at all? or it didn't split in the first place? – Wazu Wazu Jan 10 '22 at 14:33
  • Note that in an actual java app you shouldn't be doing any of this. You parse dates using a date parser, such as `DateTimeFormatter`. Don't let that distract you though - you're learning, "not production quality" is kinda the point :) – just be aware that if there's a voice in your head saying: "Huh, this all seems thoroughly complicated and unwieldy, surely there is a much better way to do all this" - yeah, there is. – rzwitserloot Jan 10 '22 at 15:55

1 Answers1

2

The problem is caused by d.next(), you should use d.nextLine() insted. to understand why you should check this quesiton What's the difference between next() and nextLine() methods from Scanner class?

Daniel Botnik
  • 539
  • 1
  • 6
  • 20
  • Thank you for this one! However, one more thing this may sound as a very stupid question but is it okay to use `nextLine` for everything input related? From int inputs to Doubles, or should there be integers or doubles I should use the appropriate ones? – Wazu Wazu Jan 10 '22 at 14:43
  • 1
    I would go with the second option – Daniel Botnik Jan 10 '22 at 14:47
  • @WazuWazu the advice 'only use `nextLine()`' is common, but incorrect. The correct way to use scanner when dealing with keyboard input is to set the delimiter to the enter key: `scanner.useDelimiter("\\R")`. Now, use all `next()` methods, __except__ `nextLine()`, don't use that at all. __ALL__ the `nextX()` methods now read an entire line. Use the next method that matches the input you need. Want an int? `.nextInt()`. Want someone's name? `.next()`. – rzwitserloot Jan 10 '22 at 15:54