4 Hours of struggles, no result..Completely stuck with my dull brain and need help..
import java.util.Scanner; public class Project5 { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); double temp; System.out.print("Enter a temperature in degrees: "); temp = keyboard.nextDouble(); System.out.print("Enter either letter C or F to indicate degrees Celsius or Fahrenheit, respectively: "); String s1 = keyboard.nextLine(); String s2 = keyboard.nextLine(); double Degrees_C = temp; double Degrees_F = temp; String c = s1; String C= s1; String F=s2; String f=s2; if (s1.equals(s1)) { Degrees_F = (9 * ((Degrees_C) / 5) + 32); System.out.print(temp + " degrees Fahrenheit is " + Degrees_F + " degrees Celsius.");} else if (s2.equals(s2)){ Degrees_C = (5 * (Degrees_F - 32) / 9); System.out.print(temp + " degrees Celsius is " + Degrees_C + " degrees Fahrenheit.");} else{ System.out.print("You either entered the wrong value or did not enter a letter C nor F.");} } }
It actually shows C to F really well.
The problems that I'm facing is that:
1. It doesn't convert F to C (Shows the C to F instead).
2. It doesn't display the error message when entering any letters other than C, c, f, or F. (EDIT) I don't think my code can read alphabet..
3. There can be too much decimal place...Any way to limit how far the decimal could go?
Thank you very much.


LinkBack URL
About LinkBacks

Reply With Quote

by clicking the
button on their useful posts.
So I retraced my steps, and realized that I was thinking too hard using if-else statements. So I researched a bit (since my textbook is a bit confusing), and found out there was a switch statement!!! And that charAt and resulting IndexOutofBoundsException kinda bothered me, but was able to solve it!
We are not trying to bother you guys, nor are we (at least I) expecting actual lines of coding. We know many of you're busy...I don't know about everyone else, but I only ask as a last resort...so maybe you non-newbs can kinda be that guiding light on solving a program that I can't solve.