User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Oct 2007
Posts: 58
Reputation: leroi green is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
leroi green leroi green is offline Offline
Junior Poster in Training

Method problems

  #1  
Oct 3rd, 2007
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;
		}
	}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2006
Location: in a dream
Posts: 127
Reputation: nschessnerd is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 6
nschessnerd's Avatar
nschessnerd nschessnerd is offline Offline
Junior Poster

Re: Method problems

  #2  
Oct 3rd, 2007
    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
  1. public static int iMethod (int i);
  2. {
  3. if (i == 0)
  4. return i;
  5.  
  6. }
  7.  
Last edited by nschessnerd : Oct 3rd, 2007 at 1:11 pm.
Reply With Quote  
Join Date: Oct 2007
Posts: 58
Reputation: leroi green is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
leroi green leroi green is offline Offline
Junior Poster in Training

Re: Method problems

  #3  
Oct 3rd, 2007
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);
	}
}
Reply With Quote  
Join Date: Dec 2006
Location: in a dream
Posts: 127
Reputation: nschessnerd is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 6
nschessnerd's Avatar
nschessnerd nschessnerd is offline Offline
Junior Poster

Re: Method problems

  #4  
Oct 3rd, 2007
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
Reply With Quote  
Join Date: Oct 2007
Posts: 58
Reputation: leroi green is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
leroi green leroi green is offline Offline
Junior Poster in Training

Re: Method problems

  #5  
Oct 3rd, 2007
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
Reply With Quote  
Join Date: Dec 2006
Location: in a dream
Posts: 127
Reputation: nschessnerd is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 6
nschessnerd's Avatar
nschessnerd nschessnerd is offline Offline
Junior Poster

Re: Method problems

  #6  
Oct 3rd, 2007
        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.
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,090
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 307
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Sensei

Re: Method problems

  #7  
Oct 3rd, 2007
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.
Reply With Quote  
Join Date: Oct 2007
Posts: 58
Reputation: leroi green is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
leroi green leroi green is offline Offline
Junior Poster in Training

Re: Method problems

  #8  
Oct 3rd, 2007
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
Reply With Quote  
Join Date: Oct 2007
Posts: 58
Reputation: leroi green is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
leroi green leroi green is offline Offline
Junior Poster in Training

Re: Method problems

  #9  
Oct 3rd, 2007
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);
                }
        }
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,090
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 307
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Sensei

Re: Method problems

  #10  
Oct 3rd, 2007
Originally Posted by leroi green View Post
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 4:06 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC