Reading Input thru console (strange error?)

Discussion in 'Java' started by perpetuallyfrozen, Sep 30, 2003.

  1. I am having some problems with the following lines of code:

    /** Gets a move entered thru console and returns it.
    * @param input the move entered by user
    * @return returns a valid move entered by user
    */
    private String getMove() {
    BufferedReader console =
    new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Enter a move: ");
    String input = console.readLine();

    if (!checkMove(input)) {
    System.out.println("Not a valid move.");
    input = getMove();
    }
    return input;
    }

    I have no idea why this code isn't working. Do I have to import
    java.io or something? The method is simply supposed to retrieve a
    string from the console and return it. What am I doing wrong?
    Suggestions?? Thanks in advance for any replies.

    - Chris

    p.s. checkMove() checks the move to a list of valid moves and returns
    a boolean
     
    perpetuallyfrozen, Sep 30, 2003
    #1
    1. Advertisements

  2. You'll have to tell what is not working. Does it compile? Does it make your
    computer explode? More details please :)
     
    Christophe Vanfleteren, Sep 30, 2003
    #2
    1. Advertisements

  3. You'll have to tell what is not working. Does it compile? Does it make your
    Sorry, I had the details in, but then I accidentally hit mousebutton 3
    and my webpage backed up, so I lost my previous post. The only errors
    I get are from the BufferedReader object and the InputStreamReader
    object. For some reason, it says it cannot resolve either of them. I
    tried adding:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;

    at the top, which got rid of the previous 3 errors, but then it gave
    me this error:

    BridgeProblem.java:45: unreported exception java.io.IOException;
    must be
    caught or declared to be thrown
    String input = console.readLine();
    ^

    That is the error I get trying to compile the program. Should I even
    have to import those two files? That doesn't really make sense. I
    recently tried putting the readLine() part in a try/catch block, but
    then I got errors related to the IOException I was throwing. It again
    said the symbol could not be resolved. Now I am completely confused,
    and I am starting to think something is wrong with my compiler
    (j2sdk1.4.1_02), but I am probably wrong.
     
    perpetuallyfrozen, Oct 1, 2003
    #3
  4. You'll have to tell what is not working. Does it compile? Does it make
    your
    Every name that is not imported must be imported before use.
    Some packages are imported automatically (such as java.lang.*),
    but most of them must be imported manually when you need them
    (that is one of the purpose of packages, you know).
    Yes. Java forces exceptions to be catched or rethrown. The method
    BufferedReader.readLine() throws an IOException which must be caught :

    try
    {
    String input = console.readLine();
    }
    catch(IOException ex)
    {
    // oops
    }

    or rethrown :

    public void f() throws IOException
    {
    String input = console.readLine();
    }

    public void g()
    {
    try
    {
    f();
    }
    catch(IOException ex)
    {
    // oops
    }
    }

    You see, you _must_ catch it sometime. And since IOException is
    a name (as BufferedReader is), it must be imported :

    import java.io.IOException;

    Since you are using quite a few names from the java.io package,
    you could do

    import java.io.*;

    and all the names from that package will be imported.
    These are names which come from a package, not files.
    That is the way most of modern languages work. Names are in a
    namespace which protects from clashing. For example, two same
    names can coexist in different packages, but importing these
    two names at the same time will trigger an error.

    Try to read a bit more about packages.
    See above.
    yep


    Jonathan
     
    Jonathan Mcdougall, Oct 1, 2003
    #4
  5. Thanks, after I put the try/catch back in and imported java.io.*,
    everything started to work. I just don't really understand why they
    include a package that can print to the console (System.out.print),
    but not one that can read from it (System.in.readLine). Whatever. I
    think I understand now. Thanks for your help.
    - Chris
     
    perpetuallyfrozen, Oct 1, 2003
    #5
  6. Thanks, after I put the try/catch back in and imported java.io.*,
    Because System.out.print is not a package, it is an method.

    The package is named java.lang.System and, as I said, java.lang.*
    is imported automatically. Inside that class there are three
    public static objects, among which is `out`. `out` is of
    type PrintStream which has the method print().

    `But why don`t I have to import java.io.PrintStream to use it?`

    Because you can use an object which was defined in another file
    without importing its class, since that file already imports
    it. But you cannot use that class yourself without importing
    it.

    For example, 'in' has the type InputStream and you
    can use it without problem such as with System.out.read(),
    but you cannot use InputStream directly in your code without
    importing it.

    hope that was clear enough.


    Jonathan
     
    Jonathan Mcdougall, Oct 2, 2003
    #6
  7. Jonathan Mcdougall:
    The package is named java.lang, the class is called System.

    Regards,
    Marco
     
    Marco Schmidt, Oct 2, 2003
    #7
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.