I am trying to write some code to practice using methods. My goal is to get the user to input their weight. Then in the method I want to convert the earth weight to moon weight, which is earth weight / 6. I think I got it but I can get the results to come back to the main class. Any idea what I am missing here?

import java.util.Scanner;

public class EarthWeight {

	
	public static  void main(String[] args) {
		
		
		Scanner input = new Scanner(System.in);
		
		
		System.out.println("Lets find out what your weight on the moon is?");
		System.out.println("Enter your weight on earth here:");
		double earthweight = input.nextDouble();
		
		
		WeightConverter WeightConverterObject = new WeightConverter();
		WeightConverterObject.moonweight(moonweight);
		
		System.out.println("Your earth weight it " + earthweight);
	
		System.out.println("\nYour moon weight it " + moonweight);
		

	}
}

The second class, where the conversion takes place looks like this.

public class WeightConverter {
	public double moonweight(double earthweight, double moonweight){
	
		
		return moonweight = earthweight / 6;
		
	}
}

That is VB style. It doesn't work that way in java (that would be pass-by-reference which java is not.

you do methods like this

...
double d = SomeObject.someMethod();
...
//SomeObject someMethod method
public double someMethod() {
  return 1.0;
}
...

That is VB style. It doesn't work that way in java (that would be pass-by-reference which java is not.

you do methods like this

...
double d = SomeObject.someMethod();
...
//SomeObject someMethod method
public double someMethod() {
  return 1.0;
}
...

Thanks for the help but I am pretty new at this and dont exactly follow. This is the very first introduction to programing I have had. What is VB? Visual Basic? Is what you posted part for the main class and part for the method class? Should it be more something like this for the method class? will all variables I create be recognized automatically from one class to the next?

d = weight; 
weight = eweight / 6;
return d;

In your main method you want to define "moonwieght" so you need to do

moonweight = someMethodCall(earthweight)

and "someMethodCall" needs to return earthweight divided by 6. And someMethodCall, of course, needs to de declared to take an argument.

Give it a try and post your new code (complete).

commented: Thanks for your help. +1

Finally got it to work. Thanks for the help masijade.

import java.util.Scanner;

public class EarthWeight {

	
	public static void main(String[] args) {
		int moonweight, earthweight;
		
		Scanner input = new Scanner(System.in);
		WeightConverter WeightConverterObject = new WeightConverter();
		
		
		
		System.out.println("Lets find out what your weight on the moon is?");
		System.out.println("Enter your weight in lbs on earth here:");
		earthweight = input.nextInt();
		
		WeightConverterObject.moonweight(earthweight);
		moonweight = WeightConverterObject.moonweight(earthweight);
		
		System.out.println("Your weight on earth is: " + earthweight + " lbs.");
	
		System.out.println("Your weight on the moon is: " + moonweight + " lbs.");
		

	
	}

	
}

And the other class.

public class WeightConverter {
	
	public int moonweight(int earthweight){
				
		
		int moonweight = earthweight / 6;
		return moonweight;
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.