Method problems
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;
}
}
leroi green
Junior Poster in Training
93 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
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);
}
}
leroi green
Junior Poster in Training
93 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
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
leroi green
Junior Poster in Training
93 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
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.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
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
leroi green
Junior Poster in Training
93 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
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);
}
}
leroi green
Junior Poster in Training
93 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
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.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847