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

Java: Passing variables

Joined
Sep 13, 2008
Messages
1,230 (0.22/day)
Location
Metro Atlanta
Processor AMD Ryzen 1700
Motherboard Gigabyte AB350 GAMING 3
Memory 16GB (2x8) 3200MHz
Display(s) Acer 24" LCD
Software Windows 10 Pro
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.
 

char[] rager

New Member
Joined
Jun 9, 2010
Messages
310 (0.06/day)
Location
Massachusetts Institute of Technology, Computer Sc
System Name Obsidianight
Processor Intel Core i7 950 @ ~ 4 GHz
Motherboard Asus P6T Deluxe
Cooling Custom Liquid Cooling
Memory 6GB (3 x 2GB) Corsair XMS3 DDR3 Triple-Channel @ ~ 1.6 GHz (9-9-9-24-1T)
Video Card(s) Zotac AMP! GTX 580 @ 900 MHz Core / 1025 MHz Memory / 1800 MHz Shader
Storage 180 GB OCZ Vertex 2 SSD
Display(s) Sceptre 24 Inch (1920 x 1200) 2 MS Response
Case Corsair Obsidian 800D
Audio Device(s) Onboard
Power Supply 1 kW Antec TruePower Quattro
Software Microsoft Windows 7 Ultimate 64-Bit / Linux Mint 9 And Fedora 13 KDE Through VirtualBox
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.
 
Joined
Mar 8, 2006
Messages
498 (0.08/day)
Processor Celeron 430 (Conroe-L)
Motherboard ASUS Rampage Formula
Cooling Water, Zalman Reserator XT
Memory Corsair Dominator PC2-8500 DDR2-1000
Video Card(s) Visiontek 2600 PRO
Storage Maxtor Maxline 3, more Maxtors
Display(s) Acer P243w 24"
Case Aspire X-Navigator ATXA9N-BK
Audio Device(s) Mbox
Power Supply Thermaltake Toughpower 750w
Software Protools and a bunch of racing games...
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

New Member
Joined
Jun 9, 2010
Messages
310 (0.06/day)
Location
Massachusetts Institute of Technology, Computer Sc
System Name Obsidianight
Processor Intel Core i7 950 @ ~ 4 GHz
Motherboard Asus P6T Deluxe
Cooling Custom Liquid Cooling
Memory 6GB (3 x 2GB) Corsair XMS3 DDR3 Triple-Channel @ ~ 1.6 GHz (9-9-9-24-1T)
Video Card(s) Zotac AMP! GTX 580 @ 900 MHz Core / 1025 MHz Memory / 1800 MHz Shader
Storage 180 GB OCZ Vertex 2 SSD
Display(s) Sceptre 24 Inch (1920 x 1200) 2 MS Response
Case Corsair Obsidian 800D
Audio Device(s) Onboard
Power Supply 1 kW Antec TruePower Quattro
Software Microsoft Windows 7 Ultimate 64-Bit / Linux Mint 9 And Fedora 13 KDE Through VirtualBox
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:
Joined
Mar 8, 2006
Messages
498 (0.08/day)
Processor Celeron 430 (Conroe-L)
Motherboard ASUS Rampage Formula
Cooling Water, Zalman Reserator XT
Memory Corsair Dominator PC2-8500 DDR2-1000
Video Card(s) Visiontek 2600 PRO
Storage Maxtor Maxline 3, more Maxtors
Display(s) Acer P243w 24"
Case Aspire X-Navigator ATXA9N-BK
Audio Device(s) Mbox
Power Supply Thermaltake Toughpower 750w
Software Protools and a bunch of racing games...
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.
 
Joined
Sep 13, 2008
Messages
1,230 (0.22/day)
Location
Metro Atlanta
Processor AMD Ryzen 1700
Motherboard Gigabyte AB350 GAMING 3
Memory 16GB (2x8) 3200MHz
Display(s) Acer 24" LCD
Software Windows 10 Pro
Thanks for trying guys, but apparently C# isnt close enough. Its not compiling. its not seeing eggs from the main method/
 

char[] rager

New Member
Joined
Jun 9, 2010
Messages
310 (0.06/day)
Location
Massachusetts Institute of Technology, Computer Sc
System Name Obsidianight
Processor Intel Core i7 950 @ ~ 4 GHz
Motherboard Asus P6T Deluxe
Cooling Custom Liquid Cooling
Memory 6GB (3 x 2GB) Corsair XMS3 DDR3 Triple-Channel @ ~ 1.6 GHz (9-9-9-24-1T)
Video Card(s) Zotac AMP! GTX 580 @ 900 MHz Core / 1025 MHz Memory / 1800 MHz Shader
Storage 180 GB OCZ Vertex 2 SSD
Display(s) Sceptre 24 Inch (1920 x 1200) 2 MS Response
Case Corsair Obsidian 800D
Audio Device(s) Onboard
Power Supply 1 kW Antec TruePower Quattro
Software Microsoft Windows 7 Ultimate 64-Bit / Linux Mint 9 And Fedora 13 KDE Through VirtualBox
I am sorry, it should be int before newEgg in the method signature.

