CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2010
    Posts
    3

    Problem with Java Algorithm Behavior: Please help

    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!");


    }}

  2. #2
    Join Date
    Oct 2010
    Posts
    3

    Re: Problem with Java Algorithm Behavior: Please help

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Problem with Java Algorithm Behavior: Please help

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Oct 2010
    Posts
    3

    Re: Problem with Java Algorithm Behavior: Please help

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured