Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Code for program that finds the average of positive numbers higher than 0.

  1. #1
    Junior Member
    Join Date
    Oct 2022
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Code for program that finds the average of positive numbers higher than 0.

    Here is my code I think it works alright, but when I ran tests for my homework via replit it shows these errors-
    Exception in thread "main" java.lang.NumberFormatException: For input string: "-1 -2 -3 0"
    at java.base/java.lang.NumberFormatException.forInputString(Num berFormatException.java:67)
    at java.base/java.lang.Integer.parseInt(Integer.java:668)
    at java.base/java.lang.Integer.parseInt(Integer.java:786)
    at Main.main(Main.java:9)
    Only one test passed the output was 0, when I was putting these numbers manually from the input test the program gave me all the right answers needed for the tests.
    Could someone tell me what's wrong with it how should I simplify it or upgrade it so it passes my tests on the replit site?
    import java.util.Scanner;

    class Main {
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int count = 0;
    int sum = 0;
    while (true) {
    int number = Integer.parseInt(scanner.nextLine());
    if (number == 0) {
    break;
    } else if (number > 0) {
    count++;
    sum += number;
    }
    }
    if (count > 0) {
    System.out.printf("%.2f%n", sum / (double) count);
    } else {
    System.out.println("0.0");
    }
    }
    }

  2. #2
    Junior Member
    Join Date
    Oct 2022
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Code for program that finds the average of positive numbers higher than 0.

    Bump, please!

  3. #3
    Junior Member
    Join Date
    Sep 2022
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Code for program that finds the average of positive numbers higher than 0.

    import java.util.Scanner;
     
    class Main {
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int count = 0;
    int sum = 0;
    while (true) {
    int number = Integer.parseInt(scanner.nextLine());
    if (number == 0) {
    break;
    } else if (number > 0) {
    count++;
    sum += number;
    }
    }
    if (count > 0) {
    System.out.printf("%.2f%n", sum / (double) count);
    } else {
    System.out.println("0.0");
    }
    }
    }

Similar Threads

  1. Adding and Subtracting positive or negative two numbers of any length with strings
    By javabeginner63 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 5th, 2014, 05:11 PM
  2. (Java) Help with alternating negative and positive numbers.
    By fcknDan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 16th, 2013, 12:48 AM
  3. Loops: Taking the average of a list of numbers
    By janeeatsdonuts in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 24th, 2013, 09:38 AM
  4. Replies: 1
    Last Post: November 26th, 2011, 12:13 PM
  5. Replies: 1
    Last Post: November 6th, 2011, 09:48 PM