Like: private int passedEggs(int newEgg)
 
Joined
Sep 13, 2008
Messages
1,230 (0.22/day)
Location
Metro Atlanta
Processor AMD Ryzen 1700
Motherboard Gigabyte AB350 GAMING 3
Memory 16GB (2x8) 3200MHz
Display(s) Acer 24" LCD
Software Windows 10 Pro
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

New Member
Joined
Jun 9, 2010
Messages
310 (0.06/day)
Location
Massachusetts Institute of Technology, Computer Sc
System Name Obsidianight
Processor Intel Core i7 950 @ ~ 4 GHz
Motherboard Asus P6T Deluxe
Cooling Custom Liquid Cooling
Memory 6GB (3 x 2GB) Corsair XMS3 DDR3 Triple-Channel @ ~ 1.6 GHz (9-9-9-24-1T)
Video Card(s) Zotac AMP! GTX 580 @ 900 MHz Core / 1025 MHz Memory / 1800 MHz Shader
Storage 180 GB OCZ Vertex 2 SSD
Display(s) Sceptre 24 Inch (1920 x 1200) 2 MS Response
Case Corsair Obsidian 800D
Audio Device(s) Onboard
Power Supply 1 kW Antec TruePower Quattro
Software Microsoft Windows 7 Ultimate 64-Bit / Linux Mint 9 And Fedora 13 KDE Through VirtualBox
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:
Joined
Sep 13, 2008
Messages
1,230 (0.22/day)
Location
Metro Atlanta
Processor AMD Ryzen 1700
Motherboard Gigabyte AB350 GAMING 3
Memory 16GB (2x8) 3200MHz
Display(s) Acer 24" LCD
Software Windows 10 Pro
test.java:16: <identifier> expected
private int passedEggs(newEgg)
^
1 error

C&P it
 

char[] rager

New Member
Joined
Jun 9, 2010
Messages
310 (0.06/day)
Location
Massachusetts Institute of Technology, Computer Sc
System Name Obsidianight
Processor Intel Core i7 950 @ ~ 4 GHz
Motherboard Asus P6T Deluxe
Cooling Custom Liquid Cooling
Memory 6GB (3 x 2GB) Corsair XMS3 DDR3 Triple-Channel @ ~ 1.6 GHz (9-9-9-24-1T)
Video Card(s) Zotac AMP! GTX 580 @ 900 MHz Core / 1025 MHz Memory / 1800 MHz Shader
Storage 180 GB OCZ Vertex 2 SSD
Display(s) Sceptre 24 Inch (1920 x 1200) 2 MS Response
Case Corsair Obsidian 800D
Audio Device(s) Onboard
Power Supply 1 kW Antec TruePower Quattro
Software Microsoft Windows 7 Ultimate 64-Bit / Linux Mint 9 And Fedora 13 KDE Through VirtualBox
Okay, put int infront of newEgg again.
 
Joined
Sep 13, 2008
Messages
1,230 (0.22/day)
Location
Metro Atlanta
Processor AMD Ryzen 1700
Motherboard Gigabyte AB350 GAMING 3
Memory 16GB (2x8) 3200MHz
Display(s) Acer 24" LCD
Software Windows 10 Pro
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

New Member
Joined
Jun 9, 2010
Messages
310 (0.06/day)
Location
Massachusetts Institute of Technology, Computer Sc
System Name Obsidianight
Processor Intel Core i7 950 @ ~ 4 GHz
Motherboard Asus P6T Deluxe
Cooling Custom Liquid Cooling
Memory 6GB (3 x 2GB) Corsair XMS3 DDR3 Triple-Channel @ ~ 1.6 GHz (9-9-9-24-1T)
Video Card(s) Zotac AMP! GTX 580 @ 900 MHz Core / 1025 MHz Memory / 1800 MHz Shader
Storage 180 GB OCZ Vertex 2 SSD
Display(s) Sceptre 24 Inch (1920 x 1200) 2 MS Response
Case Corsair Obsidian 800D
Audio Device(s) Onboard
Power Supply 1 kW Antec TruePower Quattro
Software Microsoft Windows 7 Ultimate 64-Bit / Linux Mint 9 And Fedora 13 KDE Through VirtualBox
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.
 
Joined
Sep 13, 2008
Messages
1,230 (0.22/day)
Location
Metro Atlanta
Processor AMD Ryzen 1700
Motherboard Gigabyte AB350 GAMING 3
Memory 16GB (2x8) 3200MHz
Display(s) Acer 24" LCD
Software Windows 10 Pro
Im in school for networking, ill never understand why the hell I need this class anyways :p
 

char[] rager

