Rami
Rami

Reputation: 67

Is it safe and a better practice to accept Strings instead of integers or doubles?

Is the second part a good practice in order to avoid user input mismatch?

public class StringsAsPrimitiveDate{
Scanner input= new Scanner(System.in);
System.out.println("Please enter your number:");
int numberOne=input.nextInt();
}

Or

public class StringsAsPrimitiveDate{
Scanner input= new Scanner(System.in);
System.out.println("Please enter your number:");
String numberOne=input.next();
int result = Integer.parseInt(numberOne);   
}

Thank you

Upvotes: 0

Views: 116

Answers (1)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79085

A common problem with nextInt is that it leaves behind a new line character which may be accidentally automatically consumed by the next scan. Consider the following code:

import java.util.Scanner;

class Main{
    public static void main(String args[]) {
        int rollNo;
        String name;
        Scanner scan=new Scanner(System.in);
        while (true) {
            System.out.print("Enter the roll no of the student: ");
            rollNo=scan.nextInt();
            System.out.print("Enter the name of the student: ");
            name=scan.nextLine();
            System.out.printf("Roll no. %d, Name: %s has been registered%n",rollNo, name);
        }
    }
}

A sample run:

Enter the roll no of the student: 1
Enter the name of the student: Roll no. 1, Name:  has been registered
Enter the roll no of the student: Arvind
Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at Main.main(Main.java:10)

Note that this problem can occur even with next but not with nextLine as shown with the following code:

import java.util.Scanner;

class Main {
    public static void main(String args[]) {
        int rollNo = 0;
        String name;
        boolean valid = true;
        Scanner scan = new Scanner(System.in);
        while (true) {          
            do {
                System.out.print("Enter the roll no of the student: ");
                try {
                    rollNo = Integer.parseInt(scan.nextLine());
                } catch (Exception e) {
                    System.out.println("The value should be an integer.");
                    valid = false;
                }
            } while (!valid);
            System.out.print("Enter the name of the student: ");
            name = scan.nextLine();
            System.out.println("Roll no. " + rollNo + ", Name: " + name + " has been registered");
        }
    }
}

A sample run:

Enter the roll no of the student: 1
Enter the name of the student: Arvind
Roll no. 1, Name: Arvind has been registered
Enter the roll no of the student: a
The value should be an integer.
Enter the roll no of the student: 12.5
The value should be an integer.
Enter the roll no of the student: 2
Enter the roll no of the student: Avinash
The value should be an integer.
Enter the roll no of the student: 

Conclusion: it is better to use nextLine with proper logic (e.g. exception handling, loop back on invalid input etc.) instead of next or nextInt to avoid issues mentioned above. I also recommend you go through documentation on Scanner.

Upvotes: 2

Related Questions