6 Replies - 2241 Views - Last Post: 21 November 2012 - 06:27 AM Rate Topic: -----

#1 Asma-j   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-November 12

Error in java statment

Posted 21 November 2012 - 03:16 AM

I was try to solve this "Write a program that allows the user to convert a temperature given in degrees from either Celsius to Fahrenheit or Fahrenheit to Celsius. Use the following formulas:
Degrees_C = 5(Degrees_F -32)/9
Degrees_F = (9(Degrees_C)/5) + 32)
Prompt the user to enter a temperature and either a C or c for Celsius is entered, or to Celsius if Fahrenheit is entered. Display the result in a readable format. If anything other than C, c, F or f is entered, print an error message and stop."

when i test it i have errors
import java.util.Scanner;  

    

 /**  

 Convert a temperature in degrees from either celsius to  

 fahrenheit or fahrenheit to celsius.  

  */ 

    

 public class TempConvert  

 {  

 public static void main(String[] args)  

 {  

//Have the user to enter a temperture in celsius or fahrenheit  

System.out.println("Enter a temperture");  

 System.out.println("In C or c for celsius");  

 System.out.println("And F or f for fahrenheit");  

double degrees_F;  

double degrees_C;  

degrees_F = keyboard.nextDouble;  

degrees_C = keyboard.nextDouble;  

 //Convert each temperture from celsius to fahrenheit and fahrenheit to celsius.  

 if (degrees_C){  

 celsius = (5*(10 - 32)/9);  

 System.out.println("Your degrees will be in fahrenheit");  

 }  

else if (degrees_F){  

 fahrenheit = (9*(10/5)) + 32);  

 System.out.println("Your degrees will be in celsius");  

   }  

 else{  

  System.out.println("Try again");  



//Convert the temperature to fahrenheit if celsius is enter  
            //or to celsius if fahrenheit is entered.  
    

  System.out.println("The temperture is " + degrees);  

  }  

    }  

 }


*Edited: please :code:

This post has been edited by pbl: 21 November 2012 - 06:20 AM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Error in java statment

#2 binarymatter   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-September 12

Re: Error in java statment

Posted 21 November 2012 - 03:21 AM

What error are you getting?
Was This Post Helpful? 0
  • +
  • -

#3 binarymatter   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-September 12

Re: Error in java statment

Posted 21 November 2012 - 03:28 AM

if (degrees_C){ a if statement should hold a true or false value as degrees_C is a double value is should be compare to something to make it a true or false statement example degrees_C == 10.5
Was This Post Helpful? 0
  • +
  • -

#4 Asma-j   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-November 12

Re: Error in java statment

Posted 21 November 2012 - 03:51 AM

View Postbinarymatter, on 21 November 2012 - 03:28 AM, said:

if (degrees_C){ a if statement should hold a true or false value as degrees_C is a double value is should be compare to something to make it a true or false statement example degrees_C == 10.5


3 lines show this :
not resolved

and this:
syntax error on token " else " , delete this token ..

View PostAsma-j, on 21 November 2012 - 03:50 AM, said:

View Postbinarymatter, on 21 November 2012 - 03:28 AM, said:

if (degrees_C){ a if statement should hold a true or false value as degrees_C is a double value is should be compare to something to make it a true or false statement example degrees_C == 10.5

errors
3 lines show this :
not resolved

and this:
syntax error on token " else " , delete this token ..

Was This Post Helpful? 0
  • +
  • -

#5 raghav.naganathan   User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 410
  • View blog
  • Posts: 1,449
  • Joined: 14-September 12

Re: Error in java statment

Posted 21 November 2012 - 03:53 AM

Please post the exact errors you are getting. That will probably help us understand where exactly in your code the problem is occurring.

One thing you need to note here is

if(anything) will always give TRUE, unless that anything is 0

Also, you need to declare the variables celsius and fahrenheit before you can use them in your if and elseif conditions.

regards,
Raghav

This post has been edited by raghav.naganathan: 21 November 2012 - 04:00 AM

Was This Post Helpful? 0
  • +
  • -

#6 Asma-j   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-November 12

Re: Error in java statment

Posted 21 November 2012 - 05:51 AM

in attachment showing an errors

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Error in java statment

Posted 21 November 2012 - 06:27 AM

Rather post your updated code
indent it correctly asnd it will become obvious
(in Eclipse: <CTRL>A <CTRL>I)

as far as the second error is concerned, float constant in Java are double so
5.0 / 9.0 is a double constant
5.0f / 9.0f is a float constant

your variables are declared float not double so if you mixed them, the compiler signals a possible loss of precision

This post has been edited by pbl: 21 November 2012 - 06:30 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1