Home > OS >  Error trying to call parts of a class in a method Java
Error trying to call parts of a class in a method Java

Time:03-24

I am this close to finally completing this assignment, but I've hit a roadblock. For some reason I can't get call aspects of the class in my methods. Does anyone have a way of doing this that works? I've put comments next to the three parts I'm referring to. I do not know what is causing these errors, and Im grateful for any help.

public class Main
{
  public static int w = -1;
  public static int x = 0;
  public static int y = 0;
  public static int z = 0;
  public static String[] names;
  public static int[] years;
  public static String[] studios;
    public static class Movie{
    // instance variables
    private int year;
    private String title;
    private String studio;
    private int placement;
    // Constructor for objects of class Movie
    Movie(String title, int year, String studio)
    {
        // initialize instance variables
        this.title = title;
        this.year = year;
        this.studio = studio;
    }

    public String getTitle()
    {
        return title;
    }
    
    public void setTitle(String title)
    {
        this.title = title;
    }
    public void setPlacement() {
      w =1;
      this.placement = w;
    }
      public int getPlacement() {
        return placement;
      }
    public String getStudio()
    {
        return studio;
    }
    
    public void setStudio(String studio)
    {
        this.studio = studio;
    }
    
    public int getYear()
    {
        return year;
    }
   
    public void setYear(int year)
    {
        this.year = year;
    }
   
    public String toString()
    {
        String str = String.format("%-30s M   %-20s", title, year, studio);
        return str;
    }
    }
    
    public static void arrNames(String name) {
      x =1;
      names = new String[x];
      names[(x-1)] = name.getTitle(); //error here, why can't I just use getTitle()?
    }
    public static void arrYear(String name) {
      y =1;
      years = new int[y];
      years[(y-1)] = name.getYear(); //same error
    }
    public static void arrStudio(String name) {
      z =1;
      studios = new String[z];
      studios[z-1] = name.getStudio(); //same error
    }
  public static void setGroup(String name) {
      arrNames(name);
      arrYear(name);
      arrStudio(name);
    }
    public static void getGroups(String name) {
      int a = x;
      int b = y;
      int c = z;
      int d = 0;
      int e = 0;
      int f = 0;
      System.out.println("Names Category:  ");
      while (a > 0) {
        System.out.print(names[d]   "  ");
        d =1;
        a-=1;
        }
      System.out.println("Years Category:  ");
      while (b > 0) {
        System.out.print(years[e]   "  ");
        e =1;
        b-=1;
      }
      System.out.println("Studios Category:  ");
      while (c > 0) {
        System.out.print(studios[f]   "  ");
        f =1;
        c-=1;
      }
    
  public static void main(String[] args) {
    Movie nemo = new Movie("Finding Nemo",2003,"Pixar");
    setGroup(nemo.getTitle());
    }
}

CodePudding user response:

I will take this exemple:

 public static void arrYear(String name) {
  y =1;
  years = new int[y];
  years[(y-1)] = name.getYear(); //same error
}

You are trying to use the method getYear on the variable name and the name is a String and not a Movie Object You could just replace the argument from String to Movie like that:

 public static void arrYear(Movie name) {
  y =1;
  years = new int[y];
  years[(y-1)] = name.getYear(); //same error
}

and it should work for all your functions

  • Related