473,667 Members | 2,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Units conversion program

7 New Member
1. the program needs to convert btu to jules
2. Convert calories to joules
3. Convert joules to joules
4 exit the program.
if the user type anything other than 1-4 the program should print a error message.

here is my code.
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class joules {
  4.     public static void main (String [] args) {
  5.         final double BTUs = 1056;
  6.         final double calories = 4.184;
  7.         final double joule = 1;
  8.         Scanner stdin = new Scanner(System.in);
  9.         System.out.println ("1. Convert BTUs to joules");
  10.         System.out.println ("2. Convert Calories to joules");
  11.         System.out.println ("3. Convert joules to joules");
  12.         System.out.println ("4. Exit program");
  13.         int choice = stdin.nextInt();
  14.         while (choice != 4)
  15.             if (choice < 1 || choice > 3)
  16.             //converting to joules using constants
  17.             if (choice = 1) {
  18.             double amount = stdin.nextDouble();
  19.             double joules = amount * BTUs;
  20.         System.out.println (amount + " BTUs is " + joules  "joules");
  21.                 }
  22.         if (choice = 1) {double amount = stdin.nextDouble();
  23.             double joules = amount * calories;
  24.         System.out.println (amount + " Calories is " + joules + "joules");
  25.                 }
  26.             if (choice = 1) {
  27.             double amount = stdin.nextDouble();
  28.             double joules = amount * joule;
  29.             System.out.println (amount + " joules is " + joules + "joules");
  30.                 }
  31.             //statement for wrong entries
  32.             else {
  33.         System.out.println ("That is not a valid entry.  Try again");
  34.             }
  35.  
Feb 28 '07 #1
10 5720
horace1
1,510 Recognized Expert Top Contributor
what is your problem?
Feb 28 '07 #2
DeMan
1,806 Top Contributor
You do, of course realise that you only ever check for option 1 (and doesn't java require ==)
Feb 28 '07 #3
minterman
7 New Member
what is your problem?
I started writing the code to convert btu to joules. This code is not working for me
Feb 28 '07 #4
r035198x
13,262 MVP
1. the program needs to convert btu to jules
2. Convert calories to joules
3. Convert joules to joules
4 exit the program.
if the user type anything other than 1-4 the program should print a error message.

here is my code.
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class joules {
  4.     public static void main (String [] args) {
  5.         final double BTUs = 1056;
  6.         final double calories = 4.184;
  7.         final double joule = 1;
  8.         Scanner stdin = new Scanner(System.in);
  9.         System.out.println ("1. Convert BTUs to joules");
  10.         System.out.println ("2. Convert Calories to joules");
  11.         System.out.println ("3. Convert joules to joules");
  12.         System.out.println ("4. Exit program");
  13.         int choice = stdin.nextInt();
  14.         while (choice != 4)
  15.             if (choice < 1 || choice > 3)
  16.             //converting to joules using constants
  17.             if (choice = 1) {
  18.             double amount = stdin.nextDouble();
  19.             double joules = amount * BTUs;
  20.         System.out.println (amount + " BTUs is " + joules "joules");
  21.                 }
  22.         if (choice = 1) {double amount = stdin.nextDouble();
  23.             double joules = amount * calories;
  24.         System.out.println (amount + " Calories is " + joules + "joules");
  25.                 }
  26.             if (choice = 1) {
  27.             double amount = stdin.nextDouble();
  28.             double joules = amount * joule;
  29.             System.out.println (amount + " joules is " + joules + "joules");
  30.                 }
  31.             //statement for wrong entries
  32.             else {
  33.         System.out.println ("That is not a valid entry. Try again");
  34.             }
  35.  
You should be using a switch for this.

Expand|Select|Wrap|Line Numbers
  1.  int choice = stdin.nextInt(); 
  2. switch(choice) {
  3.        case 1: {
  4.  
  5.                break;
  6.         }
  7.         case 2: {
  8.  
  9.                break;
  10.         }
  11.         .
  12.         .
  13.         .
  14.        default : {
  15.         //wrong input 
  16.  
  17.        }
  18.  
What error did you get?
Mar 1 '07 #5
horace1
1,510 Recognized Expert Top Contributor
your logic in the following statement where you test if choice is between 1 and 3 inclusive is incorrect
Expand|Select|Wrap|Line Numbers
  1.         while (choice != 4)
  2.             if (choice < 1 || choice > 3)
  3.  
you test if choice is less than 1 or greater than 3
you need to test if choice is greater than or equal to 1 AND less than or equal to 3, e.g.
Expand|Select|Wrap|Line Numbers
  1.                 while (choice != 4)
  2.                         if (choice >= 1 && choice <= 3)
  3.  
also as DeMan indicated statements such as
Expand|Select|Wrap|Line Numbers
  1.             if (choice = 1) {
  2.  
should use ==
Expand|Select|Wrap|Line Numbers
  1.             if (choice == 1) {
  2.  
however, as indicated by r035198x, it would be better to use a switch statement
Mar 1 '07 #6
minterman
7 New Member
1. the program needs to convert btu to jules
2. Convert calories to joules
3. Convert joules to joules
4 exit the program.
if the user type anything other than 1-4 the program should print a error message.


I can not get this code to execute Please hellp
here is my code.
Code:

import java.util.*;
public class Project_2 {
// main(): application entry point
public static void main (String [] args) {
final double BTUs =1056;
final double calories =4.184;
final double joule =1;
int choice;
// set up input stream
Scanner stdin = new Scanner(System. in);
System.out.prin tln ("1. Convert BTUs to Joules");
System.out.prin tln ("2. Convert Calories to Joules");
System.out.prin tln ("3. Convert Joules to Joules");
System.out.prin tln ("4. Exit program");
int choice=stdin.ne xtInt();
//converting BTUS to Joules
switch (choice){
case 1:
double amount = stdin.nextDoubl e();
double joules = amount * BTUs;
System.out.prin tln (amount + "BTUs is " + joules + "joules");
break;
// converting Calories to Joules
case 2:
double joules = amount * calories;
System.out.prin tln (amount + "Calories is " + joules + "joules");
break;
// converting Joules to Joules
case 3:
double joules = amount * joule;
System.out.prin tln (amount + "joules is " + joules + "joules");
break;
// exit program
case 4:
System.exit(0)
break;
default:
System.out.prin tln ("You have entered an incorrect number, Please try again.");
}
}
}
Mar 2 '07 #7
r035198x
13,262 MVP
1. the program needs to convert btu to jules
2. Convert calories to joules
3. Convert joules to joules
4 exit the program.
if the user type anything other than 1-4 the program should print a error message.


I can not get this code to execute Please hellp
here is my code.
Code:

import java.util.*;
public class Project_2 {
// main(): application entry point
public static void main (String [] args) {
final double BTUs =1056;
final double calories =4.184;
final double joule =1;
int choice;
// set up input stream
Scanner stdin = new Scanner(System. in);
System.out.prin tln ("1. Convert BTUs to Joules");
System.out.prin tln ("2. Convert Calories to Joules");
System.out.prin tln ("3. Convert Joules to Joules");
System.out.prin tln ("4. Exit program");
int choice=stdin.ne xtInt();
//converting BTUS to Joules
switch (choice){
case 1:
double amount = stdin.nextDoubl e();
double joules = amount * BTUs;
System.out.prin tln (amount + "BTUs is " + joules + "joules");
break;
// converting Calories to Joules
case 2:
double joules = amount * calories;
System.out.prin tln (amount + "Calories is " + joules + "joules");
break;
// converting Joules to Joules
case 3:
double joules = amount * joule;
System.out.prin tln (amount + "joules is " + joules + "joules");
break;
// exit program
case 4:
System.exit(0)
break;
default:
System.out.prin tln ("You have entered an incorrect number, Please try again.");
}
}
}


Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.util.*;
  3. public class Project_2 {
  4. // main(): application entry point
  5.  public static void main (String [] args) {
  6.   final double BTUs =1056;
  7.   final double calories =4.184;
  8.   final double joule =1;
  9.   // set up input stream
  10.   Scanner stdin = new Scanner(System.in);
  11.   System.out.println ("1. Convert BTUs to Joules");
  12.   System.out.println ("2. Convert Calories to Joules");
  13.   System.out.println ("3. Convert Joules to Joules");
  14.   System.out.println ("4. Exit program");
  15.   int choice=stdin.nextInt();
  16.   //converting BTUS to Joules
  17.   switch (choice){
  18.    case 1: {
  19.     System.out.println ("Enter the BTUs");
  20.     double amount = stdin.nextDouble();
  21.     double joules = amount * BTUs;
  22.     System.out.println (amount + "BTUs is " + joules + "joules");
  23.     break;
  24.    }
  25.    // converting Calories to Joules
  26.    case 2: {
  27.     System.out.println ("Enter the Calories");
  28.     double amount = stdin.nextDouble();
  29.     double joules = amount * calories;
  30.     System.out.println (amount + "Calories is " + joules + "joules");
  31.     break;
  32.    }
  33.    // converting Joules to Joules
  34.    case 3: {
  35.     System.out.println ("Enter the Joules");
  36.     double amount = stdin.nextDouble();
  37.     double joules = amount * joule;
  38.     System.out.println (amount + "joules is " + joules + "joules");
  39.     break;
  40.    }
  41.    // exit program
  42.    case 4: {
  43.     System.exit(0);
  44.    }
  45.    default: {
  46.     System.out.println ("You have entered an incorrect number, Please try again.");
  47.    }
  48.   }
  49.  }
  50. }
  51.  
  52.  
You were missing declarations for amount in the other case statements. Now make it possible to make many conversions before the program exits. Currently you can do only one conversion and then the program exits. You will need to use a while loop.
Mar 2 '07 #8
minterman
7 New Member
Thanks a Lot this got me on the right track. I added a while (true){
to get the program to keep looping but I want to make it pause after each calulations. How do I do that.

Thanks
Minterman
Mar 2 '07 #9
r035198x
13,262 MVP
Thanks a Lot this got me on the right track. I added a while (true){
to get the program to keep looping but I want to make it pause after each calulations. How do I do that.

Thanks
Minterman
How do you want it to pause? Pause until the user does something or pause for a specific period of time?
Mar 3 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

7
5104
by: Steven T. Hatton | last post by:
Is there anything that gives a good description of how source code is converted into a translation unit, then object code, and then linked. I'm particularly interested in understanding why putting normal functions in header files results in multiple definition errors even when include guards are used. -- STH Hatton's Law: "There is only One inviolable Law" KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com Mozilla:...
7
3256
by: Michael Lehn | last post by:
Hi, I have a question regarding the conversion of objects. When is the conversion done by the constructor and when by the operator. My feeling tells me that the constructor is preferred. But I couldn't find the exact rule in the C++ standard. And what if the classes have template parameters? It would be great if somebody could get me a rough hint where in the
31
6620
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one variable becomes 364 and the other one becomes 365. Does anyone have any insight to what the problem is? Thanks in advance. Bjørn
0
3338
by: Martin | last post by:
When I convert a program from VC6 to VC7 I get the following linker error: CVTRES : fatal error CVT1100: duplicate resource. type:STRING, name:1686, language:0x0409 LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt Duplicate resource...no problem! Except that I cannot find that value anywhere!! No such string resource
5
2217
by: Javaman59 | last post by:
I just saw an interesting identifier in a C# book I'm reading... double dollarsPerHead This jumped out at me, as it is not common to put units in an identifer. We don't see "millisecondsSinceStart" ("time"), or "milesPerHour" ("speed"). But, perhaps it's a good idea. If memory serves me right, one of the Mars landers crashed because two development teams used different units (metric and imperial), across an interface. Also, when I'm...
2
1567
by: TheSeeker | last post by:
Hi, As part of a larger project, I am trying to use the GNU Units program to provide unit conversions between quantities. My first iteration, which worked OK, was to simply use units as a command-line app, and capture its output. The larger program however, calls the conversion routine many times, and the overhead of starting units for every call made things too slow. I then re-formulated my use of units to open it using popen2, and do
5
3656
by: dhruba.bandopadhyay | last post by:
I am trying to port an old Pascal DOS game to DOS C/C++. I am wondering if anyone is familar with the dos & crt Pascal units and whether there are C/C++ equivalent libraries. Maybe dos.c & crt.c? Below lists names of variables, functions, types & weird interrupt procedures found in Pascal. Am wondering what can be done to get around them for use in C/C++. dos.pas crt.pas
4
5728
by: beatdream | last post by:
I am designing a database to handle different kinds of products ... and these products can have different properties...for example, a trouser can be specified by the width, length, color, and other additional properties...while you might need only one property for another product, e.g litres for a soft drink...so I was thinking of creating a properties table, a units table and a product table, properties-unit table... and...
18
1832
by: kwikius | last post by:
Well lads :-)... looking at your Boost Units library the impression I get is that you got your library into boost Before writing anything much apart from some documentation, which was AFAICS even noted and allowed for in the review managers preamble. Nice work boost ... :-) Why the library was moved to the top of the review queue and accepted in such an unfinished state has I conjecture a lot to do with... hmm ... the BoostCon circus? ...
0
8457
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8365
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8883
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8646
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4200
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1778
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.