Conversion Degrees

Farenheit to Celcius

Page 1 of 1

4 Replies - 412 Views - Last Post: 16 February 2010 - 08:07 PM Rate Topic: -----

#1 Guest_hambone907*


Reputation:

Conversion Degrees

Posted 16 February 2010 - 07:50 PM

So I have a program that I have previously created, but now I want to change the input from a double to a char, so instead of having the user input a 1 or 2, they would have to type an F or C. I'm not positive how I would go about doing so. I tried doing char format, but then doing so makes the if-else format not work due to boolean format. Any help would be greatly appreciated. My compiling code is as follows...

[code]

import java.util.Scanner;
import java.text.DecimalFormat;

public class Conversion
{
public static void main( String args[] )
{
Scanner input = new Scanner( System.in );

DecimalFormat myFormatter = new DecimalFormat ("###.#");

double x;
double result;
double y;


System.out.println("Enter desired temperature");

x = input.nextDouble();

System.out.println(" Select the degrees");
System.out.println(" 1) Farenheit ");
System.out.println(" 2) Celcius ");

y = input.nextDouble();


if (y==1) {

result = (5.0/9)*(x-32);

String output = myFormatter.format (result);

System.out.println(" ");
System.out.println("You entered " + x + " degrees Farenheit which = " + output + " degrees Celcius");

}
else if (y==2) {

result = (9*x)/5 + 32;

String output = myFormatter.format (result);
System.out.println(" ");
System.out.println("You entered " + x + " degrees Celcius which = " + output + " degrees Farenheit");
}
}
}


[code/]

Is This A Good Question/Topic? 0

Replies To: Conversion Degrees

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Conversion Degrees

Posted 16 February 2010 - 07:58 PM

That's because you use Scanner's nextDouble method, and a double (obviously) is not a char. Use the next() method of Scanner to return a String, and to get the first character of the String do this:
char ans = input.next().chatAt(0);

Was This Post Helpful? 0
  • +
  • -

#3 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Conversion Degrees

Posted 16 February 2010 - 07:59 PM

well, y should be a declared as a char type variable.
and you get the value into y, using the Scanner as:
y = input.nextLine().charAt(0);



then just check the value of y as:
if(y == 'F'){
//code
}
else if(y == 'C'){
//code
}


Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Conversion Degrees

Posted 16 February 2010 - 08:01 PM

You can simply use the scanner.nextLine() method in conjunction with the string.charAt() method.

So, you could do something like this:
// Grap the first character in a line.
char degrees = input.nextLine().charAt(0);

if (Character.toLowerCase(degress) == 'f') {
    //  ... DO fahrenheit
}

//And so on ...



The Character class is going to be invaluable here. The advantage of the Character.toLowerCase() part is that if a capital OR lowercase letter is entered, it evaluates EITHER as true.

edit: Why you guys so quick tonight?

This post has been edited by Dogstopper: 16 February 2010 - 08:14 PM

Was This Post Helpful? 0
  • +
  • -

#5 mr.hands  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 31-October 09

Re: Conversion Degrees

Posted 16 February 2010 - 08:07 PM

the best way i could see going about doing this is like japanir said. check your char

if(y == 'F')
{
//do this
}

else if(y == 'C')
{
//do this
}
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1