Alternative to Scanner for Data Entry

Asked

Viewed 477 times

7

In java, when we’re learning to capture data input, usually by text mode, we’re told to use the class Scanner.

Is there any alternative to Scanner to capture data input by text mode? If so, how does it work?

2 answers

7


As stated in Soen:

Class BufferedReader and InputStreamReader

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
int i = Integer.parseInt(br.readLine());

Class DataInputStream

DataInputStream dis = new DataInputStream(System.in);
int i = dis.readInt();

The readLine in the DataInputStream is discontinued, to take the value of String you can use the previous solution with BufferedReader.


Class Console

Console console = System.console();
String s = console.readLine();
int i = Integer.parseInt(console.readLine());

It seems that this method does not work on some Ides.

6

I don’t know if that would be a viable alternative, but you can use JOptionPane to pick up data entry.

String nome = JOptionPane.showInputDialog("Entre com um nome: ");
  • 1

    Interesting suggestion, but as he said it is by text mode, but leave as an alternative suggestion for those who work java.swing (it would be interesting to add this detail to the answer). By the way I found a cool example with java.swing: http://answall.com/q/72032/3635 +1

  • 1

    @Guilhermenascimento yes, I thought it would not be a 100% viable alternative but I took it because of the title. I will look at this example with swing :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.