2

I am learning Java and I am currently following a book and this is the code the book provided:

package Person;

public class Person {
    private String lastName;
    private String firstName;
    private int age;

    public Person(String last, String first, int a) {       //constructor
        lastName = last;
        firstName = first;
        age = a;
    }

    public void displayPerson() {
        System.out.print("    Last name: " + lastName);
        System.out.print(", First name: " + firstName);
        System.out.println(", Age: " + age);
    }

    public String getLast(){
        return lastName;
    }
}

class ClassDataArray {
    private Person[] a;
    private int nElems;

    public ClassDataArray(int max){
        a = new Person[max];
        nElems = 0;
    }
    public Person find(String searchName){
        int j;
        for(j = 0; j<nElems; j++){
            if(a[j].getLast().equals(searchName)){
                break;
            }
        }
        if(j == nElems){
            return null;
        }else{
            return a[j];
        }
    }

    public void insert(String last, String first, int age){
        a[nElems] = new Person(last, first, age);
        nElems++;
    }

    public boolean delete(String searchName){
        int j;
        for(j = 0; j<nElems; j++){
            if(a[j].getLast().equals(searchName)){
                break;
            }
        }
        if(j == nElems){
            return false;
        }else{
            for(int k = j; k<nElems; k++){
                a[k] = a[k + 1];
            }
            nElems--;
            return true;
        }
    }

    public void displayA(){
        for(int j = 0; j<nElems; j++){
            a[j].displayPerson();
        }
    }
//////////////////////////////////////////////////////  
    class ClassDataApp{
        public static void main(String[] args){
            int maxSize = 100;
            ClassDataArray arr;
            arr = new ClassDataArray(maxSize);

            arr.insert("Evans","Parry", 24);
            arr.insert("Smith","Lorraine",37);
            arr.insert("Yee","Tom", 43);
            arr.insert("Adams","Henry", 63);
            arr.insert("Hashimoto","Sato",21);
            arr.insert("Stimson","Henry",29);
            arr.insert("Velasquez","Jose",72);
            arr.insert("Lamarque","Henry",54);
            arr.insert("Vang","Minh",22);
            arr.insert("Creswell","Lucinda",18);

            arr.displayA();

            String searchKey = "Stimson";
            Person found;

            found = arr.find(searchKey);
            if(found != null){
                System.out.print("Found ");
                found.displayPerson();
            }else{
                System.out.println("Can't find " + searchKey);
            }
            System.out.println("Deleting Smith, Yee, and Creswell");
            arr.delete("Smith");
            arr.delete("Yee");
            arr.delete("Creswell");

            arr.displayA();
        }
    }   
}

However I am getting the error:

The method main cannot be declared static; static methods can only be declared in a static or top level type

I think creating a separate file would fix this problem but I am following a book so I might be missing a concept? The book doesn't tell me to create another .java file. It is simply 2 classes in the file and one of them contains a static main. Could someone provide some sort of insight please Thank you!

3
  • 3
    You're incorrectly nesting the class.
    – SLaks
    Commented Dec 30, 2013 at 21:49
  • THank you! @SLaks i did not see that!
    – Liondancer
    Commented Dec 30, 2013 at 21:50
  • You need an extra } before class ClassDataApp. A good IDE should show you that. Commented Dec 30, 2013 at 21:51

4 Answers 4

2

Unless it's a typo, you're missing a closing } before class ClassDataApp{

4
  • Strange, Eclipse doesn't show me any errors regarding } in OP.
    – Pshemo
    Commented Dec 30, 2013 at 21:54
  • You will also need to delete the final "}". Eclipse would detect unbalanced braces. Commented Dec 30, 2013 at 21:56
  • Very strange indeed. I counted and recounted the braces - the code is indented correctly, so there's a missing end brace. Commented Dec 30, 2013 at 21:56
  • I separated each class and all the visible errors that the eclipse displayed are gone but when I run compile the code this error appears Error: Could not find or load main class Person.ClassDataArray$ClassDataApp
    – Liondancer
    Commented Dec 30, 2013 at 22:14
1

You should put your classes into different files. You have ClassDataApp nested inside ClassDataArray

1

Read this : http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html it has a good example. ClassDataApp could be static if it is nested.

1

public static main(String[] args) will be treated as the entry point into your program only when it is placed in a public class. A .java file can have only one public class.

If you want to keep all of the classes in the same java file, make only ClassDataApp class public.

Otherwise, place them in the same package in different java files and make sure that ClassDataApp is public and the other classes are at least not private.

2
  • Thank you for the input. I am trying to put every class in one file and I tried making the ClassDataApp public but I get the error saying The public type ClassDataApp must be defined in its own file I could simply put that class in another file I guess that would be an easy fix. Do you think that because this file is in its own package that this error occurs?
    – Liondancer
    Commented Dec 30, 2013 at 22:31
  • 1
    You can put all the classes in one file but only one of them may be public. In this case, ClassDataApp should be the public class and the other 2 should be non-public. Commented Dec 31, 2013 at 22:45

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.