I hope I'm not making a pest of myself, but I do like the way your answers/questions help me figure things out on my own and help me better understand Java. I do have a class project that has some restrictions. I have read and understand the details, and even have a plan to build the program (I'm taking Campbell's advice and building it in small pieces first, then adding to the whole.) The project description is:
This project focuses on the use of of sequential, selective and repetitive programming statements, methods, and modular programming.
1. Design and implement a Java program that will gather a group of floating point numbers and determine the sum and average of the data entered. The program should use separate methods for inputting the data, calculating the sum, calculating the average, and displaying the results. A sentinel value should be used to indicate the user has completed entering their numbers. The output should display a message that includes the count of the numbers entered, the sum of the numbers and the average of the numbers. If the sum of the numbers is greater than 100, a warning message should be displayed indicating “values have exceeded a sum 100”.
2. Additional requirements include:
1. Use JOptionPane.showInputDialog() methods for your user to input their data
2. Use JOptionPane.showMessageDialog() methods to display your messages.
I have started by asking the user to input a number using the JOptionPane.showInputDialog; checks good.
Now I am working on getting the program to count the amount of numbers entered. I felt this was a logical next step. After much trial and error, I finally got the program to output the number of times numbers were entered. The problem I am running into is that it seems that the program terminates after only one input is made. It seems like the program is skipping the while statement entirely. Below is the code:
Thank you in advance for the help! I can't wait until I'm proficient enough to pay it forward!
Are you sure this is the right version of your code? From what I see, your count variable is never going to be more than 1.
"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
posted 5 years ago
You are right; I changed the count variable to 1 and all it did was add 1 + count. I never noticed before because the program terminated after inputting only one number. I probably would have encountered that if it worked the way I thought it would, but may have gotten a bit stuck then. Should I get rid of the = 0; and try to initialize count some other way? Thanks for the catch; I tend to get a bit of tunnel vision when I think I know exactly what the problem is.
Use JOptionPane.showInputDialog() methods for your user to input their data.
Use JOptionPane.showMessageDialog() methods to display your messages.
Which is a constraint on what must be used... In order to be effective, if you are not familiar with the resources that must be used in the problem... you must gather information about it -> JOptionPane API Documentation
By carefully reading the information in those links you will notice a flaw in the code that you currently have... You are not using the component’s API correctly...
To give you an insight... the purpose of the JOptionPane.showInputDialog(Object) is to retrieve information from the user because it returns a value of type String... which is whatever the user typed in the text-field
So to get you started... your code for retrieving the number the user entered using such requirement should be as follows:
Now with all this extra information and references you should be able to start over with a logical plan and design that can be implemented to fulfil your requirements...
posted 5 years ago
Thank you Rico; that's exactly what I needed.
Mike. J. Thompson
posted 5 years ago
Kris French wrote:Hello Mike,
What do you mean by
you're entering values using a JOptionPane but reading from stdin.
What I meant was that you are using an InputDialogue to request the user to enter a value, but then you're attempting to use a Scanner to try and read a value from stdin. As Rico pointed out, you were ignoring the value that the user entered into the InputDialogue.
I'm gonna teach you a lesson! Start by looking at this tiny ad:
create, convert, edit or print DOC and DOCX in Java