•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 456,519 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,821 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 494 | Replies: 9
![]() |
•
•
Join Date: Oct 2007
Posts: 58
Reputation:
Rep Power: 2
Solved Threads: 0
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;
}
}
You dont really need it in there so try
java Syntax (Toggle Plain Text)
public static int iMethod (int i); { if (i == 0) return i; }
Last edited by nschessnerd : Oct 3rd, 2007 at 1:11 pm.
•
•
Join Date: Oct 2007
Posts: 58
Reputation:
Rep Power: 2
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:
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);
}
}•
•
Join Date: Oct 2007
Posts: 58
Reputation:
Rep Power: 2
Solved Threads: 0
this part says 'missing method body, or declare abstract' and 'return outside method'
and this part gives 'non-static variable 'i' cannot be referenced from a static context' , but the book shows that this has no problem:
now, when i swap the position of these methods then the second error message for the:
goes away
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
Last edited by nschessnerd : Oct 3rd, 2007 at 1:57 pm.
This method actually won't compile either 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
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.
public static int i(int i)
{
if (i == 0)
{
return i;
}
} public static int i(int i)
{
if (i == 0)
{
return i;
}
else
{
return -1; // or whatever you need if i != 0
}
}•
•
Join Date: Oct 2007
Posts: 58
Reputation:
Rep Power: 2
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);
}
}•
•
•
•
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- error message (Windows tips 'n' tweaks)
- PHP vs ASP... the big ShOwdOwN (IT Technologies and Trends)
- Need help with Best Offers popup (Viruses, Spyware and other Nasties)
- Laptop Monitor Turns Off When Windows Is Started (Peripherals)
- IE and Firefox problems (Windows NT / 2000 / XP / 2003)
- using a varible from main method (Java)
- Sorting arrays of pointers with function? (C)
- A possible solution to about:blank problems (Viruses, Spyware and other Nasties)
- JTextField not getting updated when using setText(String) method (Java)
Other Threads in the Java Forum
- Previous Thread: problem with tomcat server
- Next Thread: How to create a setup file 4 my project??



Linear Mode