Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

378
Views
Whitespace split issue, java

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)

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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?

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!