Words together if I do not put space

0

Hi, I'm a student who has just started and I'm doing a homework, I have a problem with this code

import java.util.Scanner;

public class Ejercicio11 {


    public static void main(String[] args) {
        Scanner entrada=new Scanner(System.in);
        System.out.println("Introduce tu nombre");
        String nombre_usuario=entrada.nextLine();

        System.out.println("Introduce tu primer apellido");
        String apellido1_usuario=entrada.nextLine();

        System.out.println("Introduce tu segundo apellido");
        String apellido2_usuario=entrada.nextLine();

        System.out.println("tu nombre es: " + nombre_usuario );
        System.out.println("tu primer apellido es: " + apellido1_usuario );
        System.out.println("tu segundo apellido  es: " + apellido2_usuario );

    }

}

The problem is that when I give Start to the console with the Eclipse, when I ask my name I must for example Francisco (and add a space) so that later I do not join with the other data in the other 3 data I must also put the space.

What would be the solution for not having to do that? I suppose it will be some error of my code.

Thanks

    
asked by user100540 19.09.2018 в 12:56
source

1 answer

0

As I understand it, you would need to add spaces to your titles:

public static void main(String[] args) {
   Scanner entrada=new Scanner(System.in);
   System.out.println("Introduce tu nombre : ");
   String nombre_usuario=entrada.nextLine();

   System.out.println("Introduce tu primer apellido : ");
   String apellido1_usuario=entrada.nextLine();

   System.out.println("Introduce tu segundo apellido : ");
   String apellido2_usuario=entrada.nextLine();

   System.out.println("tu nombre es: " + nombre_usuario );
   System.out.println("tu primer apellido es: " + apellido1_usuario );
   System.out.println("tu segundo apellido  es: " + apellido2_usuario );

}

On the other hand I leave you another more updated library with which you can read data from console that was introduced in java 1.6:

Console console = System.console();
if (console == null) {
   System.out.println("Sin consola interactiva!");
   System.exit(0);
}

   System.out.println("Introduce tu nombre : ");
   console.readLine();

   //Aqui ya se introduce el titulo y se solicita ingreso de datos al usuario
   console.readLine("Introduce tu primer apellido : "); 
}

I hope it serves you.

    
answered by 19.09.2018 в 16:09