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

2

My query is because I want to ask the user to enter by keyboard the student's data, and then use the parameters entered 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);
    }
}

ExerciseModelObjects.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);
    }

}
    
asked by Jorge Gonzalez 13.01.2016 в 21:32
source

3 answers

6

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.

    
answered by 13.01.2016 / 21:43
source
1

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

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

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

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();
   }
}
    
answered by 13.01.2016 в 22:13
1

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);
    
answered by 14.01.2016 в 00:41