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, 10:21 AM   #11
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
Ok guys. I'm getting somewhere. This is still incomplete, but check it out and give me pointers.

import java.util.*;

public class TempConversion
{
    public static void main(String[] args)
    {
    	int degreeValue;
    	String typeOfConversion
    	double result;
    	
    	System.out.println("=====TEMPERATURE CONVERSION=====");
    	System.out.println();
    	System.out.println("Please enter a whole number for");
    	System.out.println("conversion:");
    	
    	Scanner keyboard = new Scanner(System.in);
    	degreeValue = new keyboard.nextInt(System.in)
    	
    	System.out.println("Now, 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:");
    	
    	boolean = restartConversion;
    	while (restartConversion = true)
    	{
			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);
			}
		}
	}
}
shangnyun is offline   Reply With Quote
Old Jun 9th, 2005, 10:48 AM   #12
Knight
Newbie
 
Join Date: Jun 2005
Posts: 28
Rep Power: 0 Knight is on a distinguished road
boolean = restartConversion;
Should be:
boolean restartConversion = true;

Your while loop is an infinite loop. You never change the value for the variable restartConversion. So, try the following:
while (restartConversion = true)
    	{
		if((typeOfConversion == "C") || (typeOfConversion == "c"))
		{
			result = 5.0((double)degreeValue - 32.0)/9.0;
			System.out.println(degreeValue + " Fahrenheit is " + result + " Celsius");
                        restartConversion = false;
		}
		else if((typeOfConversion == "F") || (typeOfConversion == "f"))
		{
			result = (9.0(degreeValue)/5.0) + 32.0;
			System.out.println(degreeValue + " Celsius is " + result + " Fahrenheit);
                        restartConversion = false;
		}
                else
                {
                        System.out.println("Oops! Something went wrong!");
                        restartConversion = false;
                }
	}

Hope this helps

Last edited by Knight; Jun 9th, 2005 at 10:51 AM.
Knight is offline   Reply With Quote
Old Jun 9th, 2005, 11:02 AM   #13
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
It's clear you need to read the input in as a string (which you should always do, no matter what - it prevents program breaking). Remember, pseudocode is one of the most useful tools you have when programming. For example:
variables:
	string: input
	double: degreesC
	double: degreesF

start loop
	ask for input
	if input is 'Q' or 'q' then break out of loop
	
	(verify input)
	loop through the input string, making sure every character is a number or a dot, and the last character is either a 'C' or an 'F'.
	
	if input ends in 'C':
		copy number portion of input into degreesC
		calculate and print degreesF
	
	else if input ends in 'F':
		copy number portion of input into degreesF
		calculate and print degreesC
end loop
With something like that, you can easily see what you need to do.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jun 9th, 2005, 11:13 AM   #14
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
OMFG I think I'm done!!!!

Remember, the assignment asks for a switch statement!

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:");
     		System.out.println();
    		System.out.println();

			degreeValue = new keyboard.nextInt(System.in);

    		System.out.println("Now, 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 = new keyboard.next(System.in);

			switch (typeOfConversion)
			{
				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(degreeValue)/5.0) + 32.0;
					System.out.println();
					System.out.println(degreeValue + " Celsius is " + result + " Fahrenheit");
					break;
				default:
					System.out.println("Oops! Your entry is invalid. Please enter either");
					System.out.println(""/F"/ or "/C"/. The letter case does not matter.");
					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(System.in);

//			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"));
	}
}

This is beautiful. It all makes PERFECT sense to me . I think this is the whole deal! I haven't compiled/tested yet, I'm sure I need to modify some stuff/possibly iron out syntax errors and whatnot... but this is it!

Ooble.. great point. Pseudo-code is great... I simply have never used it before. I really need to start using it . It would have helped here, tremendously. I did this all with straight-out coding, no pseudo-code. But check it out!
shangnyun is offline   Reply With Quote
Old Jun 9th, 2005, 11:30 AM   #15
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
OK I got the escape characters all wrong at first. I was using "/ instead of \".. that's all fixed. I'm down to two errors now, and I have no idea what's going on. What does this mean?? What does the "^" stand for anyway?

C:\JAVA\MyJava\HOMEWORK\HW Assignment 1\4th Edition\Chapter 03\TempConversion.java:37: ';' expected
					result = 5.0((double)degreeValue - 32.0)/9.0;
                                                    ^
C:\JAVA\MyJava\HOMEWORK\HW Assignment 1\4th Edition\Chapter 03\TempConversion.java:43: ')' expected
					result = (9.0(degreeValue)/5.0) + 32.0;
                                                     ^
2 errors
shangnyun is offline   Reply With Quote
Old Jun 9th, 2005, 11:57 AM   #16
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
You need a multiplication sign: 5.0 * ((double)...
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jun 9th, 2005, 12:02 PM   #17
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
YEP!! Just figured that out... missing operators. Thanks a lot . Now I have a whole new batch of errors... I fixed those two and got 4 new ones. Fixing those..

C:\JAVA\MyJava\HOMEWORK\HW Assignment 1\4th Edition\Chapter 03\TempConversion.java:24: package keyboard does not exist
			degreeValue = new keyboard.nextInt(System.in);
                                                  ^
C:\JAVA\MyJava\HOMEWORK\HW Assignment 1\4th Edition\Chapter 03\TempConversion.java:31: package keyboard does not exist
			typeOfConversion = new keyboard.next(System.in);
                                                       ^
C:\JAVA\MyJava\HOMEWORK\HW Assignment 1\4th Edition\Chapter 03\TempConversion.java:33: incompatible types
found   : java.lang.String
required: int
			switch (typeOfConversion)
                                ^
C:\JAVA\MyJava\HOMEWORK\HW Assignment 1\4th Edition\Chapter 03\TempConversion.java:59: cannot find symbol
symbol  : method next(java.io.InputStream)
location: class java.util.Scanner
			answer = keyboard.next(System.in);
                                         ^
4 errors

Any help is highly appreciated with fixing these. I have no freakin clue what I'm doing. Well, I have somewhat of a clue.. but I'm guessing
shangnyun is offline   Reply With Quote
Old Jun 9th, 2005, 12:12 PM   #18
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
OK.. I realized now that the switch statement can only take char and int values. I need the switch statement to understand my F/f and C/f though.. and those are entered by the user using

Scanner keyboard = new Scanner(System.in);
typeOfConversion = keyboard.next();

I've declared typeOfConversion as a string at the beginning... so what now? How do I make my switch statement take the string value? Only thing I can think of is by type-casting the string value to a CHAR value, but that's not possible, right?

Damn!

Why is there no

typeOfConversion = keyboard.nextChar();

!!?
shangnyun is offline   Reply With Quote
Old Jun 9th, 2005, 12:29 PM   #19
Gilward Kukel
Newbie
 
Join Date: Jun 2005
Location: Vienna, Austria
Posts: 15
Rep Power: 0 Gilward Kukel is on a distinguished road
switch(typeOfConversion.charAt(0)){
case 'c':
...
}
Gilward Kukel is offline   Reply With Quote
Old Jun 9th, 2005, 12:35 PM   #20
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
Quote:
Originally Posted by Gilward Kukel
switch(typeOfConversion.charAt(0)){
case 'c':
...
}
Genius... wow. Thank you! It worked... it just compiled without ANY errors . Oh dayum... now let me see those infinte loops I've probably created...

/me gets ready to hit CTRL + C

Do you guys hang out in any IRC channels? I could really need a Java programming IRC channel to hang out in.. with helpful people who don't get pissed at noobs

Thanks!
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 2:32 AM.

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