Hi, so I have to write a program for my Java assignment and I am just starting out so I might be doing this wrong but I can't figure out why it won't invoke the method I have created.

import java.util.Scanner;
public class EvalMarks1 {
    public static void main(String[] args) {

        Scanner input = new Scanner (System.in);
        int i;
        String endProgram = "NONE";
        String [] student = new String [10];
        do {
             System.out.println ("Please enter name of 10 students:");

        for (i = 0; i < 10; i++)
        {
            System.out.print ("Student " + (i+1) + ": ");
            student [i] = input.nextLine();
        }         
        student.print ();



        }while (true);



    }

     public void print (String [] student)

    {
        int i;

        for (i = 0; i < 10; i++)
        {
            System.out.print (student [i] + "\n");
        }
    }
}

This isn't the full program yet, but I can't proceed without invoking the method and I know this can be done without creating a method but it is required to do so in my assignment. Any help would be great, thank you

Recommended Answers

All 8 Replies

Do you have an error message (eg something about static context?). If so please post the exact complete message (copy/paste, don't summarise it)

Sorry. I am using netbeans and it's giving this error Click Here

  1. You have defined a print method with one parameter (String[]), but when you try to call it you don't pass any parameter. In other words: student.print() is looking for a print method with no parameters, and you have not defined any syuch method.
  2. student is an array of Strings, so student.print() is looking for a method print() that is defined in the String[] class.

The definition is OK, so it's the call you need to fix. Once you have dome that you will get a "Non-static method cannot be referenced from a static context" error - try to fix it yourself before coming back here for more help.

Ok, I pretty much had figured that it was the call that had to be fixed but he has just taught us methods and classes in the last lecture and I have yet to be too familiar with them. I have been trying to figure out how to call it instead since yesterday but I don't have the faintest idea, could you perhaps nudge me in the right direction?

It's a method in the same class as where you're calling it from, so you can just call it by its name; you don't need to put anything before the method name. Also you will need to pass it a String[] as a paremeter in the () after the method name.

so would that be like print(student); because when I tried that, it says unreachable statement

Yes, that's the right syntax. I don't know why thats unreachable. Please post the exact version of the code that gives that error, and the exact complete error message.
Java error messages always tell you the exact line where the error was found.

Oh ok, I had invoked the method outside of the loop, it works when I use it inside the do loop. Thank you so much for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.