Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Problem compiling

Options
  • 15-11-2001 5:15pm
    #1
    Closed Accounts Posts: 1,136 ✭✭✭


    I'm having some trouble with getting this darn program to compile (haven't we all heard that one before).

    Now it's just a simple KeyboardInput test program I'm trying to compile here:

    class KeyboardInputTest
    {
    public static void main(String[] args)
    {
    KeyboardInput in = new KeyboardInput() ;
    System.out.print("Type an integer: ") ;
    int n = in.readInteger() ;
    System.out.println("Integer was: " + n) ;
    System.out.print("Type a long: ") ;
    long l = in.readLong() ;
    System.out.println("Long was: " + l) ;
    System.out.print("Type a double: ") ;
    double d = in.readDouble() ;
    System.out.println("Double was: " + d) ;
    System.out.print("Type a float: ") ;
    float f = in.readFloat() ;
    System.out.println("float was: " + f) ;
    System.out.print("Type a char: ") ;
    char c = in.readCharacter() ;
    System.out.println("char was: " + c) ;

    c = in.readCharacter() ;
    System.out.print("Type a String: ") ;
    String s = in.readString() ;
    System.out.println("String was: " + s) ;
    }
    }

    I have another class file in the same directory, but whenever I try to compile *this* program, I get:

    Cannot resolve symbol: KeyboardInput


    Any ideas? I'm sure it's just a simple thing, just need to find out why it won't compile. Thanks

    Occy


Comments

  • Registered Users Posts: 16,413 ✭✭✭✭Trojan


    First of all a disclaimer - I'm not a java guy, and I don't know what KeyboardInput is.

    But... when you get a "cannot resolve symbol" type message it usually means you're missing a definition for it. Is it included in a package (and do you have an import statement for that package)? Or did you define it somewhere and forget to include the definition file in the project?

    Look up the message "cannot resolve symbol" in the error messages section of the compiler help, it should give you more info.

    Rgds,
    Al.


  • Registered Users Posts: 16,413 ✭✭✭✭Trojan


    ... check your CLASSPATH setting ...

    Al.


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    This is usually to do with packages.
    I was just having the exact same problems ;)
    What I did was put all my source files into one directory
    then compile using
    "javac -d . *.java"
    That should work, of course there was one interface I had to compile alone each time for some reason so I just did:
    "javac -d . Cacheable.java"

    This creates the folders according to the package names.
    i.e. if you're package is com.whatever.something
    that folder structure will be created by javac.

    Hope this helps.


  • Registered Users Posts: 2,660 ✭✭✭Baz_


    What I think is wrong is that you are trying to use a class that has not been defined (I am assuming this because KeyboardInput is not in the help files, and it is also the name of your main class).

    So to check this out you can type in the code window:
    in.
    
    and if a method list comes up then there is a KeyboardInput class, if not then you either have autocomplete (or whatever that feature is called) turned off, or the class is not there.

    However I just realized that you might not be using jbuilder like I do, so ****.

    Anyway your class is called keyboard input yet it has no data and no methods therein, I think this is your problem, yet you try to assign a variable to be a reference to an object that essentially doesn't exist (apart from main method). I also checked to see if there was a readInteger method in the object class (which you are very probably extending) and it doesn't which means from what I see that there is nowhere at all that the method is defined, this is quite bad, as you have found out.

    Now it could be that you are trying to implement another class and mistakenly named your class the same name, essentially overwriting, and denying access to that class, but I don't think that that is even possible to do.

    Unfortunately as I found out there is no easy way to input information in Java (unless you use swing), so what you must do is:
    String s;
    
    try
    {
       InputSreamReader isr = new InputStreamReader(System.in);
       BufferedReader br = new BufferedReader(isr);
       s= br.readLine();
    }
    catch(IOException e)
    {
       s="";
    }
    

    1: Ensure that the string is declared before the try/catch structure.

    2: This will input only a line of text, conversion to numbers is a different story, to integers its easy, to floats...

    3:Sorry I cannot explain better but I'm not yet good enough at Java to enable me to.

    so to convert to an integer, you can enter this line after s=br.readLine()
    int x=Integer.parseInt(s);
    

    1: This declared and defined x in one statement in the try body, and if you want to use x outside of that body you must declare it before the body.

    Now if anything is unclear there, and I'm sure it will be, just ask a question.

    Baz_

    P.S. Where did you read about doing what you were trying to do????


  • Registered Users Posts: 18,484 ✭✭✭✭Stephen


    That KeyboardInput class sounds like the "ConsoleReader" class to me. I had a similar problem - turned out I hadn't put the ConsoleReader.class in the same directory as my .java program when compiling.

    Also forgot to use the -classpath parameter when compiling, so the compiler gave that error you have.

    eg. C:\whatever> javac -classpath . KeyboardInputTest.java

    Take anything i say here with a pinch of salt, i'm an utter newbie when it comes to Java and indeed programming in general. :)


  • Advertisement
  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    I get probs like this with JBuilder, even when I stick everything in the same directory. Assuming you have written and successfully compiled the KeyboardInput class, what I tend to do is just stick something like
    package Project1;
    
    at the top of all the files I'm using while I compile and test everything, and then remove it when I'm finished. Hope this helps :)


  • Closed Accounts Posts: 37 nucular


    javac -classpath ".;%classpath%" yourprogram.java

    If you haven't got it working already.. this should fix you up.. javac (AFAIK) doesn't search your current directory unless it is on the classpath..


    otherwise if you do have package statements then you need to be in the directory at the root of your directory structure for the program and type

    javac -classpath ".;anyotherjarfiles.jar;%classpath% the/package/structure/using/slashes/yourprogram.java

    hope this helps


  • Posts: 0 ✭✭ [Deleted User]


    FFS, So much fuss over such a tiny problem!

    copy KeyboardInput.java into the same folder as KeyboardInputTest.java.

    If you dont have the file d/l from here:

    http://www.dcs.kcl.ac.uk/DevJavaSoft/FirstEdition/SourceCode/ADS/KeyboardInput.java



    [edit] Wow, I just realized, I used google to find that file, but its actually on my home network (Department of CS, King's), weird or what?[edit]


Advertisement