Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

376
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda