1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Java: Passing variables

Discussion in 'Programming & Webmastering' started by Munki, Oct 28, 2010.

  1. Munki

    Munki

    Joined:
    Sep 13, 2008
    Messages:
    1,227 (0.55/day)
    Thanks Received:
    182
    Location:
    Metro Atlanta
    Can someone give me an example of this. I'm working on a project, and I can't seem to get these damn variables to pass to other methods. Here is what I have so far(its not done).
    Code:
    import java.util.Scanner; 
    public class Eggs
    {
    	public static void main (String[] args)
    		{	
    			Scanner input = new Scanner (System.in);
    			int eggs;
    			
    			System.out.println(" Please enter in an integer: ");
    			eggs = input.nextInt();
    									
    			passedEggs(eggs);
                            
                            System.out.println(eggs);
        
    
    			
    			
    		}
    
    
    	private int passedEggs()
    	{
    		int equalDozen;
    		int remainderEggs;
    		
    		equalDozen = eggs / 12;
    		remainderEggs = eggs % 12;
    		
    		return eggs;
    		
    	}
    }
    This is due at by midnight, I have gone through the book and Google searched, but im not understanding. Could someone point me in the right direction?

    Oh, and my teacher is out of town with no service :cry:
    Thanks guys.
     
  2. char[] rager New Member

    Joined:
    Jun 9, 2010
    Messages:
    310 (0.19/day)
    Thanks Received:
    26
    Location:
    Massachusetts Institute of Technology, Computer Sc
    Warning: I don't know Java.

    But I know C#, which is very similar to Java, so.

    I believe

    should say

    Second of all

    is not being stored to anything.
     
    Munki says thanks.
  3. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.16/day)
    Thanks Received:
    16
    i agree, private int passedEggs(int newEggs)

    then int eggs = newEggs or something like that, sorry its been a while.

    Or something like passedEggs(int eggs) and this.eggs = eggs;
    but that might get confusing having eggs in there twice.

    finally, something like system.out.println(passedEggs(eggs));

    I hope this helps.

    edit: I'm not sure i see clearly what passedEggs is supposed to accomplish though.
     
    Munki says thanks.
  4. char[] rager New Member

    Joined:
    Jun 9, 2010
    Messages:
    310 (0.19/day)
    Thanks Received:
    26
    Location:
    Massachusetts Institute of Technology, Computer Sc
    Code:
    import java.util.Scanner; 
    
    public class Eggs
    {
    	public static void main (String[] args)
    	{	
    		[COLOR="Red"]// I don't know Java, so I don't know what is going on here[/COLOR]
    		Scanner input = new Scanner (System.in);
    			
    		int input, eggs;
    			
    		System.out.println(" Please enter in an integer: ");
    		input = input.nextInt();
    									
    		eggs = passedEggs(input);
                            
    		System.out.println(eggs);    			
    	}
    
    	private int passedEggs(newEgg)
    	{		
    		int equalDozen, remainderEggs, eggs = newEgg;
    		
                   [COLOR="Red"] // Keep in mind, you are not changing what is stored in eggs here.
                    // So, when you return eggs, it will be what is stored in newEgg[/COLOR]
    		equalDozen = eggs / 12;
    		remainderEggs = eggs % 12;
    		
    		return eggs;		
    	}		
    }
     
    Last edited: Oct 28, 2010
  5. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.16/day)
    Thanks Received:
    16
    looks good to me, except i dont think input = input.nextInt(); is gonna work. I dont think you can use input as a variable name.
     
  6. Munki

    Munki

    Joined:
    Sep 13, 2008
    Messages:
    1,227 (0.55/day)
    Thanks Received:
    182
    Location:
    Metro Atlanta
    Thanks for trying guys, but apparently C# isnt close enough. Its not compiling. its not seeing eggs from the main method/
     
  7. char[] rager New Member

    Joined:
    Jun 9, 2010
    Messages:
    310 (0.19/day)
    Thanks Received:
    26
    Location:
    Massachusetts Institute of Technology, Computer Sc
    I am sorry, it should be int before newEgg in the method signature.

    Like: private int passedEggs(int newEgg)
     
  8. Munki

    Munki

    Joined:
    Sep 13, 2008
    Messages:
    1,227 (0.55/day)
    Thanks Received:
    182
    Location:
    Metro Atlanta
    test.java:3: class Eggs is public, should be declared in a file named Eggs.java
    public class Eggs
    ^
    test.java:10: input is already defined in main(java.lang.String[])
    int input, eggs;
    ^
    test.java:13: incompatible types
    found : int
    required: java.util.Scanner
    input = input.nextInt();
    ^
    test.java:15: passedEggs(int) in Eggs cannot be applied to (java.util.Scanner)
    eggs = passedEggs(input);
    ^
    4 errors


    ^^ the compile errors even after adding the (int newEgg)
     
  9. char[] rager New Member

    Joined:
    Jun 9, 2010
    Messages:
    310 (0.19/day)
    Thanks Received:
    26
    Location:
    Massachusetts Institute of Technology, Computer Sc
    Well, I just taught myself some java from an online tutorial.

    Copy and paste this into your editor and see if it runs.

    Code:
    public class Eggs
    {
    	public static void main (String[] args)
    	{	
    		[COLOR="Red"]// I don't know Java, so I don't know what is going on here[/COLOR]			
    		int input, eggs;
    			
    		System.out.println(" Please enter in an integer: ");
    		input = TextIO.getlnInt();
    									
    		eggs = passedEggs(input);
                            
    		System.out.println(eggs);    			
    	}
    
    	private int passedEggs(int newEgg)
    	{		
    		int equalDozen, remainderEggs, eggs = newEgg;
    		
                    [COLOR="Red"]// Keep in mind, you are not changing what is stored in eggs here.
                    // So, when you return eggs, it will be what is stored in newEgg[/COLOR]
    		equalDozen = eggs / 12;
    		remainderEggs = eggs % 12;
    		
    		return eggs;		
    	}		
    }
     
    Last edited: Oct 29, 2010
  10. Munki

    Munki

    Joined:
    Sep 13, 2008
    Messages:
    1,227 (0.55/day)
    Thanks Received:
    182
    Location:
    Metro Atlanta
    test.java:16: <identifier> expected
    private int passedEggs(newEgg)
    ^
    1 error

    C&P it
     
  11. char[] rager New Member

    Joined:
    Jun 9, 2010
    Messages:
    310 (0.19/day)
    Thanks Received:
    26
    Location:
    Massachusetts Institute of Technology, Computer Sc
    Okay, put int infront of newEgg again.
     
  12. Munki

    Munki

    Joined:
    Sep 13, 2008
    Messages:
    1,227 (0.55/day)
    Thanks Received:
    182
    Location:
    Metro Atlanta
    I did that cause I noticed it. No luck

    man, i cant worry with this anymore. I appreciate your help and trying to get it working. I got one of the two programs to run. I'm sure you have better things to do.
     
  13. char[] rager New Member

    Joined:
    Jun 9, 2010
    Messages:
    310 (0.19/day)
    Thanks Received:
    26
    Location:
    Massachusetts Institute of Technology, Computer Sc
    Oh well, I tried. I am sorry that I was not able to assist you, maybe someone else on this forum who knows Java will be able to help you.
     
  14. Munki

    Munki

    Joined:
    Sep 13, 2008
    Messages:
    1,227 (0.55/day)
    Thanks Received:
    182
    Location:
    Metro Atlanta
    Im in school for networking, ill never understand why the hell I need this class anyways :p
     
  15. char[] rager New Member

    Joined:
    Jun 9, 2010
    Messages:
    310 (0.19/day)
    Thanks Received:
    26
    Location:
    Massachusetts Institute of Technology, Computer Sc
  16. Easy Rhino

    Easy Rhino Linux Advocate

    Joined:
    Nov 13, 2006
    Messages:
    13,458 (4.64/day)
    Thanks Received:
    3,264
    you need fordgtconcept to help you. he knows everything about java.
     
  17. WarEagleAU

    WarEagleAU Bird of Prey

    Joined:
    Jul 9, 2006
    Messages:
    10,797 (3.57/day)
    Thanks Received:
    546
    Location:
    Gurley, AL
    I would help but I am in MySQL and then fixing to go to ORacle. This is a bit different with the syntax tags and everything
     
  18. char[] rager New Member

    Joined:
    Jun 9, 2010
    Messages:
    310 (0.19/day)
    Thanks Received:
    26
    Location:
    Massachusetts Institute of Technology, Computer Sc
    I agree. In my opinion, Kreij and FordGT90Concept are the programming experts on this forum.

    Forgot to mention W1zzard. But he should be automatically thought of :) After all, he created this site and GPU-Z!
     
    Last edited: Oct 29, 2010
  19. streetfighter 2

    streetfighter 2 New Member

    Joined:
    Jul 26, 2010
    Messages:
    1,658 (1.07/day)
    Thanks Received:
    732
    Location:
    Philly
    I definitely think the "int" goes before "newEgg".

    You could try "public static int passedEggs(int newEgg)". I don't think it's gonna work though...

    In networking aren't you expecting to do lots of programming?
    http://msdn.microsoft.com/en-us/network/bb332030.aspx

    Java is a pretty simple language so it shouldn't be hard to get the ropes on it if you have experience in other languages.
     
  20. char[] rager New Member

    Joined:
    Jun 9, 2010
    Messages:
    310 (0.19/day)
    Thanks Received:
    26
    Location:
    Massachusetts Institute of Technology, Computer Sc
    I was thinking that too, replace the private with public or static.

    I don't know where he is. I think he is on that forum I gave him a link for...
     
  21. temp02 New Member

    Joined:
    Mar 18, 2009
    Messages:
    493 (0.24/day)
    Thanks Received:
    166
    By now this is probably not needed still here is a working version of the example provided:
    Code:
    import java.util.Scanner;
    
    public class Eggs
    {
    	public static void main(String[] args)
    	{	
    		Scanner input = new Scanner (System.in);
    		int eggs;
    			
    		System.out.println("Please enter in an integer: ");
    		eggs = input.nextInt();
    									
    		eggs = passedEggs(eggs);
                            
                    System.out.println(eggs);
    	}
    
    
    	private static int passedEggs(int eggs)
    	{
    		int equalDozen;
    		int remainderEggs;
    		int brokenEggs;
    		
    		equalDozen = eggs / 12;
    		remainderEggs = eggs % 12;
    
    		brokenEggs = remainderEggs / 6;
    		
    		return brokenEggs;
    	}
    }
    So the stuff you need to know:
    • Java always passes variables by value into functions and never by reference.
      The only way to get a modified variable from a function (besides passing the var as an object array) is to return a new value and use it, which is what is used in the code above.
    • You cannot refer a regular method from a static method:
      Don't ask me why, it's just the way Java works.

    Hope you understand the code.
     
    streetfighter 2 says thanks.
  22. Munki

    Munki

    Joined:
    Sep 13, 2008
    Messages:
    1,227 (0.55/day)
    Thanks Received:
    182
    Location:
    Metro Atlanta
    Ill check it out when I get home. I write all my programs in jGRASP. So ill let yall know.
     

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page