New Member
Joined
Jun 9, 2010
Messages
310 (0.06/day)
Location
Massachusetts Institute of Technology, Computer Sc
System Name Obsidianight
Processor Intel Core i7 950 @ ~ 4 GHz
Motherboard Asus P6T Deluxe
Cooling Custom Liquid Cooling
Memory 6GB (3 x 2GB) Corsair XMS3 DDR3 Triple-Channel @ ~ 1.6 GHz (9-9-9-24-1T)
Video Card(s) Zotac AMP! GTX 580 @ 900 MHz Core / 1025 MHz Memory / 1800 MHz Shader
Storage 180 GB OCZ Vertex 2 SSD
Display(s) Sceptre 24 Inch (1920 x 1200) 2 MS Response
Case Corsair Obsidian 800D
Audio Device(s) Onboard
Power Supply 1 kW Antec TruePower Quattro
Software Microsoft Windows 7 Ultimate 64-Bit / Linux Mint 9 And Fedora 13 KDE Through VirtualBox

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,444 (2.43/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software The Matrix
you need fordgtconcept to help you. he knows everything about java.
 

WarEagleAU

Bird of Prey
Joined
Jul 9, 2006
Messages
10,812 (1.67/day)
Location
Gurley, AL
System Name Pandemic 2020
Processor AMD Ryzen 5 "Gen 2" 2600X
Motherboard AsRock X470 Killer Promontory
Cooling CoolerMaster 240 RGB Master Cooler (Newegg Eggxpert)
Memory 32 GB Geil EVO Portenza DDR4 3200 MHz
Video Card(s) ASUS Radeon RX 580 DirectX 12 DUAL-RX580-O8G 8GB 256-Bit GDDR5 HDCP Ready CrossFireX Support Video C
Storage WD 250 M.2, Corsair P500 M.2, OCZ Trion 500, WD Black 1TB, Assorted others.
Display(s) ASUS MG24UQ Gaming Monitor - 23.6" 4K UHD (3840x2160) , IPS, Adaptive Sync, DisplayWidget
Case Fractal Define R6 C
Audio Device(s) Realtek 5.1 Onboard
Power Supply Corsair RMX 850 Platinum PSU (Newegg Eggxpert)
Mouse Razer Death Adder
Keyboard Corsair K95 Mechanical & Corsair K65 Wired, Wireless, Bluetooth)
Software Windows 10 Pro x64
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

New Member
Joined
Jun 9, 2010
Messages
310 (0.06/day)
Location
Massachusetts Institute of Technology, Computer Sc
System Name Obsidianight
Processor Intel Core i7 950 @ ~ 4 GHz
Motherboard Asus P6T Deluxe
Cooling Custom Liquid Cooling
Memory 6GB (3 x 2GB) Corsair XMS3 DDR3 Triple-Channel @ ~ 1.6 GHz (9-9-9-24-1T)
Video Card(s) Zotac AMP! GTX 580 @ 900 MHz Core / 1025 MHz Memory / 1800 MHz Shader
Storage 180 GB OCZ Vertex 2 SSD
Display(s) Sceptre 24 Inch (1920 x 1200) 2 MS Response
Case Corsair Obsidian 800D
Audio Device(s) Onboard
Power Supply 1 kW Antec TruePower Quattro
Software Microsoft Windows 7 Ultimate 64-Bit / Linux Mint 9 And Fedora 13 KDE Through VirtualBox
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:

streetfighter 2

New Member
Joined
Jul 26, 2010
Messages
1,655 (0.33/day)
Location
Philly
test.java:16: <identifier> expected
private int passedEggs(newEgg)
^
1 error

C&P it

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

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

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

New Member
Joined
Jun 9, 2010
Messages
310 (0.06/day)
Location
Massachusetts Institute of Technology, Computer Sc
System Name Obsidianight
Processor Intel Core i7 950 @ ~ 4 GHz
Motherboard Asus P6T Deluxe
Cooling Custom Liquid Cooling
Memory 6GB (3 x 2GB) Corsair XMS3 DDR3 Triple-Channel @ ~ 1.6 GHz (9-9-9-24-1T)
Video Card(s) Zotac AMP! GTX 580 @ 900 MHz Core / 1025 MHz Memory / 1800 MHz Shader
Storage 180 GB OCZ Vertex 2 SSD
Display(s) Sceptre 24 Inch (1920 x 1200) 2 MS Response
Case Corsair Obsidian 800D
Audio Device(s) Onboard
Power Supply 1 kW Antec TruePower Quattro
Software Microsoft Windows 7 Ultimate 64-Bit / Linux Mint 9 And Fedora 13 KDE Through VirtualBox
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

New Member
Joined
Mar 18, 2009
Messages
493 (0.09/day)
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.
 
Joined
Sep 13, 2008
Messages
1,230 (0.22/day)
Location
Metro Atlanta
Processor AMD Ryzen 1700
Motherboard Gigabyte AB350 GAMING 3
Memory 16GB (2x8) 3200MHz
Display(s) Acer 24" LCD
Software Windows 10 Pro
Ill check it out when I get home. I write all my programs in jGRASP. So ill let yall know.
 
Top