do while error checking help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • powerej
    New Member
    • Sep 2007
    • 6

    #1

    do while error checking help

    program is suppose to ask for an input from 1-8, if the user enters a number outside of this range than it will continue to ask until it does, something is wrong and i cant seem to figure it out, i am stuck. Also my if else to see if the user catches the wave seems right to me but only works like half the time. I just really want to know what i am doing wrong here.

    Code:
       import javax.swing.*; // gets option pane
       import java.text.DecimalFormat; //formats decimals
    	
        public class Lab8  {
          
          public static  final int G = 32; //a constant
          public static double waveSpeed;  //a class double variable
          public static String userInput;
       	// method that generates a random wave
           public static double randomWaveSpeed () {
             int depth =	randomNumber(1, 20);
             int length = randomNumber(1, 15);
             waveSpeed = depth / length;
          			
            // determining which wave is selected
             if (depth >= .5){
               
                return deepWaveSpeed(length);
             }
             else 
             
                return shallowWaveSpeed(depth);
          }
          // method that generates a random number
           public static int randomNumber(int low , int hi ){
          
             return (low + (int)(Math.random() * hi));
          }
       		
       	// method that determines the speed of a wave in shallow water	   
           public static double shallowWaveSpeed(int depth ){
            
    		   return Math.sqrt((G*depth));
          }
          // method that determines the speed of a wave in deep water
           public static double deepWaveSpeed(int length ){
            
    		   return  Math.sqrt((G*length)/(2*Math.PI));
          }
        
          // method that displays whether or not the user caught the wave from 
          // the paddle speed entered
           public static void displayRideOutcome(double paddleSpeed ){
             String output;   
          	        
             if(paddleSpeed > waveSpeed){
               
                DecimalFormat twoDigits = new DecimalFormat("0.00");
             
             // generating output
                output = "Wave speed = " + twoDigits.format(randomWaveSpeed()) + "(ft/sec)" + "\n";
                output += "Paddle speed = " + twoDigits.format(paddleSpeed) + "(ft/sec)" + "\n";      	
                output +=  "You were able to catch the wave.";
             }
             
             else{   
               
                DecimalFormat twoDigits = new DecimalFormat("0.00");
             
             // generating output
                output = "Wave speed = " + twoDigits.format(randomWaveSpeed()) + "(ft/sec)" + "\n";
                output += "Paddle speed = " + twoDigits.format(paddleSpeed) + "(ft/sec)" + "\n"; 
                output += "You were not able to catch the wave.";
             }
          
             //outputing with JOptionPane                
             JOptionPane.showMessageDialog(null, output, "Lab8 (Edward Powers)", JOptionPane.PLAIN_MESSAGE);
          }
          
            // main method			
           public static void main (String[] args)  {
             String userInput;
             double paddleSpeed; 
             int option;     
          
          // a do while loop til the user clicks no
             do {
                do{
                   userInput = JOptionPane.showInputDialog("What is your paddle speed (ft/sec)?" + "\n" +
                      											"Valid Range is (1.0 to 8.0)");
                
                // if statement to cancel prog with error message if the user click cancel
                   if (userInput == null)
                   {
                      JOptionPane.showMessageDialog(null, "User clicked cancel.  The program will exit.",
                         "Lab8 (Edward Powers)", JOptionPane.ERROR_MESSAGE );
                      System.exit(0);
                   }	          
                  
                   paddleSpeed = Double.parseDouble(userInput);
                   if (paddleSpeed < 1 | paddleSpeed > 8){
                      userInput = JOptionPane.showInputDialog("What is your paddle speed (ft/sec)?" + "\n" +
                         										"Valid Range is (1.0 to 8.0)");}
                   else{          
                      displayRideOutcome ( paddleSpeed);
                   }		
                   option = JOptionPane.showConfirmDialog(null, "Would you like to continue?", "Lab7 (Edward Powers)", JOptionPane.YES_NO_OPTION );
                
                }
                while (option == JOptionPane.YES_OPTION);
             
             }
                while ( paddleSpeed >= 1 | paddleSpeed <=8);
          }
       }
  • Koolioman
    New Member
    • Oct 2007
    • 11

    #2
    I'm no expert, but I have a working way to use if/else logic and have it reject numbers that are out of the range.

    Code:
    String^ aString = System::Convert::ToString(textBox1->Text);
    float a = System::Convert::ToSingle(textBox1->Text);
    
    if (aString->Length == 0)
    {
    MessageBox::Show(" Unable to calculate");
    }
    
    else if (a > 8)
    {
    MessageBox::Show("Unable to calculate");
    }
    else
    {
    MessageBox::Show("Good answer!");
    }
    Make sure that you use the brackets exactly the way i did, or it won't work right.
    If there is only one line of code after the if/else, then you don't need brackets.

    Comment

    Working...