Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 9th, 2005, 12:48 PM   #21
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
OMFG... it works it works it WORKS. It does EXACTLY what it's supposed to do! Except for one thing

Consider the following code:

    		System.out.println("Next, please enter either \"C\" or \"c\" for a");
    		System.out.println("Celsius-Fahrenheit conversion, or \"F\" or \"f\"");
    		System.out.println("for a Fahrenheit-Celsius conversion:");
			System.out.println();

//			typeOfConversion = (char)typeOfConversion;
			typeOfConversion = keyboard.next();

			switch (typeOfConversion.charAt(0))
			{
				case 'C':
				case 'c':
					result = 5.0 * ((double)degreeValue - 32.0) / 9.0;
					System.out.println();
					System.out.println(degreeValue + " Fahrenheit is " + result + " Celsius");
					break;
				case 'F':
				case 'f':
					result = (9.0 * ((double)degreeValue) / 5.0) + 32.0;
					System.out.println();
					System.out.println(degreeValue + " Celsius is " + result + " Fahrenheit");
					break;
				default:
		    		System.out.println();
					System.out.println("Oops! Your entry is invalid. Please enter either");
					System.out.println("\"F\" or \"C\". The letter case does not matter.");
					break;
			}

After the default OOPS printout... which occurs when the user enters anything but F, f, C, or c... the whole program ends and starts over. Of course, because it's got a break statement there. But how could I loop it in such a way that the user only has to continue from this point of invalid entry? In other words, how do I let the user RETRY/REENTER the correct value, rather than having the whole program end right there and start from the beginning?

Thank you
shangnyun is offline   Reply With Quote
Old Jun 9th, 2005, 12:59 PM   #22
Knight
Newbie
 
Join Date: Jun 2005
Posts: 28
Rep Power: 0 Knight is on a distinguished road
*** Messed up a bit. Can't fix just yet. Have to run. Fix later ***

Place it in a while loop. Insert the following:

                 

    		System.out.println("Next, please enter either \"C\" or \"c\" for a");
    		System.out.println("Celsius-Fahrenheit conversion, or \"F\" or \"f\"");
    		System.out.println("for a Fahrenheit-Celsius conversion:");
			System.out.println();
                                      
//			typeOfConversion = (char)typeOfConversion;
			typeOfConversion = keyboard.next();
                        while(typeOfConversion.charAt(0).equalsIgnoreCase(c) || typeOfConversion.charAt(0).equalsIgnoreCase(f))
                       {
			switch (typeOfConversion.charAt(0))
			{
				case 'C':
				case 'c':
					result = 5.0 * ((double)degreeValue - 32.0) / 9.0;
					System.out.println();
					System.out.println(degreeValue + " Fahrenheit is " + result + " Celsius");
					break;
				case 'F':
				case 'f':
					result = (9.0 * ((double)degreeValue) / 5.0) + 32.0;
					System.out.println();
					System.out.println(degreeValue + " Celsius is " + result + " Fahrenheit");
					break;
				default:
		    		System.out.println();
					System.out.println("Oops! Your entry is invalid. Please enter either");
					System.out.println("\"F\" or \"C\". The letter case does not matter.");
					break;
			}
}

That should work
Knight is offline   Reply With Quote
Old Jun 9th, 2005, 1:00 PM   #23
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
There's irc://irc.freenode.net/#programmingforums - 'tis great.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jun 9th, 2005, 1:40 PM   #24
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
Holy crap... I did it, boys! I DID IT. The program is 99.99% complete. All I have to do is maybe add a comment or two and edit out the other comments.

It performs well

Here it is, my first kick-ass program!!


import java.util.*;

