Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How do you call a method from the same non-application class?

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How do you call a method from the same non-application class?

    I have a read method that reads information from a file into an array. I have to be able to call that method to another method in that same class that uses the information in the array. I'v been stuck on this for a while now and I have no idea what to do. Any help is appreciated.


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: How do you call a method from the same non-application class?

    What doesn't work? Do you get any errors/exceptions? If so post them.

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: How do you call a method from the same non-application class?

    Are these static methods or instance methods? Is your array global or being passed from one method to the other? Hard to determine what you are doing without seeing the code or an error.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How do you call a method from the same non-application class?

    I'm not getting any errors because I have no idea how to even try it. I want to be able to use the read method in the display method, which displays the information of a certain student depending on what ID the method accepts.

    public void read(File input)
    {
    input = new File("Enrollment.txt");

    try
    {
    Scanner in = new Scanner(input);
    int i = 0;

    while (in.hasNextLine() && i < students.length)
    {
    fName = in.next();
    lName = in.nextLine();
    uid = in.nextLine();
    hours = in.nextInt();
    in.nextLine();
    major = in.nextLine();

    students[i] = new Student();

    i++;
    }
    }
    catch (FileNotFoundException e)
    {
    System.out.println("The file could not be found.");
    }
    }





    public void displayStudent(String unid)
    {
    String lName = getLName(), fName = getFName(), major = getMajor();
    int i = 0, hours = getHours();
    File input = new File("Enrollment.txt");
    Scanner in = null;

    try
    {
    in = new Scanner(input);

    while (in.hasNext())
    {
    if (unid.equals(uid))
    {
    System.out.println(students[i]);
    System.out.println(fName + lName + "\n" + uid + "\n"
    + hours + "\n" + major);
    }

    else
    {
    System.out.println("The UID does not exist.");
    }

    i++;
    }
    }

    catch (FileNotFoundException e)
    {
    System.out.println("The file was not found.");
    }
    }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do you call a method from the same non-application class?

    use the read method in the display method
    To call a method in the same class, just use the method name and its args:
      theMethodToBeCalled(theArgsForTheMethod); //  call theMethodToBeCalled()

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Can't call method from external class
    By zlloyd1 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 24th, 2012, 08:30 PM
  2. Call class method from another class
    By NewbieJavaProgrammer in forum Object Oriented Programming
    Replies: 1
    Last Post: November 21st, 2012, 06:56 AM
  3. Can't call paint() method from another class
    By Huw in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 18th, 2012, 02:15 AM
  4. update static field and call method within the class?
    By GeneralPihota in forum Object Oriented Programming
    Replies: 7
    Last Post: February 6th, 2012, 09:20 PM
  5. Call method(s) within the same class
    By mwr76 in forum Object Oriented Programming
    Replies: 8
    Last Post: September 26th, 2011, 12:58 AM

Tags for this Thread