943,625 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3300
  • Java RSS
Jan 30th, 2007
0

do-while loop & other stuff

Expand Post »
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
java Syntax (Toggle Plain Text)
  1. System.out.println("Press y to continue using the program or any other character to exit");
  2. s = stdin.read();
  3. System.out.println( );
  4. }
  5. while (s = "y"){
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.
Attached Files
File Type: java Root22.java (1.3 KB, 39 views)
File Type: java RootApproximator2.java (582 Bytes, 32 views)
Similar Threads
Sponsor
Reputation Points: 50
Solved Threads: 11
Posting Whiz
Thinka is offline Offline
375 posts
since Aug 2004
Jan 30th, 2007
0

Re: do-while loop & other stuff

From your code:
Java Syntax (Toggle Plain Text)
  1. 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 ==
Reputation Points: 48
Solved Threads: 7
Posting Whiz
aniseed is offline Offline
353 posts
since Apr 2006
Jan 30th, 2007
0

Re: do-while loop & other stuff

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
java Syntax (Toggle Plain Text)
  1. s=stdin.read();
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?
Sponsor
Reputation Points: 50
Solved Threads: 11
Posting Whiz
Thinka is offline Offline
375 posts
since Aug 2004
Jan 30th, 2007
0

Re: do-while loop & other stuff

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).
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jan 31st, 2007
0

Re: do-while loop & other stuff

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?
Sponsor
Reputation Points: 50
Solved Threads: 11
Posting Whiz
Thinka is offline Offline
375 posts
since Aug 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: How to discard stopwords contained in class and do word count? (edited)
Next Thread in Java Forum Timeline: IO Exception/Null





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC