Results 1 to 6 of 6

Thread: I'm stumped

  1. #1
    Budnnet is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default I'm stumped

    Im having a problem with code that I've written, the problem occurs when I try to prompt for user input at the end of the first loop. What happens is the println fires but the prompt never does and it exits the loop.
    Java Code:
    import java.util.Scanner;
    
    public class Lab3test
    {
      public static void main( String[] arguments )
      {
        
    //Variable declaration
        int added = 0;
        int subbed = 0;
        int multed = 0;
        int dived = 0;
        int moded = 0;
        double numberOne = 0;
        double numberTwo = 0;
        double minValue1 = Double.MAX_VALUE;  
        double minValue2 = Double.MAX_VALUE;
        double maxValue1 = Double.MIN_VALUE;
        double maxValue2 = Double.MIN_VALUE;
        String userInput;
                
        Scanner keyboard = new Scanner ( System.in );
        
        System.out.print ( "Input a number or press <Enter> to exit" );
        userInput = keyboard.nextLine();
                  
        while ( userInput.trim().length() > 0 )       
        {
          numberOne = Double.parseDouble (userInput);
          System.out.print ( "Input a operation +,-,*,/,%" );
          char operator = keyboard.next().charAt(0);
          while ( operator != '+' && operator != '-' && operator != '*' && operator != '/' && operator != '%' )
          {
            System.out.print ( "Invalid operation, input a valid operation +,-,*,/,%" );
            operator = keyboard.next().charAt(0);
          } 
                  
          System.out.print ( "\nInput a second number" );
          numberTwo = keyboard.nextDouble();   
          
          if ( numberOne > maxValue1) maxValue1 = numberOne;
          if ( numberTwo > maxValue2 && operator != '/' ) maxValue2 = numberTwo;
          if ( numberOne < minValue1) minValue1 = numberOne;
          if ( numberTwo < minValue2 && operator != '/' ) minValue2 = numberTwo;
                               
          if (operator == '+') 
          {  
            System.out.println ( numberOne + " + " + numberTwo + " = " + ( numberOne + numberTwo ));
            added++;
          }
                  
          else if ( operator == '-' ) 
          {
            System.out.println ( numberOne + " - " + numberTwo + " = " + ( numberOne - numberTwo ));
            subbed++;
          }          
              
          else if ( operator == '*' )
          {           
            System.out.println ( numberOne + " * " + numberTwo + " = " + ( numberOne * numberTwo ));
            multed++;
          }
                
          else if ( operator =='/' )
          {
            while ( numberTwo == 0 ) //Preventing divison by zero
            {
              System.out.print ( "Division by zero is undefined, Please enter another number" );
              numberTwo = keyboard.nextDouble();  
            }
            
            System.out.println ( numberOne + " / " + numberTwo + " = " + ( numberOne / numberTwo ));
            dived++;         
                     
            if ( numberTwo > maxValue2 ) maxValue2 = numberTwo;
            if ( numberTwo < minValue2 ) minValue2 = numberTwo;
          }
                  
          else if ( operator == '%' )         
          {
            System.out.println ( numberOne + " % " + numberTwo + " = " + ( numberOne % numberTwo ));
            moded++;
          }
          System.out.println ( "Input a number or press <Enter> to exit" );    //the problem occurs here this line executes
          userInput = keyboard.nextLine();              //this line does not and the loop exits
        }
        System.out.println ( "Checking the Min and Max values\n" );
        System.out.println ( "Min value for 1st: " + minValue1 + "\nMax value for 1st: " + maxValue1 );
        System.out.println ( "Min value for 2nd: " + minValue2 + "\nMax value for 2nd: " + maxValue2 );    
        System.out.println ( "\nChecking the status of the operations\n" );
        System.out.println ( "Added " + added + " times." );       
        System.out.println ( "Subtracted " + subbed + " times." );
        System.out.println ( "Multiplied " + multed + " times." ); 
        System.out.println ( "Divided " + dived + " times." );
        System.out.println ( "Modulused/Remaindered " + moded + " times.\n" ); 
        System.out.println ( "Thanks for Testing!!!" );
      }
    }
    Last edited by Budnnet; 03-03-2014 at 04:40 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: I'm stumped

    I suggest you write a small program to investigate reading in lines and characters from the console.
    You probably have a lingering <CR> somewhere in there. Also check the API for assistance.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,029
    Rep Power
    6

    Default Re: I'm stumped

    The API documentation of the Scanner class, for example. Very easy to do;

    - google "java 7 Scanner"
    - First hit should be: Scanner (Java Platform SE 7 )
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: I'm stumped

    it exits the loop
    Try debugging the code to see what the computer sees that causes the code to exit the loop: Add a println() statement that prints out the value of the variable that controls the looping. Seeing its values will help you understand what the code is doing.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Budnnet is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default Re: I'm stumped

    I figured out the issue, apparently scanner wanted consistent use of nextLine()

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,029
    Rep Power
    6

    Default Re: I'm stumped

    Good job dude. That's common pitfall #1 you tackled. On to the other dozen that exist (in other classes).

    You get extra credit if you can understand and explain WHY you need to do this, in stead of just thinking "jeez the Scanner class is stupid".
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. infinite loop has me stumped
    By D.good in forum New To Java
    Replies: 12
    Last Post: 02-16-2012, 09:49 PM
  2. Stumped on unknown problem
    By stevenfriz in forum New To Java
    Replies: 9
    Last Post: 02-16-2012, 09:55 AM
  3. Replies: 9
    Last Post: 01-29-2012, 03:03 AM
  4. Stumped... any help would be appreciated!
    By gard007 in forum New To Java
    Replies: 3
    Last Post: 12-01-2011, 07:42 AM
  5. stumped......
    By trueblue in forum New To Java
    Replies: 21
    Last Post: 07-13-2009, 04:16 PM

Posting Permissions

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