| Author |
Best way to get input
|
michael delta
Ranch Hand
Joined: Aug 11, 2009
Posts: 35
|
|
|
hello, I'm using Scanner class to get input from user but I noticed there are some other ways to do that. Could you please tell me the pros and cons of each way with a tiny presentation if possible. Thnx in advance!
|
 |
michael delta
Ranch Hand
Joined: Aug 11, 2009
Posts: 35
|
|
I have another question,I'm not starting a new thread for this.
This is an exercise from past exams:
a. I'm not really getting the control flow
b. What happens if two or more exceptions happen at the same time in a try block?For example
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32675
|
|
Welcome to JavaRanch
Unusual name (as mine is): is that your real name?
4 popular ways to get entry from the keyboardCommand-line argumentsjavax.swing.JOptionPane.showInputDialog() methodBufferedReader wrapped around a Reader, eg InputStreamReader, wrapped around a Stream, eg System.injava.util.Scanner. Since Scanner was introduced in 2004 (Java 5) it has probably become the most popular means of keyboard input.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32675
|
|
Please use one thread per question, and you would have been better to start a second thread. Your code is difficult to read because you didn't indent it.
You can't get two simultaneous exceptions in the same thread; the first Exception will stop the try before the second Exception can occur. In your example I would expect the ArrayOutOfBoundsException to prevent the potential ArithmeticException from occurring.
You haven't been told to catch ArrayIndexOutOfBoundsException or ArithmeticException, have you?
|
 |
michael delta
Ranch Hand
Joined: Aug 11, 2009
Posts: 35
|
|
Thnx,nice to be here.
As for the name, I'm Greek
Please use one thread per question, and you would have been better to start a second thread.
I'm sorry, I'll do so.
I was looking at the exercise, the double exception was just a what-if-case of mine.
So,if I can't get two simultaneous exceptions in the same thread as long as a statement evaluates from right to left wouldn't I expect the ArithmeticException to occur first?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32675
|
|
|
You will have to try it, but if you go left-to-right I would have thought the array index problem would occur first.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9948
|
|
The assignment operator is right-to-left, and is also pretty low in the precedence chart - which makes sense. You have to evaluate the RHS before you can assign it to the left, and you'd want to do almost everything else before you make that assignment.
So I think you'll get a divide by zero error first...i think. I haven't tried it.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32675
|
|
fred rosenberger wrote:So I think you'll get a divide by zero error first...i think. I haven't tried it.
I have, and I am keeping quiet about what happened.
|
 |
 |
|
|
subject: Best way to get input
|
|
|