#about Scanner

41 messages · Page 1 of 1 (latest)

near cape
#

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

simple joltBOT
#

Hey, @near cape!
Please remember to /close this post once your question has been answered!

near cape
#

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

molten pond
#

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

near cape
#

declared like this? --> Scanner teclado = new Scanner(System.in) without try(){} ??

molten pond
#

yes

near cape
#

VSCode gave me this error, let me screenshot it

molten pond
#

On line 27 do: teclado.close();

near cape
molten pond
#

no more error?

near cape
#

okay now imagine that I want to repeat this program 5 times without clicking the running buttom

molten pond
#

use a for loop

near cape
near cape
molten pond
#

yes, 100%

near cape
#

okay

#

give me a min

molten pond
#
for(int i = 0; i < 4; i++) {
  //code here
}
#

Make sure to declare Scanner, and .close() it outside the loop

near cape
#

that's my real problem, I can't use teclado.close(); because I want to loop the program 5 times

molten pond
#

lol

#
Scanner teclado = new Scanner(System.in);
byte edad;
String respuesta;

for(int i = 0; i < 4; i++) {
  //code here
}

teclado.close();
near cape
#

okay It works, so this is the best way to use the Scanner in JAVA??

#

before you go let me show you something

molten pond
#

in this instance, yes

near cape
molten pond
#

you should need to rarely use try

#

unless you're dealing with files or fetching, etc.

near cape
#

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?

molten pond
#

it's always good practice to .close()

#

there's no need to use try here

near cape
#

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 DukeCheers

molten pond
#

of course! my pleasure 🙂