# Java: Passing variables



## Munki (Oct 28, 2010)

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).

```
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 
Thanks guys.


----------



## char[] rager (Oct 28, 2010)

Warning:  I don't know Java.

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

I believe 



> private int passedEggs()



should say



> private int passedEggs(int newEggs)



Second of all



> passedEggs(eggs)



is not being stored to anything.


----------



## spy2520 (Oct 28, 2010)

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.


----------



## char[] rager (Oct 28, 2010)

```
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;		
	}		
}
```


----------



## spy2520 (Oct 29, 2010)

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.


----------



## Munki (Oct 29, 2010)

Thanks for trying guys, but apparently C# isnt close enough. Its not compiling. its not seeing eggs from the main method/


----------



## char[] rager (Oct 29, 2010)

I am sorry, it should be int before newEgg in the method signature.

Like:  private int passedEggs(int newEgg)


----------



## Munki (Oct 29, 2010)

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)


----------



## char[] rager (Oct 29, 2010)

Well, I just taught myself some java from an online tutorial.

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


```
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;		
	}		
}
```


----------



## Munki (Oct 29, 2010)

test.java:16: <identifier> expected
	private int passedEggs(newEgg)
	                             ^
1 error

C&P it


----------



## char[] rager (Oct 29, 2010)

Okay, put int infront of newEgg again.


----------



## Munki (Oct 29, 2010)

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.


----------



## char[] rager (Oct 29, 2010)

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.


----------



## Munki (Oct 29, 2010)

Im in school for networking, ill never understand why the hell I need this class anyways


----------



## char[] rager (Oct 29, 2010)

I found a link for you to post your problem on. They will most likely be able to assist you.

http://www.javaprogrammingforums.com/


----------



## Easy Rhino (Oct 29, 2010)

you need fordgtconcept to help you. he knows everything about java.


----------



## WarEagleAU (Oct 29, 2010)

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


----------



## char[] rager (Oct 29, 2010)

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!


----------



## streetfighter 2 (Oct 29, 2010)

Munki said:


> test.java:16: <identifier> expected
> private int passedEggs(newEgg)
> ^
> 1 error
> ...



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...



Munki said:


> Im in school for networking, ill never understand why the hell I need this class anyways



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.


----------



## char[] rager (Oct 29, 2010)

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...


----------



## temp02 (Oct 29, 2010)

By now this is probably  not needed still here is a working version of the example provided:

```
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.


----------



## Munki (Oct 29, 2010)

Ill check it out when I get home. I write all my programs in jGRASP. So ill let yall know.


----------

