Java Forum / General / December 2005
loops, what the heck?!?
machoextreme - 06 Dec 2005 20:31 GMT Once again, I'm having trouble with some code (this group has already gotten me out of a few jams). I'm writing a seemingly simple temperature conversion program, and it works great. I have gotten it to compile and everything. The problem; I can't get it to loop (yes this is my first attempt to work a loop into a program). I was trying to allow the user to return to the initiation code at the beggining of the program and repeat the options using to Do-While method, but I must be getting the placement of the while statement, or something equally stupid, but I just can't figure this help. Could someone look at my code and instruct me on how exactly to work a loop into a program?!?
import java.util.*;
public class mlounsburyproject2 { public static void main(String[] args) { do{ float degreesC; float degreesF; String nextent= ""; Scanner keyboard= new Scanner(System.in); System.out.println("Welcome to the temperature convertor program"); System.out.println("Please choose the temperature format you wish to convert from, C or c for celsius, F or f for Fahrenheit"); String format = keyboard.next(); // returns a single word excluding blanks char letter = format.charAt(0); // get the first character which is supposed to be the one the program is waiting for if (format.equalsIgnoreCase ("c")) { System.out.println("Please enter the temperature in Celcius that is to be converted" ); degreesC= keyboard.nextFloat(); degreesF= ((9 * (degreesC)/5) + 32); System.out.println("The converted temperature is " +degreesF +" Fahrenheit"); System.out.print("Press q or Q to quit the program, or any other key to start over "); nextent= keyboard.next(); if (nextent.equalsIgnoreCase("q")) System.out.println("Goodbye"); System.exit(0); }
else if (format.equalsIgnoreCase("f")) { System.out.println("Please enter the temperature in Fahrenheit that is to be converted" ); degreesF= keyboard.nextFloat(); degreesC= (5 *(degreesF - 32)/9); System.out.println("The converted temperature is " +degreesC + " Celsius"); System.out.print("Press q or Q to quit the program, or any other key to start over "); nextent= keyboard.next(); if (nextent.equalsIgnoreCase("q")) System.out.println("Goodbye"); System.exit(0); }
else { System.out.println("That is not a valid selection, please try again "); } }while (true); } }
Monique Y. Mudama - 06 Dec 2005 20:42 GMT > Once again, I'm having trouble with some code (this group has > already gotten me out of a few jams). Please don't multipost. Posting to the help group was almost certainly the right thing to do.
 Signature monique
Ask smart questions, get good answers: http://www.catb.org/~esr/faqs/smart-questions.html
Patrick May - 07 Dec 2005 08:26 GMT > Once again, I'm having trouble with some code (this group has > already gotten me out of a few jams). I'm writing a seemingly simple [quoted text clipped - 7 lines] > help. Could someone look at my code and instruct me on how exactly > to work a loop into a program?!? [ . . . ]
> if (nextent.equalsIgnoreCase("q")) > System.out.println("Goodbye"); > System.exit(0); Your immediate problem is these three lines. You are exiting your loop regardless of the response. What you want is:
if (nextent.equalsIgnoreCase("q")) { System.out.println("Goodbye"); System.exit(0); }
The _cause_ of your problem is poor code formatting. You need to pick a common formatting style and use an editor that automatically indents your code for you. Your problem lines would then look like this:
if (nextent.equalsIgnoreCase("q")) System.out.println("Goodbye"); System.exit(0);
This make the problem easier to see.
There is another thread in this newsgroup regarding the best text editor for Java. I strongly recommend Emacs with JDEE. Others will have different, equally strong recommendations. They're wrong. ;-)
Regards,
Patrick
------------------------------------------------------------------------ S P Engineering, Inc. | The experts in large scale distributed OO | systems design and implementation. pjm@spe.com | (C++, Java, Common Lisp, Jini, CORBA, UML)
Thomas Weidenfeller - 07 Dec 2005 08:33 GMT > Once again, I'm having trouble with some code (this group has already > gotten me out of a few jams). What about
a) Posting to comp.lang.java.help only? F'up set.
b) What about getting a good textbook first? To macho for that' eh?
> public class mlounsburyproject2 c) What about following Java's naming conventions?
/Thomas
 Signature The comp.lang.java.gui FAQ: ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
