• Post Reply Bookmark Topic Watch Topic
  • New Topic

New Project New Questions

 
Kris French
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello once again!

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!

Kris
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kris French
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J,

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.

Kris
 
Dave Tolls
Ranch Hand
Posts: 2284
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kris French wrote:


I've cut out the irrelevant bits of code.
You never read in the number from the first dialog, so 'number' is still 0 when you get to the while loop. Hence no looping.

By the way, have you asked this somewhere else? I'm sure I answered about the 'count' problem somewhere today...
 
Kris French
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No Dave this was this first for me, but I'll try to search; maybe I'll learn something Thank you for the help!

Kris
 
Dave Tolls
Ranch Hand
Posts: 2284
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kris French wrote:No Dave this was this first for me, but I'll try to search; maybe I'll learn something Thank you for the help!

Kris


It's quite possible I answered here but didn't actually hit the submit button.
It is a Friday after all.
 
Kris French
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing worse than trying to focus when you've had some bad beer and bad Chinese food on a Friday.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect you'll come across another bug soon related to your use of Scanner. You're entering values using a JOptionPane but reading from stdin.
 
Kris French
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Mike,

What do you mean by
you're entering values using a JOptionPane but reading from stdin.
?
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When given requirements such as:
  • 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

    Links to specific required information:
    JOptionPane.showInputDialog(Object)
    JOptionPane.showMessageDialog(Component, Object)

    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...
     
    Kris French
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Rico; that's exactly what I needed.

    Kris
     
    Mike. J. Thompson
    Bartender
    Posts: 689
    17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!