# Java (energy conversion code)



## jacy (Jul 19, 2004)

Hi,
This is what i have to do.

1. Convert BTUs to joules.
If this option is selected, the user will be prompted for the number of BTUs to convert to joules. It will multiply this input number by 1,056 and print out a message giving the number of joules. It will then prompt the user for more input.

2. Convert Calories to joules.
If this option is selected, the user will be prompted for the number of Calories to convert to joules. It will multiply this input number by 4.184 and print out a message giving the number of joules. It will then prompt the user for more input.

3. Convert joules to joules.
If this option is selected, the user will be prompted for the number of joules to convert to joules. It will multiply this input number by 1 and print out a message giving the number of joules. It will then prompt the user for more input.

4. Exit the program.
This option will exit the program. The program should not exit until the user has selected this option.

Note that if the user types anything other than options 1, 2, 3, or 4, the program should print an error message, and prompt the user for more input.

And this is what i came up with, but i am not getting the desired output. Please help, thanks.

[\CODE]

import java.util.*;

public class energyconversion {

public static void main(String[]args) {

Scanner stdin=new Scanner(System.in);
char reply = 'n';

boolean stilldeciding = true;
do {

System.out.print("Enter a choice between 1 and 4: ");
int choice = stdin.nextInt();
if (choice>=1 && choice<=4) {
switch (choice) {
case 1:
System.out.print("Enter the number of BTUs ");
double btu = stdin.nextDouble();
double joules = btu * 1056;
System.out.println("joules is " + joules);
break;
case 2:
System.out.print("Enter the number of calories ");
double calories = stdin.nextDouble();
double joule = calories * 4.184;
System.out.println("joules is " + joule);
break; 
case 3:
System.out.print("Enter the number of joules ");
double joule1 = stdin.nextDouble();
double joule2 = joule1 * 1;
System.out.println("joules is "+ joule2);
break; 
case 4:
System.out.println("Program will exit");
break;
}
}
else {
System.out.println("Invalid input");
}

System.out.print("Decision (y,n): ");
if (stdin.hasNext()) {
String response = stdin.nextLine();
if (response.length() == 1) {
reply = response.charAt(0);
reply = Character.toLowerCase(reply);
if ((reply =='y') || (reply == 'n')) {
stilldeciding = true;
}
}
}
else {
stilldeciding = false;
}
}while (stilldeciding);

}

}

[CODE\]


----------



## dquigley (Apr 17, 2006)

That is not a bad first try. You made it harder on yourself by trying to do everything inside the main loop. Functions that are used repeatedly are usually best and easier to use out on their own. I don't know why you have that "Decision" code there.

Here is one way to do it.

Best,
Dan


```
import java.util.Scanner;

public class energyconversion {

	public static void main(String[]args) {
	
		Scanner stdin = new Scanner(System.in);
		System.out.println("Engergy Converter Version 1.0");
		boolean stilldeciding = true;
		do { 
			System.out.println("1. Convert BTUs to Joules");
			System.out.println("2. Convert Calories to Joules");
			System.out.println("3. Convert Joules to Joules");
			System.out.println("4. Exit Converter");
			System.out.print("Please select (1-4): ");
			int choice = getInteger(stdin); 
			switch (choice) {
				case 1:
				{
					System.out.println("Enter the number of BTUs: ");
					double btu = getDouble(stdin);
					double joules = btu * 1056;
					System.out.println(btu + " BTU is " + joules + " Joules");
					break;
				}
				case 2:
				{
					System.out.println("Enter the number of calories ");
					double calories = getDouble(stdin);
					double joules = calories * 4.184;
					System.out.println(calories + " Calories is " + joules+ " Joules");
					break;
				}
				case 3:
				{
					System.out.println("Enter the number of joules ");
					double joules = stdin.nextDouble() * 1;
					System.out.println(joules + "Joules is "+ joules + " Joules");
					break;
				}
				case 4:
					System.out.println("Program will exit");
					stilldeciding = false;
					break;
				default:
					System.out.println("Incorrect selection");
					System.out.println("");
			}
			System.out.println("");
			
		}while (stilldeciding);
	
	}

	static int getInteger(Scanner s)
	{
		do {
		}while(!s.hasNextInt());
		return s.nextInt();
	}

	static double getDouble(Scanner s)
	{
		do {
		}while(!s.hasNextDouble());
		return s.nextDouble();
	}

}
```


----------



## jacy (Jul 19, 2004)

Thanks Dan i really appreciate your help. I am new to java so could you please tell me what does the last section of the code does. What books do u recommend for Java? Thanks Dan.


```
static int getInteger(Scanner s)
	{
		do {
		}while(!s.hasNextInt());
		return s.nextInt();
	}

	static double getDouble(Scanner s)
	{
		do {
		}while(!s.hasNextDouble());
		return s.nextDouble();
	}
```


----------



## dquigley (Apr 17, 2006)

Those two functions accept a Scanner object and in this program wait for a user to input either a valid integer or a valid double in the console. In the double case, for example, Scanner.hasNextDouble scans keyboard input and attempts to convert it to a double value, but does not remove it. If it can convert, it returns true, otherwise false. I used the ! (not) operator (which inverts false to true) in the while test so that the while loop continues until there is a valid entry.


```
static double getDouble(Scanner s)     // accepts a passed Scanner object
	{
		do {
		}while(!s.hasNextDouble()); // wait endlessly until until user enters a valid double value
		return s.nextDouble(); // return the double value
	}
```
I've never read a book on Java - I just use the documentation at Sun's website. Maybe someone else could recommend one.

Best,
Dan


----------



## jacy (Jul 19, 2004)

Thanks Dan, i really appreciate your effort.


----------

