Solved

Calling a method

Posted on 2004-03-29
10
266 Views
Last Modified: 2012-05-04
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();      
      
      }




}
0
Comment
Question by:pete420
  • 4
  • 3
  • 3
10 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 10708716
should be

new filmTest().readFilmDetails();

in main
0
 
LVL 5

Expert Comment

by:twobitadder
ID: 10708741
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();
     }

0
 
LVL 5

Expert Comment

by:twobitadder
ID: 10708747
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();
     }
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 5

Expert Comment

by:twobitadder
ID: 10708754
bleh just go with cehj's that's right.
0
 

Author Comment

by:pete420
ID: 10708804
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();
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10708823
Please read the answers in order pete420. The answer i posted is correct
0
 

Author Comment

by:pete420
ID: 10708847
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10708873
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
0
 

Author Comment

by:pete420
ID: 10708891
thanks man, its much appreciated.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10708908
8-)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question