#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0

    Gosling, Chapter 1


    I have started working my way through Gosling's 1996 Java book. It isn't going so well. In Chapter 1, I am merely trying to type in Example 1.11 exactly as written and run it.

    Besides the fact that there appears to be a typo in Gosling's book (ProcessValues calls a non-existent function processValue, without the s), my java compiler gives an error when compiling:

    Could not find or load main class Lookup.

    Could someone please clue me in as to what is going on?


    interface Lookup {

    Object find(String name);
    }

    void processValues(String[] names, Lookup table) {
    for (int i=0; i<names.length;i++) {
    Object value = table.find(names[i]);
    if (value != null)
    processValues(names[i],value);
    }
    }

    class SimpleLookup implements Lookup {
    private String[] Names;
    private Object[] Values;
    public Object find(String name) {
    for (int i=0 ; i<Names.length; i++) {
    if (Names[i].equals(name))
    return Values[i];
    }
    return null;

    }
    }

    public static void main(String[] args) {
    SimpleLookup.find("foo");
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Please post the full text of the program (in code tags)
    and the full text of the error messages.

    my java compiler gives an error when compiling:

    Could not find or load main class Lookup.
    That does not look like a compiler error message. It looks like an error from the java command when it tries to execute a class that it can not find. For example:
    Code:
    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
    
    D:\JavaDevelopment\Testing\ForumQuestions9>java NoClassFile
    Error: Could not find or load main class NoClassFile
    
    D:\JavaDevelopment\Testing\ForumQuestions9>
    First use the javac command to create a Lookup.class file,
    then use the java command: java Lookup
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0
    Originally Posted by NormR
    Please post the full text of the program (in code tags)
    and the full text of the error messages.
    Many forums disable code tags for new users, and devshed apppears to be one of them.

    Thank you for your guidance. I am now able to at least see what errors the compiler spits out, although it is rather useless
    without a way of seeing what line numbers correspond to
    which lines of code.

    During my over 30 years of programming, I have used Unix/Linux, editing in emacs, and debugging with gdb. I thought I would give Windows a try this time. That seems to be the root of the problems. Emacs is barely usable in Windows, and the shells that are available do not even allow cut and paste.

    Eclipse and Netbeans have steep learning curves, and in addition require knowledge of Java. Most books suggest learning Java without an IDE first. Hence the problems.

    I have programmed extensively in C and C++ (even Pascal), but Java seems quite a bit more complicated.

    The biggest problem is finding a reasonable way to edit and compile the programs, and to see which lines the compiler is complaining about. Eclipse and Netbeans seem to be wasting more time than they are saving, with a variety of obscure symbols to denote the various errors.

    Sorry for the new user id. Devshed locked me out of my old account and would not let me reset my password.

    javac Lookup.java
    Lookup.java:12: error: class, interface, or enum expected
    void processValues(String[] names, Lookup table) {
    ^
    Lookup.java:13: error: class, interface, or enum expected
    for (int i=0; i<names.length;i++) {
    ^
    Lookup.java:13: error: class, interface, or enum expected
    for (int i=0; i<names.length;i++) {
    ^
    Lookup.java:15: error: class, interface, or enum expected
    if (value != null)
    ^
    Lookup.java:17: error: class, interface, or enum expected
    }
    ^
    5 errors
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    The error messages say that the compiler has found the end of a class and expects there to be a definition for a class, interface, or enum next.
    Check that the {}s are properly placed so all the methods are in a class's definition.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0
    Originally Posted by NormR
    The error messages say that the compiler has found the end of a class and expects there to be a definition for a class, interface, or enum next.
    Check that the {}s are properly placed so all the methods are in a class's definition.
    Thank you for your time. I have switched to reading Core Java, which is a much better book. It simultaneously gives an introduction to Java 7 and the Eclipse IDE. There are no typos, and all the programs compile. I have made it all the way through the first three Chapters with no problems, because the author correctly explains everything.

    Unfortunately, this forum refuses to allow me to log in, and I have to re-register with a new username and e-mail every time I want to reply, so this will be my last reply.

    Kind Regards.

IMN logo majestic logo threadwatch logo seochat tools logo