Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: (Beginner) Hopelessly lost on Temperature converting (C to F, F to C)

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default (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..
    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.
    Last edited by skw4712; July 17th, 2011 at 01:11 AM. Reason: I don't even know what I typed in, actually...My brain is completely numb now, time for some sleep.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: (Beginner) Hopelessly lost on Temperature converting (C to F, F to C)

    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?


    way to limit how far the decimal
    Look at the printf method or the DecimalFormat class to control the number of digits displayed.
    Last edited by Norm; July 17th, 2011 at 09:28 AM.

  3. The Following User Says Thank You to Norm For This Useful Post:

    skw4712 (July 18th, 2011)

  4. #3
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default 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:
    if (s1.equals(s1)) {
    ...and...
    else if (s2.equals(s2)){

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  6. #5
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: (Beginner) Hopelessly lost on Temperature converting (C to F, F to C)

    Quote Originally Posted by Norm View Post
    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.

    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.

    At a certain point the clues should get closer and stronger.
    Yes, of course.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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

  8. #7
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: (Beginner) Hopelessly lost on Temperature converting (C to F, F to C)

    Quote Originally Posted by Norm View Post
    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?

    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

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  10. #9
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default 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
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  11. #10
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default 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

  12. #11
    Junior Member
    Join Date
    Jul 2011
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Talking Re: (Beginner) Hopelessly lost on Temperature converting (C to F, F to C)

    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!
    Last edited by skw4712; July 18th, 2011 at 05:59 AM.

  13. #12
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default 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.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: (Beginner) Hopelessly lost on Temperature converting (C to F, F to C)

    The comments are all tongue-in-cheek.
    Absolutely. We love programming and want to help newbies get over the initial hurdle.

  15. #14
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default 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.

Similar Threads

  1. NEED HELP WITH TEMPERATURE
    By Dracer in forum AWT / Java Swing
    Replies: 5
    Last Post: December 11th, 2010, 03:12 PM
  2. Help, please. I'm lost
    By Borb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 17th, 2010, 08:50 PM
  3. I'm lost
    By stoptheerrors in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 31st, 2010, 08:47 AM
  4. lost
    By nyny in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 7th, 2010, 07:32 AM
  5. Converting temperature program
    By ixjaybeexi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 20th, 2009, 07:27 PM