![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
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);
}
}
}
} |
|
|
|
|
|
#12 |
|
Newbie
Join Date: Jun 2005
Posts: 28
Rep Power: 0
![]() |
boolean = restartConversion; 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. |
|
|
|
|
|
#13 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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 |
|
|
|
|
|
#14 |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
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! |
|
|
|
|
|
#15 |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#16 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
You need a multiplication sign: 5.0 * ((double)...
|
|
|
|
|
|
#17 |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
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 errorsAny 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 ![]() |
|
|
|
|
|
#18 |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
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(); !!? |
|
|
|
|
|
#19 |
|
Newbie
Join Date: Jun 2005
Location: Vienna, Austria
Posts: 15
Rep Power: 0
![]() |
switch(typeOfConversion.charAt(0)){
case 'c':
...
} |
|
|
|
|
|
#20 | |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
Quote:
. 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! |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|