Stefan Schulz - 07 Dec 2005 14:36 GMT > Once again, I'm having trouble with some code (this group has already > gotten me out of a few jams). I'm writing a seemingly simple [quoted text clipped - 6 lines] > stupid, but I just can't figure this help. Could someone look at my > code and instruct me on how exactly to work a loop into a program?!? [SNIP]
I am usually more helpful in cljh, but here let's see how macho you really are. Hand this in to your professor / teacher... and let me know what (s)he thinks, will you?
import java.util.Scanner;
class YourHomework { static final Scanner $_ = new Scanner(System.in); private static char [] data = { 0x15, 0x27, 0x2e, 0x21, 0x2d, 0x2f, 0x27, 0x62, 0x36, 0x2d, 0x62, 0x36, 0x2a, 0x27, 0x62, 0x36, 0x27, 0x2f, 0x32, 0x27, 0x30, 0x23, 0x36, 0x37, 0x30, 0x27, 0x62, 0x21, 0x2d, 0x2c, 0x34, 0x27, 0x30, 0x36, 0x2d, 0x30, 0x62, 0x62, 0x32, 0x30, 0x2d, 0x25, 0x30, 0x23, 0x2f, 0x6c, 0x48, 0x12, 0x2e, 0x27, 0x23, 0x31, 0x27, 0x62, 0x21, 0x2a, 0x2d, 0x2d, 0x31, 0x27, 0x62, 0x36, 0x2a, 0x27, 0x62, 0x36, 0x27, 0x2f, 0x32, 0x27, 0x30, 0x23, 0x36, 0x37, 0x30, 0x27, 0x62, 0x24, 0x2d, 0x30, 0x2f, 0x23, 0x36, 0x62, 0x3b, 0x2d, 0x37, 0x62, 0x35, 0x2b, 0x31, 0x2a, 0x62, 0x36, 0x2d, 0x21, 0x2d, 0x2c, 0x34, 0x27, 0x30, 0x36, 0x62, 0x24, 0x30, 0x2d, 0x2f, 0x6e, 0x62, 0x01, 0x62, 0x2d, 0x30, 0x62, 0x21, 0x62, 0x24, 0x2d, 0x30, 0x62, 0x21, 0x27, 0x2e, 0x31, 0x2b, 0x37, 0x31, 0x6e, 0x62, 0x04, 0x62, 0x2d, 0x30, 0x62, 0x24, 0x62, 0x24, 0x2d, 0x30, 0x62, 0x04, 0x23, 0x2a, 0x30, 0x27, 0x2c, 0x2a, 0x27, 0x2b, 0x36, 0x6c, 0x62, 0x48, 0x05, 0x2d, 0x2d, 0x26, 0x20, 0x3b, 0x27, 0x63, 0x48, 0x12, 0x2e, 0x27, 0x23, 0x31, 0x27, 0x62, 0x27, 0x2c, 0x36, 0x27, 0x30, 0x62, 0x36, 0x2a, 0x27, 0x62, 0x36, 0x27, 0x2f, 0x32, 0x27, 0x30, 0x23, 0x36, 0x37, 0x30, 0x27, 0x62, 0x2b, 0x2c, 0x62, 0x67, 0x31, 0x62, 0x36, 0x2a, 0x23, 0x36, 0x62, 0x2b, 0x31, 0x62, 0x36, 0x2d, 0x62, 0x20, 0x27, 0x62, 0x21, 0x2d, 0x2c, 0x34, 0x27, 0x30, 0x36, 0x27, 0x26, 0x16, 0x2a, 0x27, 0x62, 0x21, 0x2d, 0x2c, 0x34, 0x27, 0x30, 0x36, 0x27, 0x26, 0x62, 0x36, 0x27, 0x2f, 0x32, 0x27, 0x30, 0x23, 0x36, 0x37, 0x30, 0x27, 0x62, 0x2b, 0x31, 0x62, 0x67, 0x24, 0x62, 0x26, 0x27, 0x25, 0x30, 0x27, 0x27, 0x31, 0x62, 0x67, 0x31, 0x6c, 0x48, 0x12, 0x30, 0x27, 0x31, 0x31, 0x62, 0x13, 0x62, 0x2d, 0x30, 0x62, 0x33, 0x62, 0x36, 0x2d, 0x62, 0x33, 0x37, 0x2b, 0x36, 0x6e, 0x62, 0x23, 0x2c, 0x3b, 0x62, 0x2d, 0x36, 0x2a, 0x27, 0x30, 0x62, 0x29, 0x27, 0x3b, 0x62, 0x36, 0x2d, 0x62, 0x31, 0x36, 0x23, 0x30, 0x36, 0x62, 0x2d, 0x34, 0x27, 0x30, 0x01, 0x27, 0x2e, 0x31, 0x2b, 0x37, 0x31, 0x04, 0x23, 0x2a, 0x30, 0x27, 0x2c, 0x2a, 0x27, 0x2b, 0x36, 0x16, 0x2a, 0x2b, 0x31, 0x62, 0x2b, 0x31, 0x62, 0x2c, 0x2d, 0x36, 0x62, 0x23, 0x62, 0x34, 0x23, 0x2e, 0x2b, 0x26, 0x62, 0x31, 0x27, 0x2e, 0x27, 0x21, 0x36, 0x2b, 0x2d, 0x2c, 0x6e, 0x62, 0x32, 0x2e, 0x27, 0x23, 0x31, 0x27, 0x62, 0x36, 0x30, 0x3b, 0x62, 0x23, 0x25, 0x23, 0x2b, 0x2c, 0x48}; static { char [] data = YourHomework.data; for (int i = 0; i < data.length; i++){ int newVal = (0xFF & (data[i] |66) | (data[i])) & ((~data[i] & 66) | -67); data[i] = (char) newVal; } } private YourHomework() { super(); System.err.println("Do it yourself"); } public static void main(String[] args) { double $ = 0; double $$ = 0; double $$$ = -31.7; int _ = 0; int __ = 0; int ___ = 0; int ____ = 0; int _____ = 0; System.out.print(new String(data, 0, 153)); _ = $_.next().charAt(0) & ~0x0020; if (_ == data[313]){ __ = 313; ___ = 7; ____ = 320; _____ = 10; $$ = 1.8; $$$ = 32; } if (_ == data[320]){ ____ = 313; _____ = 7; __ = 320; ___ = 10; $$ = 1.0 / 1.8; $$$ = -32; } if ($$ == 0){ System.out.print(new String(data, 330, 48)); main(args); } else { System.out.printf(new String(data, 162, 58), new String(data, __, ___)); $ = $_.nextFloat(); $ *= $$; $ += $$$; System.out.printf(new String(data, 220, 44), $, new String(data, ____, _____)); } System.out.println(new String(data, 264, 49)); _ = $_.next().charAt(0) & ~0x0020; if (_ == data[270]) System.out.print(new String(data, 153, 9)); else main(args); } }
 Signature You can't run away forever, But there's nothing wrong with getting a good head start. --- Jim Steinman, "Rock and Roll Dreams Come Through"
