# Need Help on Java Programming - Weight on other planets



## bjc92113

Hi, I need help on an java assignment, here's what it needs to do:

Ask the user for weight value on Earth, and asked the user to choose planet and calculate their weight on that planet.

1. Mecury
2. Venus
~ for all planets in the solar system
Reply: ex. 70 kg on Earth is 40kg on the moon.
Ask for another weight
Then let them put in another weight and loop.
Quit the program when 0 is entered.
Complain if invalid number is entered.
Must use 'switch'(case) statements in code


Here's the code I have so far:

import java.util.*;
public class Planetweight
{
public static void main(String[]args)
{
boolean done = false; //boolean = is program done or not
int userChoice; //integer to hold user's choice
//two doubles used in the equation
double d1 , d2; //make multiple declarations with coma
Scanner inputReader = new Scanner(System.in);

System.out.print("Choose a planet to find out how much you weigh on that planet");

//give user choices
System.out.println("Choose from the following options:\n");
System.out.println("	1. Mercury"); //0.38
System.out.println("	2. Venus"); //0.91
System.out.println("	3. Earth"); //1.0
System.out.println("	4. Mars"); //0.38
System.out.println("	5. Jupiter"); //2.54
System.out.println("	6 Saturn."); //107
System.out.println("	7. Uranus"); //90
System.out.println("	8. Neptune"); //1.15
System.out.println("	9. Pluto"); //0.08

userChoice = inputReader.nextInt();

//ask the user for the two numbers
System.out.print("\n Enter your current weight on Earth in kilograms:");
d1 = inputReader.nextDouble();

System.out.print(d1 + "kg on Earth is" );
}
}

the numbers after the // is the comparison of the gravitational pull on that planet compared to Earth's.

any input would be appreciated


----------



## dm01

Which part do you need help with? Are you just plain stuck, or do you have some idea where you need to go with this?


----------



## bjc92113

program is supposed to calculate the weight of the person on a different planet and respond in this format:
" 'x' kg on Earth is equal to 'y' kg on (selected planet)"

I'm stuck on how to put the name of the planet selected by the user into the response..


----------



## dm01

Looks like you're going to need a string for the planet name.

String planetname = null;

You only need one switch to make this work. Read http://java.sun.com/docs/books/tutorial/java/nutsandbolts/switch.html for a very good explanation of how the switch statement works.

I'll let you figure out the rest on your own. If you have any questions, please post back. This particular assignment has intrigued me, so I decided to build one as well . The finicky validation and repeats stymied me, but I think I got it now.


----------



## bjc92113

Here's my current Code now:

import java.util.*;
public class Planetweight
{
public static void main(String[]args)
{
boolean done = false; //boolean = is program done or not
int userChoice; //integer to hold user's choice
int i = 0;
//two doubles used in the equation
double d1; //declare d1 as a double
Scanner inputReader = new Scanner(System.in);

System.out.print("Choose a planet to find out how much you weigh on that planet.\n");

//give user choices
System.out.println("Choose from the following options:\nType 0 to quit.\n");
System.out.println("	1. Mercury"); //0.38
System.out.println("	2. Venus"); //0.91
System.out.println("	3. Earth"); //1.0
System.out.println("	4. Mars"); //0.38
System.out.println("	5. Jupiter"); //2.54
System.out.println("	6 Saturn."); //1.07
System.out.println("	7. Uranus"); //0.9
System.out.println("	8. Neptune"); //1.15
System.out.println("	9. Pluto"); //0.08
System.out.println("	10. Moon"); //0.17

userChoice = inputReader.nextInt();

//ask the user for the two numbers
System.out.print("\n Enter your current weight on Earth in kilograms:");
d1 = inputReader.nextDouble();

while (!done)
{
if (i==0)
{
System.out.println("Goodbye.");
done = true;
}
else if (!(i==0))	



switch(userChoice)
{
case 1: 
System.out.println(d1 + "kg on Earth is " + d1*0.38 + "kg on Mercury.");
break; //tells system to break out

case 2: 
System.out.println(d1 + "kg on Earth is " + d1*0.91 + "kg on Venus.");
break;

case 3: 
System.out.println(d1 + "kg on Earth is " + d1*1.0 + "kg on Earth.");
break;

case 4: 
System.out.println(d1 + "kg on Earth is " + d1*0.38 + "kg on Mars.");
break;

case 5: 
System.out.println(d1 + "kg on Earth is " + d1*2.54 + "kg on Jupiter.");
break;

case 6: 
System.out.println(d1 + "kg on Earth is " + d1*1.07 + "kg on Saturn.");
break;

case 7: 
System.out.println(d1 + "kg on Earth is " + d1*0.9 + "kg on Uranus.");
break;

case 8: 
System.out.println(d1 + "kg on Earth is " + d1*1.15 + "kg on Neptune.");
break;

case 9: 
System.out.println(d1 + "kg on Earth is " + d1*0.8 + "kg on Pluto.");
break;

case 10: 
System.out.println(d1 + "kg on Earth is " + d1*0.17 + "kg on the Moon.");
break;

default: //user picked a bogus value
System.out.println("invalid input");
break;
}

}


}
}

but I'm having problems getting the program to loop using WhileLoops
btw, also needs to loop when user enters a invalid input


----------



## dm01

For validation, you are going to need several boolean variables and while statements. You could use do while statements, but they are not considered good practice. The while loop operates on a condition, just like any other loop statement. This condition can be as simple as x==1 or planet_Flag==false; more complex conditions can be created using operators, but this is unnecessary.

while statements are fairly simple to implement singly. Once you get into stacked loop statements, things become tricky.


Code:


byte x == 1;

while (x==1)
{
[INDENT]x=2;
while (x==2)
{
[INDENT]x=3
while (x==1)
{
[INDENT]x=4
while (x==4)
{
[INDENT]x=16[/INDENT]
}
[/INDENT]}
while (x==3)
x=6
[/INDENT]}
[/INDENT]}

What number is x assigned to? x==6

As always, if you have any questions, post back


----------



## bjc92113

The code above is mainly composed of while loops, but I'm required by my teacher to use switchcases, so here's what I tried:

/*
Ask the user for weight value on Earth, and asked the user to choose planet and calculate their weight on that planet.

1. Mecury
2. Venus
~ for all planets in the solar system

Reply: ex. 70 kg on Earth is 40kg on the moon.

Ask for another weight
Then let them put in another weight and loop.
Quit the program when 0 is entered.

Complain if invalid number is entered.

Must use 'switch'(case) statements in code

Due: next Friday

*/
import java.util.*;
public class Planetweight
{
public static void main(String[]args)
{
boolean done = false; //boolean = is program done or not
int userChoice; //integer to hold user's choice
//two doubles used in the equation
double d1; //declare d1 as a double
Scanner inputReader = new Scanner(System.in);

System.out.print("Choose a planet to find out how much you weigh on that planet.\nType 0 to quit.\n \n");

//give user choices
System.out.println("	1. Mercury"); //0.38
System.out.println("	2. Venus"); //0.91
System.out.println("	3. Earth"); //1.0
System.out.println("	4. Mars"); //0.38
System.out.println("	5. Jupiter"); //2.54
System.out.println("	6 Saturn."); //1.07
System.out.println("	7. Uranus"); //0.9
System.out.println("	8. Neptune"); //1.15
System.out.println("	9. Pluto"); //0.08
System.out.println("	10. Moon"); //0.17

userChoice = inputReader.nextInt();



while (userChoice = inputReader.nextInt()) //if the user did not type 0, the calculator will begin
{

//ask the user for weight
System.out.print("\n Enter your current weight on Earth in kilograms:");
d1 = inputReader.nextDouble();

switch(userChoice) //takes the case chooses the action to perform
{
case 1: 
System.out.println(d1 + "kg on Earth is " + d1*0.38 + "kg on Mercury.");
break; //tells system to break out

case 2: 
System.out.println(d1 + "kg on Earth is " + d1*0.91 + "kg on Venus.");
break;

case 3: 
System.out.println(d1 + "kg on Earth is " + d1*1.0 + "kg on Earth.");
break;

case 4: 
System.out.println(d1 + "kg on Earth is " + d1*0.38 + "kg on Mars.");
break;

case 5: 
System.out.println(d1 + "kg on Earth is " + d1*2.54 + "kg on Jupiter.");
break;

case 6: 
System.out.println(d1 + "kg on Earth is " + d1*1.07 + "kg on Saturn.");
break;

case 7: 
System.out.println(d1 + "kg on Earth is " + d1*0.9 + "kg on Uranus.");
break;

case 8: 
System.out.println(d1 + "kg on Earth is " + d1*1.15 + "kg on Neptune.");
break;

case 9: 
System.out.println(d1 + "kg on Earth is " + d1*0.8 + "kg on Pluto.");
break;

case 10: 
System.out.println(d1 + "kg on Earth is " + d1*0.17 + "kg on the Moon.");
break;

case 0:
System.out.println("Goodbye!");
System.exit(0);
break;

default: //user picked a bogus value
System.out.println("Pick a valid number, retard");
break;
}

}
if (userChoice==0) //tells the user program is done
{
System.out.println("Goodbye!!");
}


}
}

but when I compile this, I get this error:

---------- Capture Output ----------
> "C:\Program Files\Java\jdk1.6.0_16\bin\javac.exe" Planetweight.java
Planetweight.java:50: incompatible types
found : int
required: boolean
while (userChoice = inputReader.nextInt()) //if the user did not type 0, the calculator will begin
^
1 error
> Terminated with exit code 1.

but I don't know how to change it into boolean, cuz whatever I change will only return me with more errors..


----------



## dm01

Code:


userChoice = inputReader.nextInt();

while (userChoice = inputReader.nextInt())

This reads as: "the integer that the user entered is assigned to userChoice. While the integer that the user entered is assigned to userChoice, the statements in the squirly brackets underneath me may execute." Although this looks legal in English, it is in fact an impossible condition, and the compiler is very confused as a result.

Operating conditions almost invariably consist of a variable, one or more operands, and a number or other variable(s). In this case, you want userChoice to be greater than or equal to zero. I would initialize userChoice to -1 at the start of each iteration to clear this condition.



Code:


while (userChoice >= 0)

You don't really need this if you have a case 0:


Code:


if (userChoice==0) //tells the user program is done
{
System.out.println("Goodbye!!");
}

Just to help clean things up a bit.

Good luck, and remember: sometimes the compiler does not have the best solution.

PS: insulting the user will not win you any favours with the instructor. I learned this the hard way :S.


----------



## bjc92113

thx for the helps, I managed to get the code to work. :wave:


----------



## dm01

Glad to hear it. Drop by any time if you're having trouble.


----------

