+ 1

How to clean Java console after case is executed, and why my variable "usuario" doesn't keep the value after reching Agenda.main

import java.util.Scanner; import samples.Contactos; public class Agenda { public static void main(String[] args) { // TODO Auto-generated method stub Scanner myscan = new Scanner(System.in); Contactos contactos = new Contactos(); Contactos usuario = new Contactos(); if (usuario.getNombre().equals("Usuario")){ System.out.println("Bienvenido, escriba su nombre"); usuario.setNombre(myscan.nextLine()); } else System.out.println("_______________________________"); System.out.println("**--Agenda de "+ usuario.getNombre() + "--**"); System.out.println(""); System.out.println(" 1. Mostrar agenda de contactos"); System.out.println(" 2. Agregar contacto"); System.out.println(" 3. Buscar contacto"); System.out.println(" 4. Cargar fichero de contactos"); System.out.println(" 5. Contacto por defecto"); System.out.println("_______________________________"); System.out.println("Escriba la opcion deseada del menĂș"); Scanner opciones = new Scanner(System.in); int menu = opciones.nextInt(); switch(menu) { case 1: System.out.println(" Aqui se mostraran los contactos "); break; case 2: System.out.println(" Aqui se mostrara como agregar un contacto"); break; case 3: System.out.println(" Aqui se podrĂĄ buscar un contacto"); break; case 4: System.out.println(" Aqui se podra cargar un fichero con contactos preprogramados"); break; case 5: System.out.println("Hola, el contacto por defecto es:"); System.out.println("Nombre: " + contactos.getNombre()); System.out.println("NĂșmero: " + contactos.getNumero()); System.out.println("Email: " + contactos.getEmail()); break; } Agenda.main(args); } } public class Contactos { private int Numero=111111111; private String Nombre="Usuario"; private String Email="tuemail@dominio.com"; //GETTERS public int getNumero() { return Numero; } public String getNombre() { return Nombre; } public

7th Jul 2017, 6:04 PM
D0vAk1N
D0vAk1N - avatar
3 Answers
+ 4
The reason usuario doesn't keep the same values is because of how you are performing your loop. You are calling the main method. Okay, so what does the main method do? Well it creates usuario. So everything in that variable is reset. Note* Since you are also calling the method, it isn't even really the same variable.* To fix that you can do one of the following: Make usuario a global variable and initialize it outside the main method (I don't recommend this). Or You can use a while true loop to loop the method, right before you take the users input, instead of re-calling the method. (I do recommend this). I think you can (Not 100% sure) clear the console with this: System.out.print("\033[H\033[2J"); System.out.flush(); Obtained from: https://stackoverflow.com/questions/10241217/how-to-clear-console-in-java
7th Jul 2017, 6:16 PM
Rrestoring faith
Rrestoring faith - avatar
0
sorry, i made this program in spanish, i hope that is not a problem
7th Jul 2017, 6:04 PM
D0vAk1N
D0vAk1N - avatar
0
Thank you!
7th Jul 2017, 10:46 PM
D0vAk1N
D0vAk1N - avatar