473,795 Members | 3,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

do while error checking help

6 New Member
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 1957
Koolioman
11 New Member
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
3207
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. SO please, if you can, please send me the php.ini file used by most hosts. -- Sharif Tanvir Karim http://www.onlyonxbox.net
2
4467
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 and handling. One area that has me puzzled is error checking some of the file system operations. Just simple things such as open, rename, unlink, print and some others ... all the sample code I've seen uses them in a form of: open(...) || die...
3
1621
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 advance <%@language="VBScript"%> <% option explicit %> <% response.buffer = True %>
17
2737
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 and reporting script errors easy and painless. This could be accomplished easily if window.onerror were ubiquitous, but it is not. My new idea is to hack together something superficially resembling C's assert() macro, perhaps something like ...
1
1562
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 of errors in my solution. It slows down my PC so badly that it is hard to get anything done at all. I have a 2.8GHz P4 with 2gig of RAM. I have searched everywhere I can think, including this newsgroup and haven't found anything yet.
16
2637
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 where someone suggested to me I was spending too much time writing error messages, and that I was therefore missing the benefit of using a scripting language. The idea, apparently, is that the PHP interpreter writes all the error messages that are...
11
4276
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 will require individual builds for each device so that the device serial number is contained in the program memory. To do this, the C application must be compiled with the serial number assigned to a variable within the source code file. I...
0
968
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 automatically updated onto the database - My problem is - when user goes to the nested table the application is still looking for parameters to update the database - which is causing errors - how will I prevent this from happening on the second...
7
3707
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 everytime the message box appears and you select ok the form closes does any body no of a solution the code for the ok button is: Sub ClickOKButton() Dim rsJobs As DAO.Recordset Dim rsCust As DAO.Recordset Dim NewCustId As Long
0
10435
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10163
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.