hello everyone, i'm in class and having problems with this method declaration. i'm brand new so please don't be too rough, just trying to understand what i'm doing wrong & what will be needed to fix it

public static int iMethod (int i);
	{
		while (true)
		{
			if (i == 0)
				return i;
		}
	}
public static int iMethod (int i);
    {
        while (true)
        {
            if (i == 0)
                return i;
        }
    }

the while(true) will go on forever causing it to crash.
You dont really need it in there so try

public static int iMethod (int i);
    {
       if (i == 0)
                return i;
 
    }

okay, super thanx...

i've now plugged it into the full system but then errors flew up everywhere. here's the whole thing:

public class Five {

	// Declare Variable
	int i;

        public static int iMethod(int i);
        {
                if (i == 0)
                return i;
 
        }

	public static void main(String[] args)
	{
		// Prompt the user to enter an integer
		String intString = JOptionPane.showInputDialog("Enter an integer:");

		// Convert a string into int
		i = Integer.parseInt(intString);

		System.out.println("i = " + i);
	}
}
public class Five {
 
    // Declare Variable
    int i;
 
        public static int iMethod(int i);
        {
                if (i == 0){
                return i;
}
 
        }
 
    public static void main(String[] args)
    {
        // Prompt the user to enter an integer
        String intString = JOptionPane.showInputDialog("Enter an integer:");
 
        // Convert a string into int
        i = Integer.parseInt(intString);
 
        System.out.println("i = " + i);
    }
}

you dont use iMethod anywhere... i dont see why its needed

this part says 'missing method body, or declare abstract' and 'return outside method'

public static int i(int i);
        {
                if (i == 0)
                {
                return i;
                }
        }

and this part gives 'non-static variable 'i' cannot be referenced from a static context' , but the book shows that this has no problem:

public static void main(String[] args)
	{
		// Prompt the user to enter an integer
		String intString = JOptionPane.showInputDialog("Enter an integer:");

		// Convert a string into int
		i = Integer.parseInt(intString);

		System.out.println("i = " + i);
	}

now, when i swap the position of these methods then the second error message for the:

public static void main(String[] args)
	{
		// Prompt the user to enter an integer
		String intString = JOptionPane.showInputDialog("Enter an integer:");

		// Convert a string into int
		i = Integer.parseInt(intString);

		System.out.println("i = " + i);
	}

goes away

public static int i(int i);//<--delete that
        {
                if (i == 0)
                {
                return i;
                }
        }
 
 
     public static void main(String[] args)
    {
        // Prompt the user to enter an integer
        String intString = JOptionPane.showInputDialog("Enter an integer:");
 
        // Convert a string into int
       int i = Integer.parseInt(intString);
 
        System.out.println("i = " + i);
    }

sorry for missing those

This method actually won't compile either

public static int i(int i)
        {
                if (i == 0)
                {
                return i;
                }
        }

because there is no guaranteed return. If the i==0 check is false, there is no return. You need to return something if your check fails

public static int i(int i)
        {
                if (i == 0)
                {
                      return i;
                }
                else
                {
                      return -1; // or whatever you need if i != 0
                }
        }

Also, I can't figure out what the purpose of such a method is. Always name methods with something descriptive of what it is doing - "i" does not tell anyone anything about the purpose of the method.

well the book doesn't actually give a purpose for the program. it just wants you to make it work. and maybe that's the problem that i've been having. maybe i feel i'm bound to something that i'm really not. maybe i should just write whatever code will create an output

Okay, I've let it go and tried to end all with this since there's no specified function I just figured I'd do this but still getting problems

public static int i(int i)
        {
                if (i == 0)
                {
                      return i;
                      System.out.println("i is = " + i);
                }
                else if (i != 0)
                {
                      return i;
                      System.out.println("i is = " + i);
                }
        }

Okay, I've let it go and tried to end all with this since there's no specified function I just figured I'd do this but still getting problems

public static int i(int i)
        {
                if (i == 0)
                {
                      return i;
                      System.out.println("i is = " + i);
                }
                else if (i != 0)
                {
                      return i;
                      System.out.println("i is = " + i);
                }
        }

??? That makes even less sense than the first method. If "i" is zero or if it is not zero, return "i"? What is it that the method is intended to do?

The "unreachable statement" error you are surely getting are from the println() statements being after the return statements. Once the method has returned the value, code after that point will not be executed. Move the statements before the returns and they will be fine.

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.