简体   繁体   中英

“Cannot be resolved to a type”

Trying to create a new instance of an object. I want to create a new film and have it displayed in an arraylist.

So I have two classes.

Class 1

public class Films {

//Film Attributes
private String title, studio, director;
private int release, duration, rating;


//default constructor
public Films(){
    title = null;
    studio = null;
    director = null;
    release = 0;
    duration = 0;
    rating = 0;
}

//Start of Film method
public void setFilms(String title, String studio, String director, 
                  int release, int duration, int rating) 
{
    this.title = title;
    this.studio = studio;
    this.director = director;
    this.release = release;
    this. duration = duration;
    this.rating = rating;
}//end of Film method

//Start of getString method
public String getString(){
    return "\nTitle: " + title + "\nRelease: " + release + "\nDuration: " + duration
            + "\nStudio: " + studio + "\nDirector: " + director + "\nRating: " + rating
            + "\n";
}//end of getString method

}

and class 2

    //start of createFilm() method
private static void createFilm(){

    Films newfilm = new Films(); //create a new instance of a film object

    System.out.println("Film Title: ");
    String title = in.next();

    System.out.println("Release Date: ");
    int release = in.nextInt();

    System.out.println("Duration: ");
    int duration = in.nextInt();

    System.out.println("Studio: ");
    String studio = in.next();

    System.out.println("Director: ");
    String director = in.next();

    System.out.println("Rating: ");
    int rating = in.nextInt();

    newfilm.setFilms(title, release, duration, studio, director, rating);
    myfilms.add(newfilm);
}

}

I only included the method which i am having trouble with in class 2. Basically the trouble im having is in class 2, the line;

Films newfilm = new Films(); //create a new instance of a film object

is telling me 'Films cannot be resolved to a type' x2. So both 'Films'.

I imagine its something very stupid and silly that i've overlooked, assumed or missed out but I cant figure out what the problem is.

This is my first post on this subject as you can probably tell so im sorry if i've missed out valuable and key information and parts of the code. I'm new to programming.

It is an import issue you need to add import statment here is an example

Notice first line package com.ura.stam.com.ura.stammm; it is basicly a place where your class is placed

package com.ura.stam.com.ura.stammm;

public class MyFilms {

public MyFilms(){
    System.out.println("Callin constructor of films class");
}

}

Now look how i import it to other class Notice the second line import com.ura.stam.com.ura.stammm.MyFilms; it tells the compile to bring this class to MyTestClass

package com.ura.stam.com.ura.sdss;
import com.ura.stam.com.ura.stammm.MyFilms;

public class MyTestClass{
public static void main(String[] args) {
    MyFilms myFilms = new MyFilms();
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM