CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
John Michael Montuya
Level 10
Cebu City
  • 19.05.2019
  • 1603views
  • 9comments

Why is my code failing the last part of verification? Everything checks out but " The displayed result should match the task conditions for any input data. " Please help :(

Question about the task Arithmetic mean
Java Syntax,  Level 5,  Lesson 5
Resolved

Use the keyboard to enter numbers, and then calculate the arithmetic mean.
If the user enters -1, display the arithmetic mean of all entered numbers and end the program.
-1 should not be included in the calculation.

Here are some examples:
a) if you enter the numbers
1
2
2
4
5
-1
then we display
2.8

b) if you enter the numbers
4
3
2
1
-1
then we display
2.5

Hint: one of the solutions to this problem uses the following construct:

while (true) {
    int number = read the number;
    if (check whether the number is -1)
        break;
}

Requirements:
  • The program must read data from the keyboard.
  • The program should display data on the screen.
  • After entering -1, the program must correctly terminate.
  • If you sequentially enter the numbers 1, 2, 2, 4, 5 and -1, the program should display 2.8.
  • If you sequentially enter the numbers -100, 0, 100, and -1, the program should display 0.0.
  • If you sequentially enter the numbers 1 and -1, the program should display 1.0.
  • The displayed result should match the task conditions for any input data.
package com.codegym.task.task05.task0507; /* Arithmetic mean */ import java.io.*; public class Solution { public static void main(String[] args) throws Exception { //write your code here BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); float mean = 0; float sum = 0; float counter = 0; while (true){ String input = reader.readLine(); float number = Float.parseFloat(input); if (number==-1){ break; } else if (number<0 || number>0){ sum = sum+number; counter++; } } mean = sum/counter; System.out.println(mean); } }
0
Comments (9)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Hryniup
Level 8 , Oswiecim, Poland
19 May 2019, 11:24solution
What about this case? If you sequentially enter the numbers -100, 0, 100, and -1, the program should display 0.0. Ur else if statement is unnecessary imho.
+4
John Michael Montuya
Level 10 , Cebu City, Philippines
19 May 2019, 14:33
Ironically the unnecessary else if statement was the cause, so you are spot on! My logic was for the program to break if non-integers were entered but its actually pretty pointless :P I removed all the unnecessary crap and got it to work.
0
romeo
Level 5 , Paramaribo, Suriname
2 June 2019, 21:16
So how did you get the program to add the inputs together without if else? like this? else if (number != -1){ }
0
John Michael Montuya
Level 10 , Cebu City, Philippines
2 June 2019, 21:24
Just removed the extra "if" and left it as "else", not "else if": if (number==-1){ break; } else { sum = sum+number; counter++; }
+2
Joseph
Level 22 , United States
17 July 2019, 04:22
I've been having a problem with this and the similar adding task from before. If I declare and initialize any variables in main and then have a while loop in main the while loop will not use them. Then it doesn't output the value of "mean" outside the while loop. I've tried working the code how you have it (including the corrections) and it still doesn't work.
0
John Michael Montuya
Level 10 , Cebu City, Philippines
17 July 2019, 17:15
can you paste your code here?
0
Joseph
Level 22 , United States
17 July 2019, 21:29
Looks like I was wrong. The task verified and I passed it. For one I didn't think about the fact that it only displays after the while loop breaks, and that only happens when I input -1. The other reason was when doing the task on the site I was having trouble compiling it. It kept giving me an error on the line where I parseFloat inside the while loop. But on the ide I had no problem with that at all. Still not sure why that is.
0
John Michael Montuya
Level 10 , Cebu City, Philippines
17 July 2019, 21:50
Good stuff! The web IDE is actually wonky sometimes, so I recommend that you use IDEA for more complex tasks moving forward.
0
Joseph
Level 22 , United States
17 July 2019, 22:25
It does seem that way. Might have been my fault but who knows. Thanks for the reply!
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.