473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Units conversion program

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 5691
horace1
1,510 Expert 1GB
what is your problem?
Feb 28 '07 #2
DeMan
1,806 1GB
You do, of course realise that you only ever check for option 1 (and doesn't java require ==)
Feb 28 '07 #3
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 8TB
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 Expert 1GB
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
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.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 program");
int choice=stdin.nextInt();
//converting BTUS to Joules
switch (choice){
case 1:
double amount = stdin.nextDouble();
double joules = amount * BTUs;
System.out.println (amount + "BTUs is " + joules + "joules");
break;
// converting Calories to Joules
case 2:
double joules = amount * calories;
System.out.println (amount + "Calories is " + joules + "joules");
break;
// converting Joules to Joules
case 3:
double joules = amount * joule;
System.out.println (amount + "joules is " + joules + "joules");
break;
// exit program
case 4:
System.exit(0)
break;
default:
System.out.println ("You have entered an incorrect number, Please try again.");
}
}
}
Mar 2 '07 #7
r035198x
13,262 8TB
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.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 program");
int choice=stdin.nextInt();
//converting BTUS to Joules
switch (choice){
case 1:
double amount = stdin.nextDouble();
double joules = amount * BTUs;
System.out.println (amount + "BTUs is " + joules + "joules");
break;
// converting Calories to Joules
case 2:
double joules = amount * calories;
System.out.println (amount + "Calories is " + joules + "joules");
break;
// converting Joules to Joules
case 3:
double joules = amount * joule;
System.out.println (amount + "joules is " + joules + "joules");
break;
// exit program
case 4:
System.exit(0)
break;
default:
System.out.println ("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
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 8TB
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
How do you want it to pause? Pause until the user does something or pause for a specific period of time?
Thanks. When I ran the program in DOS mode it work fine. It just ran fast when I ran the program in JGRASP. Now I just have to create a user guide and a test program.. Thanks a Lot...

Minterman
Mar 3 '07 #11

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

Similar topics

7
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...
7
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...
31
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...
0
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:...
5
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...
2
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...
5
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? ...
4
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...
18
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.