Click to See Complete Forum and Search --> : Problem with Java Algorithm Behavior: Please help


Ave_L
October 24th, 2010, 07:58 PM
Short Story: I am a student who was assigned a simple problem ( using what we've learned thus far) to write some Java code for a program to receive a user inputted number and then output how many cycles of square roots it takes to get the number below 1.01. That part was simple enough and the program I wrote performs the task.

Here is my problem: When I was checking for runtime errors I encountered a very strange behavior of the program. During certain situations it will display a message twice back to back. Myself and my professor can not understand why it does this as the code seems very straight forward. Hopefully some one here can run the code and/or explain why the code runs this way.

The error happens when you first enter a character like 'A' , then you enter the digit 1, then enter a character 'A' again . This will produce the System.out.print msg twice. Praying that some one will understand why it does this. Here is the code I wrote:



// Chap 4 handout sheet Question4 Square roots

import java.util.*;
import static java.lang.Math.*;

public class Q4squares
{
public static void main(String[] args)
{

Scanner scan = new Scanner(System.in);
double num1, num2 = 0;
boolean result;
char letter;


System.out.println("\nWelcome to the square root cycle Calculator!");
System.out.println("This program will prompt you to enter a number and then \ncalculate how many"
+ " cycles of square roots it takes until \nthe number is less then 1.01!"
+ "\nSeems weird right? Lets begin:");

do
{
do
{
/* This section aquires a user inputed number; tests to see if its
a double and tests to see if its less then 10*/


System.out.print("\nPlease enter a number (must be higher then 10): ");
result = scan.hasNextDouble();
while (!(result))
{
scan.nextLine();
System.out.print("Input must be a number. Please re-enter: ");
result = scan.hasNextDouble();
}
num1 = scan.nextDouble();
} while ( num1 < 11);

num2 = num1; // retain number for final display

/* This section performs the square calculation and output
of how many cycles it takes to get below 1.01 */


int count = 0;
for ( ;num1 > 1.01; count++)
{
num1 = Math.sqrt(num1);
}

System.out.println("The number " + num2 + " took " + count + " square root "
+ "cycles to be less then 1.01");

/* This section is to give the user a choice if they would like to enter another
number to calculate and tests to make sure the char thats inputed is a Y or N */


System.out.print("\nWould you like to enter another number?: Y(Yes) or N(No): ");
letter = scan.next().charAt(0);

while (!(letter == 'y' || letter =='Y' || letter == 'n' || letter == 'N'))
{
System.out.print("Please re-enter: Y(Yes) or N(No) ");
letter = scan.next().charAt(0);
}
} while (letter == 'Y' || letter == 'y');

System.out.println("Ok. Goodbye!");


}}

Ave_L
October 25th, 2010, 08:10 PM
No one has any ideas? I was thinking it had something to do with how the PC handles hasNext and deals with loops. I guess I'll just chalk this one up as a mystery.

laserlight
October 26th, 2010, 01:46 AM
Here is a simple test:
1. At the prompt for a number, enter 100
2. At the prompt for yes or no, enter y
3. At the prompt for a number, enter A

You should observe the same behaviour, i.e., the "Input must be a number. Please re-enter: " message is repeated. The reason is that after you entered 'y', the newline is left in the input buffer, and this is discarded by the scan.nextLine() in the first iteration of the input error checking loop that prints the message. Consequently, the actual invalid input of 'A' is only discarded on the next iteration, hence the error message is printed twice.

A solution is to read in from standard input using scan.nextLine() only. You then store the line in a string, and parse the string for a double, e.g., by creating another Scanner based on the string. Likewise, when you want to read the yes or no input, read the input as a line.

Ave_L
October 26th, 2010, 08:51 AM
Thank you so much for replying Laserlight! That problem has been (de)bugging me all weak. Bad joke? Ah well. You've been a great help, thank you. :)