aspose file tools*
The moose likes Beginning Java and the fly likes Best way to get input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Best way to get input" Watch "Best way to get input" New topic
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: 39851
    
  28
Welcome to JavaRanch
Unusual name (as mine is): is that your real name?

4 popular ways to get entry from the keyboard
  • Command-line arguments
  • javax.swing.JOptionPane.showInputDialog() method
  • BufferedReader wrapped around a Reader, eg InputStreamReader, wrapped around a Stream, eg System.in
  • java.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: 39851
        
      28
    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.
    You haven't been told to catch ArrayIndexOutOfBoundsException or ArithmeticException, have you?

    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: 39851
        
      28
    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: 11479
        
      16

    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.


    There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39851
        
      28
    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.
     
    jQuery in Action, 2nd edition
     
    subject: Best way to get input