#Best practice when using scanner class

1 messages · Page 1 of 1 (latest)

elfin wigeon
#

Hello guys, consider this piece of code:

import java.util.Scanner;
public class Recap {
    public static void main(String[] args) {
        Statistics total = new Statistics();
        System.out.println("Enter numbers:");
        while (true) {
            Scanner sc = new Scanner(System.in);
            int number = sc.nextInt();
            if (number != -1) {
                total.addNumber(number);
            }
            else {
                sc.close();
                break;
            }
        }
        System.out.println(total.sum());
    }
}

I have a question:

When using the Scanner class to take inputs, is it best practice to declare the Scanner object inside the loop itself or outside the loop please (Does that make a difference?)

Talking of best practice, say we write the following class:

public class Statistics {
    private int count;
    private int sum = 0;

    public Statistics() {
        int count = 0;
    }

    public void addNumber(int number) {
        count++;
        sum += number;
    }

    public int getCount() {
        return count;
    }

    public int sum() {
        return sum;
    }

    public double average() {
        return (double) sum / count;
    }
}

say I need to call the sum() method inside another method, like a toString() method. Normally, is it best to prefix the sum() method being called inside the toString() method by the this keyword? or we can just omit it for better readability ? Because behind the scenes, the compiler will implicitly use it anyway

frank spadeBOT
#

<@&987246399047479336> please have a look, thanks.

silent cypress
#

You shouldn't create a new instance every time.

#

There's also the following to consider:

frank spadeBOT
#

Mixing any nextXXX method with nextLine from the Scanner class for user input, will not ask you for input again but instead result in an empty line read by nextLine.

To prevent this, when reading user input, always only use nextLine. If you need an int, do

int value = Integer.parseInt(scanner.nextLine());

instead of using nextInt.

Assume the following:

Scanner scanner = new Scanner(System.in);

System.out.println("Enter your age:");
int age = scanner.nextInt();
System.out.println("Enter your name:");
String name = scanner.nextLine();

System.out.println("Hello " + name + ", you are " + age + " years old");

When executing this code, you will be asked to enter an age, suppose you enter 20.
However, the code will not ask you to actually input a name and the output will be:

Hello , you are 20 years old.

The reason why is that when you hit the enter button, your actual input is

20\n

and not just 20. A call to nextInt will now consume the 20 and leave the newline symbol \n in the internal input buffer of System.in. The call to nextLine will now not lead to a new input, since there is still unread input left in System.in. So it will read the \n, leading to an empty input.

So every user input is not only a number, but a full line. As such, it makes much more sense to also use nextLine(), even if reading just an age. The corrected code which works as intended is:

Scanner scanner = new Scanner(System.in);

System.out.println("Enter your age:");
// Now nextLine, not nextInt anymore
int age = Integer.parseInt(scanner.nextLine());
System.out.println("Enter your name:");
String name = scanner.nextLine();

System.out.println("Hello " + name + ", you are " + age + " years old");

The nextXXX methods, such as nextInt can be useful when reading multi-input from a single line. For example when you enter 20 John in a single line.

elfin wigeon
# frank spade

yeah I see, will stick to the Integer.parseInt/Inter.valueOf

elfin wigeon
proud cave
silent cypress
#

There's no need to create a new instance every time, that also requires some CPU cycles.

#

When you're eating and empty your glass, you don't grab a new one every time but reuse it.

elfin wigeon
#

yep I see, ty !

thick jungle
# elfin wigeon Hello guys, consider this piece of code: ```java import java.util.Scanner; publ...

Normally, is it best to prefix the sum() method being called inside the toString() method by the this keyword? or we can just omit it for better readability ?
If removing it improves readability then remove it, otherwise keep it.
The purpose of this is to differentiate between attributes and temporary variables like function parameters and local variables. Because of the this keyword we can write code like this:

class A {
    int x;

    A(int x) {
        this.x = x;
    }
}

Without the this we would have no way to differentiate between the attribute x and the function parameter x.

elfin wigeon
# silent cypress When you're eating and empty your glass, you don't grab a new one every time but...

hmm I have a question, since it's best practice to declare the scanner only once, is it also best practice to declare something that we know we would be overwritting its value outside the look? (it will be easier with an example, see below):


String name;
Scanner sc = new Scanner(System.in);
while (true) {
  name = sc.nextLine(); // name is declared outside
}


Scanner sc = new Scanner(System.in);
while (true) {
  String name = sc.nextLine(); // name is declared inside
}
frank spadeBOT
silent cypress
#

Not really, since it might potentially never be created.

#

But it depends on the exact scenario.

elfin wigeon
#

so it doesn't really matter where we declare it? The idea is the value collected in name variable would be use to create a person with that specific name

silent cypress
#

It does, it should be as short lived as possible.