There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
blue screen blue screen of death boot computer connection cpu crash css dell display driver drivers email error ethernet excel firefox firefox 3 game hard drive hardware internet internet explorer itunes laptop malware monitor network networking nvidia outlook outlook 2003 outlook express partition printer problem problems router security slow software sound trojan usb video virus vista windows windows xp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Java (energy conversion code)


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
jacy's Avatar
Senior Member with 113 posts.
 
Join Date: Jul 2004
Experience: Beginner
20-Oct-2006, 10:58 PM #1
Java (energy conversion code)
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\]

Last edited by jacy : 20-Oct-2006 11:19 PM.
dquigley's Avatar
Computer Specs
Senior Member with 112 posts.
 
Join Date: Apr 2006
Location: Woodinville, WA
Experience: Advanced
21-Oct-2006, 05:44 PM #2
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

Code:
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's Avatar
Senior Member with 113 posts.
 
Join Date: Jul 2004
Experience: Beginner
21-Oct-2006, 10:43 PM #3
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.

Code:
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's Avatar
Computer Specs
Senior Member with 112 posts.
 
Join Date: Apr 2006
Location: Woodinville, WA
Experience: Advanced
21-Oct-2006, 11:44 PM #4
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.


Code:
	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's Avatar
Senior Member with 113 posts.
 
Join Date: Jul 2004
Experience: Beginner
22-Oct-2006, 12:45 AM #5
Thanks Dan, i really appreciate your effort.
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 03:43 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.