| | |
do-while loop & other stuff
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Hi, I am new to Java, and have been trying to write a program that calculates the square root of a number using the Babylonian/Heron method. I have been able to make it work, largely due to a lot of help from a PhD student.
So yeah, what I'm trying to do now is to create a do-while loop so that a user chooses whether they want to exit the program or not.
So far i've got Where s is the variable name of whatever the user's response is. I want to say if the user presses y, for the whole program to run again, and if they don't, for the program to terminate.
All help is appreciated, thanks guys.
P.S. the relevant java class files are attached.
So yeah, what I'm trying to do now is to create a do-while loop so that a user chooses whether they want to exit the program or not.
So far i've got
java Syntax (Toggle Plain Text)
System.out.println("Press y to continue using the program or any other character to exit"); s = stdin.read(); System.out.println( ); } while (s = "y"){
All help is appreciated, thanks guys.
P.S. the relevant java class files are attached.
Never approach a computer with the words "I'll just do this quickly..."
From your code:
This is wrong in two aspects:
1. You are trying to do an assignment operation instead of comparison.
2. Even if you want to compare Strings, you should be using the equals() method instead of ==
Java Syntax (Toggle Plain Text)
while (s = "n"){
This is wrong in two aspects:
1. You are trying to do an assignment operation instead of comparison.
2. Even if you want to compare Strings, you should be using the equals() method instead of ==
Hey aniseed, i'm using Eclipse, and it doesn't seem to like me using "equals()".
It also has a problem with my reading in line, i've got and it told me that it couldn't convert an integer to a character (which sounds reasonable enough!)
Any ideas as to how to make that a character reading in line?
It also has a problem with my reading in line, i've got
java Syntax (Toggle Plain Text)
s=stdin.read();
Any ideas as to how to make that a character reading in line?
Never approach a computer with the words "I'll just do this quickly..."
Eclipse likes equals a lot... What you're doing, comparing object references using ==, is a deadly sin almost universally (there are a very few scenarios where you want it, but those are so specific you can for now forget about them).
In general: NEVER use == to compare objects, it doesn't work (or rather it works perfectly, just not how you think it does).
In general: NEVER use == to compare objects, it doesn't work (or rather it works perfectly, just not how you think it does).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
OK jwenting, thanks, I changed the type to int, so that now my code compiles and runs with no errors, but the test doesn't work
. The joys of hacking code. Anyone know or recommend any good (current) books for absolute beginners with no teachers to help out?
. The joys of hacking code. Anyone know or recommend any good (current) books for absolute beginners with no teachers to help out? Never approach a computer with the words "I'll just do this quickly..."
![]() |
Similar Threads
- Help with a date loop & error '80020009' (ASP)
- loop that skips a number (C)
- Perl- How to call an event after a time delay (Perl)
Other Threads in the Java Forum
- Previous Thread: How to discard stopwords contained in class and do word count? (edited)
- Next Thread: IO Exception/Null
| Thread Tools | Search this Thread |
Tag cloud for Java
911 addressbook android api append apple applet application arguments array arrays automation binary bluetooth character chat class classes client code component csv database draw eclipse error event exception file fractal ftp game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me japplet java javaarraylist javaprojects jmf jni jpanel julia linked linux list loop map method methods mobile netbeans newbie number object objects oracle oriented panel print printf problem program programming project projects recursion replaydirector reporting researchinmotion return robot rotatetext scanner score screen se server set size sms socket sort sql stream string swing test threads time tree ubuntu windows






