(Beginner) Hopelessly lost on Temperature converting (C to F, F to C)
4 Hours of struggles, no result..Completely stuck with my dull brain and need help..@-)
Code Java:
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.
Re: (Beginner) Hopelessly lost on Temperature converting (C to F, F to C)
Quote:
It doesn't convert F to C (Shows the C to F instead).
Is that a logic problem? How does the user tell the program which way to convert?
You need to walk through your logic using a piece of paper and a pencil. Make a box for each variable that you are using.
Go through your code, line by line and write in the box for each variable the value of each variable as it changes.
Look at each if statement and consider what the values of the variables are for that if statement.
There are six String variables. What are each of the variables used for?
Quote:
way to limit how far the decimal
Look at the printf method or the DecimalFormat class to control the number of digits displayed.
Re: (Beginner) Hopelessly lost on Temperature converting (C to F, F to C)
Hmm. Comparing something with itself is always going to give 'true' so there's not much point actually doing it - as in:
Code java:
if (s1.equals(s1)) {
...and...
else if (s2.equals(s2)){
Re: (Beginner) Hopelessly lost on Temperature converting (C to F, F to C)
Would the OP find that if he played computer and looked at the code line by line and evaluated what each line does?
Do these OPs learn anything if we tell them what the problem with their code is vs trying to make them look at their code to see what it is doing?
At a certain point the clues should get closer and stronger.
Re: (Beginner) Hopelessly lost on Temperature converting (C to F, F to C)
Quote:
Originally Posted by
Norm
Would the OP find that if he played computer and looked at the code line by line and evaluated what each line does?
Possibly. When you're starting out, there's so much to remember that, if you're not sure what you're looking for, you can quite easily miss the obvious.
Quote:
Do these OPs learn anything if we tell them what the problem with their code is vs trying to make them look at their code to see what it is doing?
This is an important question, and I think the answer is 'it depends'. There are different kinds of coding error, and some are more educational than others. Attention errors, like missing or misplaced braces, spelling, case sensitivity, using wrong variable, etc., are minimally educational - the lesson is basically 'take more care' - and even when the lesson is learned, they are the kind of errors made even by experienced coders (careless use of copy-paste is a classic). For these sorts of errors, I don't think a great deal is lost by pointing them out and letting the coder get on to the trickier problems of syntax, semantics, logic, structure, etc. I remember being grateful for people pointing out 'silly' mistakes in my code, because sometimes you're so busy looking for the complicated or subtle, or so totally confused and at sea, that discovering the cause is trivially simple error puts things back in perspective again.
A counter argument is that leaving the OP to find a silly mistake is good reinforcement for a focused and careful approach, and I wouldn't argue with that. It's a judgement call as to which you feel is more beneficial in a particular case.
YMMV.
Quote:
At a certain point the clues should get closer and stronger.
Yes, of course.
Re: (Beginner) Hopelessly lost on Temperature converting (C to F, F to C)
Great answer. I was getting a bit grumpy with another issue.
Back to the OP's problem
Re: (Beginner) Hopelessly lost on Temperature converting (C to F, F to C)
Quote:
Originally Posted by
Norm
Great answer.
Thanks - it is important - it's basically why we make time in a busy schedule of being nagged and feeling guilty to come here and give someone a 'eureka!' moment... oh wait, is that just me?
Quote:
I was getting a bit grumpy with another issue.
If you're talking about some of the recent threads where I've had to bite my fist to stop from posting something rude, then I quite understand your grumpiness... if not, I hope whatever it is gets sorted out ;)
Re: (Beginner) Hopelessly lost on Temperature converting (C to F, F to C)
I agree, but We're hanging out our laundry for all to see.
Re: (Beginner) Hopelessly lost on Temperature converting (C to F, F to C)
dlorde & Norm, I can't thank you enough for your patience and contributions to the forums.
I can understand your frustrations when it comes to newbies and going over the same stuff again and again.
Feel free to take a back seat before you succumb to extreme stress lol
I can't be responsible for any nervous breakdowns ;)
Re: (Beginner) Hopelessly lost on Temperature converting (C to F, F to C)
They say that managing programmers is like herding cats.
Norm has reminded me - I must do the laundry today. I promise to hang it out of sight ;)
Re: (Beginner) Hopelessly lost on Temperature converting (C to F, F to C)
Code Java:
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();
double Degrees_C = temp;
double Degrees_F = temp;
System.out.print("Enter either letter C or F to indicate degrees Celsius or Fahrenheit, respectively: ");
String s1 = keyboard.next();
char c1 = s1.charAt(0);
switch (c1){
case 'C':
case 'c':
Degrees_F = (9 * ((Degrees_C) / 5) + 32);
System.out.print(temp + " degrees Celsius is " + Degrees_F + " degrees Fahrenheit.");
break;
case 'F':
case 'f':
Degrees_C = (5 * (Degrees_F - 32) / 9);
System.out.print(temp + " degrees Fahrenheit is " + Degrees_C + " degrees Celsius.");
break;
default:
System.out.print("You did not enter a letter C nor F.");
break;
}
}
}
Finally got it...Actually Norm has helped me with his 2nd post, you know, a good-ol' piece of paper and pencil method :> 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!
Some of you may be laughing right now at my newb excitement, but as all of you would know when you struggle at something for hours and find the solution (pretty much by yourself), it's really rewarding.
Also, plz don't hate or belittle (which is kinda how I felt) newbs...:(( 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.
I feel much satisfied solving a problem rather than others solving it for me. But since I probably am the most impatient person you'll ever encounter, there's a point (around 3 hour mark) that I go absolutely berserk. That's when I put my Q's on the forum. If you do give me a coding (which you guys thankfully didn't), I would definitely write down what was wrong with my code and thought process and try to make sure I don't make same mistakes again...
If you guys feel annoyed at repetitive newbie Q's, maybe you guys can redirect us newbs to a thread with similar Q's that has been solved or show us some hint that might help...
I'm gonna stop talking now...Anyways, thanks so much for everybody who helped me, and good day!
Re: (Beginner) Hopelessly lost on Temperature converting (C to F, F to C)
Hey skw4712, good follow up post. I'm glad you have taken on board what has been said.
No one hates here. Everyone was a newbie at some point :)
It's just that we get a lot of newbie type questions and Norm & dlorde are always on the front line answering them. The comments are all tongue-in-cheek.
You don't have to worry about asking questions here. Reading what you have wrote confirms that you are an ideal member to have.
Re: (Beginner) Hopelessly lost on Temperature converting (C to F, F to C)
Quote:
The comments are all tongue-in-cheek.
Absolutely. We love programming and want to help newbies get over the initial hurdle.
Re: (Beginner) Hopelessly lost on Temperature converting (C to F, F to C)
Yeah - I can still feel the frustration, annoyance and embarrassment of my first days programming - like anything else, it just takes persistence and it gets easier. Our comments were in no way aimed at the OP or this thread; sorry if it seemed that way.