#about Scanner
41 messages · Page 1 of 1 (latest)
Hey, @near cape!
Please remember to /close this post once your question has been answered!
which way is the best way to use the Scanner in JAVA?? using try or public static?? forget the spanish sentences I just want to know how to use the Scanner
Any object should be scoped to it's part in the program, method, class, etc.
Line 19 should not be used, the Scanner should be declared on line 17
declared like this? --> Scanner teclado = new Scanner(System.in) without try(){} ??
yes
On line 27 do: teclado.close();
no more error?
okay now imagine that I want to repeat this program 5 times without clicking the running buttom
use a for loop
nope but, let me show you
can I do it with for ?
yes, 100%
for(int i = 0; i < 4; i++) {
//code here
}
Make sure to declare Scanner, and .close() it outside the loop
that's my real problem, I can't use teclado.close(); because I want to loop the program 5 times
okay my bad
let me fix
lol
Scanner teclado = new Scanner(System.in);
byte edad;
String respuesta;
for(int i = 0; i < 4; i++) {
//code here
}
teclado.close();
okay It works, so this is the best way to use the Scanner in JAVA??
before you go let me show you something
in this instance, yes
because some times it's better using the public static or try(){} right?
you should need to rarely use try
unless you're dealing with files or fetching, etc.
I was using try in cousin numbers program look
/*
* 2.- Un número es primo si solo tiene dos divisores: el 1 y el propio número.
* Implementa un programa Java que pida por teclado 5 números. Para cada uno de ellos:
*
* - Comprueba si es negativo. En caso afirmativo, muestra el mensaje por pantalla "El número es negativo".
* - Si es positivo, deberá mostrar por pantalla si es primo o no.
* - Procesados los 5 números, el programa finaliza.
*/
import java.util.Scanner;
/**
*
* @author Francisco José Rodríguez Afonso
*/
public class PROG04_Ejerc2 {
public static void main(String[] args) {
Scanner teclado = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
int num;
int detectar = 0;
System.out.print("\nIntroduce un número: ");
num = teclado.nextInt();
if (num < 0) {
System.out.println("El número es negativo.");
} else {
for (int j = 1; j <= num; j++) {
if (num % j == 0) detectar++;
}
if (detectar == 2) {
System.out.println("El número " + num + " es primo.");
} else {
System.out.println("El número " + num + " no es primo.");
}
}
}
teclado.close();
System.out.println("\nPrograma finalizado.\n");
}
}
/*
* 2.- Un número es primo si solo tiene dos divisores: el 1 y el propio número.
* Implementa un programa Java que pida por teclado 5 números. Para cada uno de ellos:
*
* - Comprueba si es negativo. En caso afirmativo, muestra el mensaje por pantalla "El número es negativo".
* - Si es positivo, deberá mostrar por pantalla si es primo o no.
* - Procesados los 5 números, el programa finaliza.
*/
import java.util.Scanner;
/**
*
* @author Francisco José Rodríguez Afonso
*/
public class PROG04_Ejerc2 {
public static void main(String[] args) {
try (Scanner teclado = new Scanner(System.in)) {
for (int i = 0; i < 5; i++) {
int num;
int detectar = 0;
System.out.print("\nIntroduce un número: ");
num = teclado.nextInt();
if (num < 0) {
System.out.println("El número es negativo.");
} else {
for (int j = 1; j <= num; j++) {
if (num % j == 0) detectar++;
}
if (detectar == 2) {
System.out.println("El número " + num + " es primo.");
} else {
System.out.println("El número " + num + " no es primo.");
}
}
}
}
System.out.println("\nPrograma finalizado.\n");
}
}
is better to use the .close() for the moment with those programs right?
thank you very much for your time, I really needed an answer for this and you gave me one, now I understand this better, I'm new in programming and I have a lot of things to learn, thank you 
of course! my pleasure 🙂