public class TempConversion
{
    public static void main(String[] args)
    {
    	int degreeValue;
    	String typeOfConversion;
    	double result;
    	String answer;

    	Scanner keyboard = new Scanner(System.in);

    	do
    	{
    		System.out.println("=====TEMPERATURE CONVERSION=====");
    		System.out.println();
    		System.out.println("Please enter a whole number for");
    		System.out.println("conversion. You will be asked");
     		System.out.println("what type of conversion you want to");
     		System.out.println("perform in the next step:");
     		System.out.println();

			degreeValue = keyboard.nextInt();

			boolean typeOfConversionInputIsInvalid = true;
			while (typeOfConversionInputIsInvalid)
			{
	    		System.out.println();
	    		System.out.println();
	    		System.out.println("Please enter either \"C\" or \"c\" for a");
	    		System.out.println("Celsius-Fahrenheit conversion, or \"F\" or \"f\"");
	    		System.out.println("for a Fahrenheit-Celsius conversion:");
				System.out.println();

				typeOfConversion = keyboard.next();

				switch (typeOfConversion.charAt(0))
				{
					case 'C':
					case 'c':
						result = 5.0 * ((double)degreeValue - 32.0) / 9.0;
						System.out.println();
						System.out.println(degreeValue + " Fahrenheit is " + result + " Celsius");
						typeOfConversionInputIsInvalid = false;
						break;
					case 'F':
					case 'f':
						result = (9.0 * ((double)degreeValue) / 5.0) + 32.0;
						System.out.println();
						System.out.println(degreeValue + " Celsius is " + result + " Fahrenheit");
						typeOfConversionInputIsInvalid = false;
						break;
					default:
			    		System.out.println();
						System.out.println("Oops! Your entry is invalid. Let's try that again,");
						System.out.println("shall we?");
						typeOfConversionInputIsInvalid = true;
						break;
				}
			}

			System.out.println();
			System.out.println();
			System.out.println("Would you like to perform another conversion?");
			System.out.println("Please enter \"yes\" or \"no\":");
			System.out.println();

			answer = keyboard.next();

//			if((typeOfConversion == "C") || (typeOfConversion == "c"))
//			{
//				result = 5.0((double)degreeValue - 32.0)/9.0;
//				System.out.println(degreeValue + " Fahrenheit is " + result + " Celsius");
//			}
//			else if((typeOfConversion == "F") || (typeOfConversion == "f"))
//			{
//				result = (9.0(degreeValue)/5.0) + 32.0;
//				System.out.println(degreeValue + " Celsius is " + result + " Fahrenheit);
//			}

		}while (answer.equalsIgnoreCase("yes"));
	}
}

Holy crap... took me ALL DAY!

The boolean stuff in the switch statement in order to control/exit the while loop certainly is not the most elegant way to do it, and by all means suggest how it could be done more elegantly/simply!, but that's all I could come up with for now.

There must be a way to do it by just using a boolean value at the beginning of the WHILE statement..

while (typeOfConversion does NOT equal F, f, C, or c)

Don't know how though... still, this thing WORKS!
shangnyun is offline   Reply With Quote
Old Jun 9th, 2005, 1:44 PM   #25
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
Quote:
Originally Posted by Knight
*** Messed up a bit. Can't fix just yet. Have to run. Fix later ***

Place it in a while loop. Insert the following:

                 

    		System.out.println("Next, please enter either \"C\" or \"c\" for a");
    		System.out.println("Celsius-Fahrenheit conversion, or \"F\" or \"f\"");
    		System.out.println("for a Fahrenheit-Celsius conversion:");
			System.out.println();
                                      
//			typeOfConversion = (char)typeOfConversion;
			typeOfConversion = keyboard.next();
                        while(typeOfConversion.charAt(0).equalsIgnoreCase(c) || typeOfConversion.charAt(0).equalsIgnoreCase(f))
                       {
			switch (typeOfConversion.charAt(0))
			{
				case 'C':
				case 'c':
					result = 5.0 * ((double)degreeValue - 32.0) / 9.0;
					System.out.println();
					System.out.println(degreeValue + " Fahrenheit is " + result + " Celsius");
					break;
				case 'F':
				case 'f':
					result = (9.0 * ((double)degreeValue) / 5.0) + 32.0;
					System.out.println();
					System.out.println(degreeValue + " Celsius is " + result + " Fahrenheit");
					break;
				default:
		    		System.out.println();
					System.out.println("Oops! Your entry is invalid. Please enter either");
					System.out.println("\"F\" or \"C\". The letter case does not matter.");
					break;
			}
}

That should work
Shouldn't it be whlie (The input does NOT EQUAL C, c, F, f)???

