Link to home
Start Free TrialLog in
Avatar of pete420
pete420

asked on

Calling a method

the problem im sure is actually quite simple but i think im appraching it the wrong way.
whenever i compile and run this class it terminated straight away. ie. "Process exit" is displayed.
it creates the object i think cuz there are no erros in my film object code.

basically all i want to know is how do i get the readFilmDetails method to be displayed. Why does my program not ask the user to input the data.

many thanks

pete

class filmTest
{
      //properties
      private String filmName;
      private int filmRating;
      private double filmMinutes;
      private int age;
      
      public static void main(String[] args)
      {
            Film[] film = new Film [5];

      
      }

      public void readFilmDetails()
      {
            System.out.println("Film Name: ");
            filmName = uuInOut.ReadString();
            System.out.println("Enter film rating: ");
            filmRating = uuInOut.ReadInt();
            System.out.println("Enter film minutes");
            filmMinutes = uuInOut.ReadDouble();      
      }

      public void readAge()
      {
            System.out.println("Enter Age: ");
            age = uuInOut.ReadInt();      
      
      }




}
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of twobitadder
twobitadder

You don't call the method readFilmDetails.

When your program is first started it executes the main method, you should call readFilmDetails inside here.
eg.

public static void main(String[] args)
     {
          Film[] film = new Film [5];
          readFilmDetails();
     }

public static void main(String[] args)
     {
          Film[] film = new Film [5];
          readFilmDetails();
     }


should be

public static void main(String[] args)
     {
          Film[] film = new Film [5];
          filmTest.readFilmDetails();
     }
bleh just go with cehj's that's right.
Avatar of pete420

ASKER

whenever i add in
readFilmDetails();
i get the following error msg.

filmTest.java:14: non-static method readFilmDetails() cannot be referenced from a static context
            filmTest.readFilmDetails();
Please read the answers in order pete420. The answer i posted is correct
Avatar of pete420

ASKER

yes i did see that, no offence was intended,
however i didnt think that was the 'correct' way to do it. i tried it and saw it worked but thought it was a strange way to get it inputed. correct me if im wrong because im hardly experienced in java.
No it's customary. You can turn your method into a static one and then you *would* be able to call it with

filmTest.readFilmDetails();

but at the moment it's an instance method, not a class one, and therefore requires an instance of class fileTest to be created before it can be called
Avatar of pete420

ASKER

thanks man, its much appreciated.
8-)