473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1.    import javax.swing.*; // gets option pane
  2.    import java.text.DecimalFormat; //formats decimals
  3.  
  4.     public class Lab8  {
  5.  
  6.       public static  final int G = 32; //a constant
  7.       public static double waveSpeed;  //a class double variable
  8.       public static String userInput;
  9.        // method that generates a random wave
  10.        public static double randomWaveSpeed () {
  11.          int depth =    randomNumber(1, 20);
  12.          int length = randomNumber(1, 15);
  13.          waveSpeed = depth / length;
  14.  
  15.         // determining which wave is selected
  16.          if (depth >= .5){
  17.  
  18.             return deepWaveSpeed(length);
  19.          }
  20.          else 
  21.  
  22.             return shallowWaveSpeed(depth);
  23.       }
  24.       // method that generates a random number
  25.        public static int randomNumber(int low , int hi ){
  26.  
  27.          return (low + (int)(Math.random() * hi));
  28.       }
  29.  
  30.        // method that determines the speed of a wave in shallow water       
  31.        public static double shallowWaveSpeed(int depth ){
  32.  
  33.            return Math.sqrt((G*depth));
  34.       }
  35.       // method that determines the speed of a wave in deep water
  36.        public static double deepWaveSpeed(int length ){
  37.  
  38.            return  Math.sqrt((G*length)/(2*Math.PI));
  39.       }
  40.  
  41.       // method that displays whether or not the user caught the wave from 
  42.       // the paddle speed entered
  43.        public static void displayRideOutcome(double paddleSpeed ){
  44.          String output;   
  45.  
  46.          if(paddleSpeed > waveSpeed){
  47.  
  48.             DecimalFormat twoDigits = new DecimalFormat("0.00");
  49.  
  50.          // generating output
  51.             output = "Wave speed = " + twoDigits.format(randomWaveSpeed()) + "(ft/sec)" + "\n";
  52.             output += "Paddle speed = " + twoDigits.format(paddleSpeed) + "(ft/sec)" + "\n";          
  53.             output +=  "You were able to catch the wave.";
  54.          }
  55.  
  56.          else{   
  57.  
  58.             DecimalFormat twoDigits = new DecimalFormat("0.00");
  59.  
  60.          // generating output
  61.             output = "Wave speed = " + twoDigits.format(randomWaveSpeed()) + "(ft/sec)" + "\n";
  62.             output += "Paddle speed = " + twoDigits.format(paddleSpeed) + "(ft/sec)" + "\n"; 
  63.             output += "You were not able to catch the wave.";
  64.          }
  65.  
  66.          //outputing with JOptionPane                
  67.          JOptionPane.showMessageDialog(null, output, "Lab8 (Edward Powers)", JOptionPane.PLAIN_MESSAGE);
  68.       }
  69.  
  70.         // main method            
  71.        public static void main (String[] args)  {
  72.          String userInput;
  73.          double paddleSpeed; 
  74.          int option;     
  75.  
  76.       // a do while loop til the user clicks no
  77.          do {
  78.             do{
  79.                userInput = JOptionPane.showInputDialog("What is your paddle speed (ft/sec)?" + "\n" +
  80.                                                               "Valid Range is (1.0 to 8.0)");
  81.  
  82.             // if statement to cancel prog with error message if the user click cancel
  83.                if (userInput == null)
  84.                {
  85.                   JOptionPane.showMessageDialog(null, "User clicked cancel.  The program will exit.",
  86.                      "Lab8 (Edward Powers)", JOptionPane.ERROR_MESSAGE );
  87.                   System.exit(0);
  88.                }              
  89.  
  90.                paddleSpeed = Double.parseDouble(userInput);
  91.                if (paddleSpeed < 1 | paddleSpeed > 8){
  92.                   userInput = JOptionPane.showInputDialog("What is your paddle speed (ft/sec)?" + "\n" +
  93.                                                              "Valid Range is (1.0 to 8.0)");}
  94.                else{          
  95.                   displayRideOutcome ( paddleSpeed);
  96.                }        
  97.                option = JOptionPane.showConfirmDialog(null, "Would you like to continue?", "Lab7 (Edward Powers)", JOptionPane.YES_NO_OPTION );
  98.  
  99.             }
  100.             while (option == JOptionPane.YES_OPTION);
  101.  
  102.          }
  103.             while ( paddleSpeed >= 1 | paddleSpeed <=8);
  104.       }
  105.    }
  106.  
  107.  
Oct 19 '07 #1
1 1943
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.

Expand|Select|Wrap|Line Numbers
  1. String^ aString = System::Convert::ToString(textBox1->Text);
  2. float a = System::Convert::ToSingle(textBox1->Text);
  3.  
  4. if (aString->Length == 0)
  5. {
  6. MessageBox::Show(" Unable to calculate");
  7. }
  8.  
  9. else if (a > 8)
  10. {
  11. MessageBox::Show("Unable to calculate");
  12. }
  13. else
  14. {
  15. MessageBox::Show("Good answer!");
  16. }
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.
Oct 19 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Sharif Tanvir Karim | last post by:
Can anyone send me a php.ini file that is used by almost by all hosts? My damn computer's (winxp) mini server is messed up and I am starting over so I am trying ot get php to behave on my system....
2
by: Marc S. Gibian | last post by:
I am putting together a Perl tool to manage a large set of tasks related to building a rather complex set of software. One of the goals for moving to Perl is to provide very rigorous error checking...
3
by: Steven Scaife | last post by:
Below is my ASP page, I have changed the update to read DRIAL which doesn't exist, shouldnt this throw an error, or if the connection cannot be made shouldnt it throw an error as well thanks in...
17
by: Christopher Benson-Manica | last post by:
All right, since my previous idea (calling functions through a wrapper) was apparently so awful no one could suggest any improvements, I'll try a different tack. My end goal is to make detecting...
1
by: eric | last post by:
Hello, Does anyone know how to turn off the constant error checking that VB.NET performs in the background while code is being edited? I am working on a migration project and VS finds thousands...
16
by: lawrence k | last post by:
I've made it habit to check all returns in my code, and usually, on most projects, I'll have an error function that reports error messages to some central location. I recently worked on a project...
11
by: Bryan Crouse | last post by:
I am looking a way to do error checking on a string at compile time, and if the string isn't the correct length have then have the compiler throw an error. I am working an embedded software that...
0
by: KA NMC | last post by:
I have One Datagrid with a dataset that calls two tables - the First table is the table the user will be editing - which edited on the grid or in a textbox - as the user edits the grid - that info is...
7
by: liam sheerin | last post by:
Hi i am fairly new to vba and have a taxi booking database. every time you book a job the cust_name field must be filled out and same if account job for account_ref and account_password fields but...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.