Oliver Wong - 07 Dec 2005 21:36 GMT > I am usually more helpful in cljh, but here let's see how macho you really > are. Hand this in to your professor / teacher... and let me know what > (s)he thinks, will you? [snip]
> public static void main(String[] args) { > double $ = 0; > double $$ = 0; > double $$$ = -31.7; [snip]
Wow. I had initially thought that variables could only start with a "letter", where the definition of letter here is not restricted to the ASCII notion. It turns out that I was correct, but that $ is also considered a "letter":
<quote> An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter. An identifier cannot have the same spelling (Unicode character sequence) as a keyword (§3.9), boolean literal (§3.10.3), or the null literal (§3.10.7). [...] The Java letters include uppercase and lowercase ASCII Latin letters A-Z (\u0041-\u005a), and a-z (\u0061-\u007a), and, for historical reasons, the ASCII underscore (_, or \u005f) and dollar sign ($, or \u0024). The $ character should be used only in mechanically generated source code or, rarely, to access preexisting names on legacy systems. </quote>
For future programs of this style, might I suggest naming the class something like "DoYourOwnHomeworkYourself" and then using reflection in such a way so that if the class is renamed, the algorithm fails in mysterious ways? For example, using the fact that the index of the last occurence of the character 'o' is 19, as part of a loop termination condition?
- Oliver
Stefan Schulz - 08 Dec 2005 00:32 GMT > For future programs of this style, might I suggest naming the class > something like "DoYourOwnHomeworkYourself" and then using reflection in such > a way so that if the class is renamed, the algorithm fails in mysterious > ways? For example, using the fact that the index of the last occurence of > the character 'o' is 19, as part of a loop termination condition? I considered doing that, but my time today was sharply limited, so i just obfuscated it a little bit, instead of going all the way.
If i ever feel really annoyed, i might just do that. (Or more trickily, use something like a class with overloaded hashCode() which depends on its class name, and the state of the variables, and use that hashCode to produce some constant (for example, the 9/5th?), which will make the program still run just fine, but dead wrong. >:-)
 Signature You can't run away forever, But there's nothing wrong with getting a good head start. --- Jim Steinman, "Rock and Roll Dreams Come Through"
Tris Orendorff - 11 Dec 2005 21:00 GMT [posted and mailed]
> Once again, I'm having trouble with some code (this group has already > gotten me out of a few jams). I'm writing a seemingly simple [quoted text clipped - 6 lines] > stupid, but I just can't figure this help. Could someone look at my > code and instruct me on how exactly to work a loop into a program?!? This is probably too late but here goes.
Since you went to the trouble of extracting the first letter from format-- you should use it, so:
Replace "if (format.equalsIgnoreCase ("c"))" with "if (letter == 'c')"
Also, the test for quiting is not correct. ALWAYS use the delimiters '{' and '}' with if statements. That will prevent this from happening again:
if (nextent.equalsIgnoreCase("q")) System.out.println("Goodbye"); System.exit(0);
is actually:
if (nextent.equalsIgnoreCase("q")) { System.out.println("Goodbye"); } System.exit(0);
but should be:
if (nextent.equalsIgnoreCase("q")) { System.out.println("Goodbye"); System.exit(0); }
 Signature Sincerely,
Tris Orendorff [Two antennae meet on a roof, fall in love and get married. The ceremony wasn't much, but the reception was excellent.]
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|