Because the user is supposed to REDO it if it doesn't. If it DOES equal those chars, which is what your while statement suggests, why would the user want to re-go through the loop??
shangnyun is offline   Reply With Quote
Old Jun 9th, 2005, 2:03 PM   #26
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
EDIT:
Cropped the damn thing.

EDIT2:
Found a very nasty logical error. When "c" was pressed the program would do an F-C conversion instead of C-F. Same for when "f" was used. Here it is fixed and completed:


Sample output!!!


Last edited by shangnyun; Jun 9th, 2005 at 2:22 PM.
shangnyun is offline   Reply With Quote
Old Jun 9th, 2005, 2:28 PM   #27
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
Final code:

import java.util.*;

public class TempConversion
{
    public static void main(String[] args)
    {
    	int degreeValue;
    	String typeOfConversion;
    	double result;
    	String answer;

    	Scanner keyboard = new Scanner(System.in);

    	do
    	{
    		System.out.println("=====TEMPERATURE CONVERSION=====");
    		System.out.println();
    		System.out.println("Please enter a whole number for");
    		System.out.println("conversion. You will be asked");
     		System.out.println("what type of conversion you want to");
     		System.out.println("perform in the next step:");
     		System.out.println();

			degreeValue = keyboard.nextInt();

			boolean typeOfConversionInputIsInvalid = true;
			while (typeOfConversionInputIsInvalid)
			{
	    		System.out.println();
	    		System.out.println();
	    		System.out.println("Please enter either \"C\" or \"c\" for a");
	    		System.out.println("Celsius-Fahrenheit conversion, or \"F\" or \"f\"");
	    		System.out.println("for a Fahrenheit-Celsius conversion:");
				System.out.println();

				typeOfConversion = keyboard.next();

				switch (typeOfConversion.charAt(0))
				{
					case 'C':
					case 'c':
						result = (9.0 * ((double)degreeValue) / 5.0) + 32.0;
						System.out.println();
						System.out.println(degreeValue + " Celsius is " + result + " Fahrenheit");
						typeOfConversionInputIsInvalid = false;
						break;
					case 'F':
					case 'f':
						result = 5.0 * ((double)degreeValue - 32.0) / 9.0;
						System.out.println();
						System.out.println(degreeValue + " Fahrenheit is " + result + " Celsius");
						typeOfConversionInputIsInvalid = false;
						break;
					default:
			    		System.out.println();
						System.out.println("Oops! Your entry is invalid. Let's try that again,");
						System.out.println("shall we?");
						typeOfConversionInputIsInvalid = true;
						break;
				}
			}

			System.out.println();
			System.out.println();
			System.out.println("Would you like to perform another conversion?");
			System.out.println("Please enter \"yes\" or \"no\":");
			System.out.println();

			answer = keyboard.next();

//			if((typeOfConversion == "C") || (typeOfConversion == "c"))
//			{
//				result = 5.0((double)degreeValue - 32.0)/9.0;
//				System.out.println(degreeValue + " Fahrenheit is " + result + " Celsius");
//			}
//			else if((typeOfConversion == "F") || (typeOfConversion == "f"))
//			{
//				result = (9.0(degreeValue)/5.0) + 32.0;
//				System.out.println(degreeValue + " Celsius is " + result + " Fahrenheit);
//			}

		}while (answer.equalsIgnoreCase("yes"));
	}
}

Any suggestions for improvements are welcome. Please tell me . I will only learn by reading your responses/suggestions.


Thanks a TON for all the help!
shangnyun is offline   Reply With Quote
Old Jun 10th, 2005, 2:36 AM   #28
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 5 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
I would ask what type of conversion first.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Old Jun 10th, 2005, 12:46 PM   #29
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Looks fantastic, mate.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jun 10th, 2005, 1:09 PM   #30
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
Thank you, dear sir!! This is my first big success with Java. Spent ALL day on it and figured a lot of the code out myself. Granted, I had no clue what to do when I started.. but when I got done, I had twice the experience that I had before I started

So nobody cann suggest a more elegant way to control that while loop? I don't know if placing boolean variables in the different cases in the switch statement is the most effective way to do it. And by placing the boolean to true in the default case . I was proud when I figured that out all by myself though!

Thanks again for the kind comment.
shangnyun is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:56 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC