Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: do while loop terminating to early

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default do while loop terminating to early

    Hi all,

    I am a newbie to programming and this is my first attempt to write a java program. I calculating the root of a integer that is supplied by the user, this then calls a method that does the calculation. The user is then prompted to either continue to calculate more root calculations or terminate the program, in this case the program provides a "Thank you for participating" message. This is done by either providing a "y" or "n"
    Even if the user enters a "y" to continue, the loop terminates. This is the code that I have written:

    import java.io.*;
     
    public class root {
     
    	public static int calcRoot(int a)
    	{
    		// if (a != integer)
    		// throw new intErrorExecption("Input must be a integer");
    		return a=(int) Math.sqrt(a);
    	}
     
     
    	public static void main(String[] args) throws IOException 
    	{
    		int tempInt=0;
    		String sCont;
    		BufferedReader stdin = new BufferedReader
    			      (new InputStreamReader(System.in));
    		// try
     
    		do 
    		{
     
    		System.out.print("Enter integer: ");
    		System.out.flush();
    		tempInt=Integer.parseInt( stdin.readLine());
     
    		// }  
     
    	       // catch (intErrorExecption e)
    	       // {
     
     
    	       System.out.println("The square root of " +tempInt+ " is "+calcRoot(tempInt));
     
     
     
    	       System.out.print("Do you want to continue? (y / n): ");
     
     
    	       System.out.flush();
     
     
    			/* Code to check if the read line provided y or n, if y continue loop else
    			 *  exit loop and print "Thank you for participating"
    			 */
     
    			sCont=stdin.readLine();
    			 if(sCont=="n")
    			    {
    			    	break;
     
    			    }
     
     
    			} while	(sCont == "y");	//loop is terminated here even though variable sCont is set here for string value "y"
     
     
     
    		System.out.println("Thank you for participating");
     
     
    	}
     
    }

    I am greatfull if someones spots the error of my algorithm.

    thanks
    -Sohail


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: do while loop terminating to early

    Don't use == to compare Strings. Use the equals() method instead. The == operator determines whether two Objects are the same instance, the equals() method checks for semantic equality.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: do while loop terminating to early

    Thanks a lot, I altered my conditional and looping code to the following and it worked:

    do 
    		{
     
    		System.out.print("Enter integer: ");
    		System.out.flush();
    		tempInt=Integer.parseInt( stdin.readLine());
     
    	       System.out.println("The square root of " +tempInt+ " is "+calcRoot(tempInt));
     
     
     
    	       System.out.print("Do you want to continue? (y / n): ");
     
     
    	       System.out.flush();
     
     
    			/* Code to check if the read line provided y or n, if y continue loop else
    			 *  exit loop and print "Thank you for participating"
    			 */
     
    			sCont=stdin.readLine();
    			 if(sCont.equals("n"))
    			    {
    			    	break;
     
    			    }
     
     
    			} while	(sCont.equals("y"));
     
     
     
    		System.out.println("Thank you for participating");

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: do while loop terminating to early

    For Strings, you might also want to check out the equalsIgnoreCase() function.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  2. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  3. why the while loop is not terminating? plz help me out in this..
    By ab7 in forum Loops & Control Statements
    Replies: 3
    Last Post: December 30th, 2011, 01:48 AM
  4. For loop; Problems with my for loop
    By mingleth in forum Loops & Control Statements
    Replies: 5
    Last Post: November 16th, 2011, 07:24 PM
  5. [SOLVED] allow a new input, dicarding the last mismatch input without terminating the program
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2010, 04:44 AM