The better way to ask for input?

I am wondering which is the best way to go with java code. I need to create a class with simple prompts for input.. I have tried using both classes and cannot work out the particular benefits for each. Is this because I am still in the early stages of programming or are there situations that will occur as it becomes more complex??

import java.util.Scanner;

public class myClass
{
    Scanner stdin = new Scanner(System.in);

    public String getInput(String prompt)  
    {
        System.out.print(prompt);
        return stdin.nextLine();
    }

}

… or

import java.io.*;

public class myClass
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Input something: ");
        String name = stdin.readLine();
    }
}

I know these examples are showing different methods within these classes, but thought this might serve well for the discussion.

I’m really not sure which site is the best to ask this on.

9

I’ve been a Java programmer for 6 years now, and I rarely have to worry about how to read input from the command line. Most times, when I’m dealing with input and output, they are usually streams of some sort and therefore using a BufferedReader is generally a best practice, since it takes the bottleneck off having to read a file line by line.

However, for what you’re out to do, using System.in with Scanner is perfectly fine. Lets just say that if I wanted to handle large amounts of input, perhaps I would wrap System.in with a BufferedInputStream for efficiency, but if your program is waiting on you, then there is no advantage gained.

6

Simply reading System.in with BufferedReader, you end up writing a few lines of boilerplate code.

Moving up to Scanner, as suggested in Neil’s answer, makes it easier to pull data of various types out of the stream.

Personally, when a command-line interface is really the best choice (which includes any time it’s a stated requirement), I like to use System.console(). It’s not always available (for example, when running inside some IDEs), but when it is, it offers useful things like readPassword() which prevents the user’s input from being echoed back to the screen, and it makes it so that you can read from and write to the console using the same object. It’s really designed around the typical use case for command line tools that require user interaction.

Using System.console() doesn’t explicitly conflict with Scanner:

Console console = System.console();
String name = console.readLine("Please enter your name: ");
Scanner ageScanner = new Scanner(console.readLine("How old are you? ");
int age = ageScanner.nextInt();

They don’t quite play perfectly together in this way, since you have to instantiate a new Scanner for each line. Console does offer access to a Reader, which you can use to instantiate a Scanner:

Console console = System.console();
String name = console.readLine("Please enter your name: ");
Scanner consoleScanner = new Scanner(console.reader());
int age = consoleScanner.nextInt();

(Disclaimer: I haven’t tried the above code, and I’m not exactly sure how things will behave if you use console.readLine() intermixed with usages of a Scanner made from console.reader(). For clarity and consistency, it might be wise to choose one method and stick to it.)

Sticking with this example, I’d be more likely to write:

Console console = System.console();
String name = console.readLine("Please enter your name: ");
int age = Integer.parseInt(console.readLine("How old are you? ");

Depending on your needs, you may find that one, the other, or both are best suited to your needs. Generally, I find that Scanner is better suited to reading a file with a known format, and Console is better suited to interactive user input, because Scanner really shines when you have multiple tokens per line of input. Asking a user to enter that sort of input interactively is probably not going to result in the best experience.

JavaDoc: java.io.Console

Related question: Java: How to get input from System.console()

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật