Enter data by keyboard and send it as parameters to the constructor

My query is because I want to ask the user to enter by keyboard the student data, and then use the entered parameters to send them to the student constructor. What is the way to do this?

Student.java

public class Alumno {

    private String nom = "null";
    private String ap = "null";
    private String dni = "null";
    private String tel = "null";

    public Alumno(String nom,String ap, String dni, String tel){
        this.nom = nom;
        this.ap = ap;
        this.dni = dni;
        this.tel = tel;
    }

    public void mostrarAlum(){
        System.out.println("Alumno: "+nom+" Apellido: "+ape+" DNI: "+dni+"  Telefono: "+tel);
    }
}

Object Model exercise.java

package ejerciciomodelarobjetos;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 *
 * @autor jorge
 */
public class EjercicioModelarObjetos {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //TODO code application logic here
        System.out.println("Ingresee los datos correspondientes:");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //System.out.print("Nombre:");
        Alumno alu = new Alumno(nom, ap, dni, tel);
    }

}
 2
Author: Carlos Muñoz, 2016-01-13

3 answers

Java has the Class Scanner :

//Inicializamos el escáner
Scanner scanner = new Scanner(System.in);

//Pedimos los datos del estudiante
System.out.print("Ingresa nombre del alumno: ");
String studentName = scanner.nextLine();
System.out.print("Ingresa apellido del alumno: ");
String studentLastname = scanner.nextLine();
System.out.print("Ingresa dni del alumno: ");
String studentDni = scanner.nextLine();
System.out.print("Ingresa teléfono del alumno: ");
String studentPhone = scanner.nextLine();

//Aquí llamamos al constructor
Alumno alumno = new Alumno(studentName, studentLastname, studentDni, studentPhone);

This is how you ask for data from the keyboard in the terminal.

 6
Author: Omar, 2016-01-13 20:49:00

You have to create a program that asks for the data and once retrieved, invoke the constructor.

How to request data from the user will vary depending on the type of program you use.

The following code is an example of a (simplified) console application.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Program {

 public static void main(String[] args) throws IOException {

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    System.out.print("Introduzca Nombre ");
    String nombre = reader.readLine();
    //... lo mismo para las demás propiedades

    Alumno alum = new Alumno(nombre,apellido, dni, telefono);
    alum.mostrarAlum();
   }
}
 1
Author: Javier Reséndiz, 2016-01-13 21:13:30

You can also use the Class javax.swing.JOptionPane to allow the user to enter the corresponding data. Example:

// Entrada de datos
String nom = JOptionPane.showInputDialog("Nombre:");
String ape = JOptionPane.showInputDialog("Apellido:");
String dni = JOptionPane.showInputDialog("DNI:");
String tel = JOptionPane.showInputDialog("Teléfono:");

// Se crea la instancia de Alumno
Alumno alumno = new Alumno(nom, ape, dni, tel);
 1
Author: Paul Vargas, 2016-01-13 23:41:35