Hi, I'm completely new to Java and having some trouble basically understanding everything. I have some code and am wondering how to edit it in order to make some questions that I'm being asked to work.

Here's the code:

import java.util.*; // for class Scanner
public class Copy {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
System.out.println("Ready to copy numbers!\n" +
"Enter Control-Z when done!");
double num;
while(stdin.hasNext()) {
num = stdin.nextDouble();
System.out.printf("%.2f\n", num);
}
System.out.println("Goodbye!");
System.exit(0);
}
}

Here is what I'm being asked:
Modify the above program to print the
a. Count of numbers input
b. The total of the numbers input
c. The average of the numbers input

Hint: Your program will need to have 2 variables, say n and total, that keep a running count of the
number of values read and the total up to that point.

Any help would be highly appreciated, thanks so much !

Dani AI

Generated

A compact, practical approach is to add two variables outside the input loop — an int counter and a double accumulator — update them for every numeric token read, and compute the average after end-of-file. The example below is a self-contained program that is a bit more robust than the original: it uses hasNextDouble() to avoid InputMismatchException, skips non‑numeric tokens, and prints count, total and average only if at least one number was read.

import java.util.Scanner;

public class NumberStats {
    public static void main(String[] args) {
        int count = 0;
        double total = 0.0;

        System.out.println("Enter numbers (EOF to finish). Ctrl-D on Unix/Mac or Ctrl-Z then Enter on Windows.");

        try (Scanner scanner = new Scanner(System.in)) {
            while (scanner.hasNext()) {
                if (scanner.hasNextDouble()) {
                    double num = scanner.nextDouble();
                    count++;
                    total += num;
                    System.out.printf("Read: %.2f%n", num);
                } else {
                    // skip any non-numeric token and continue reading
                    scanner.next();
                }
            }
        }

        if (count > 0) {
            double average = total / count;
            System.out.printf("Count: %d%nTotal: %.2f%nAverage: %.2f%n", count, total, average);
        } else {
            System.out.println("No numeric input received.");
        }
    }
}

Notes and clarifications: declare and initialize count and total before the loop so their values persist; use double for the running total (and for the final average) to avoid integer division; check count > 0 before dividing to avoid divide‑by‑zero. Using hasNextDouble() prevents exceptions when a non-numeric token appears; the code above simply skips such tokens — an alternative is to stop on the first non-number by using while (scanner.hasNextDouble()). This follows ’s hint about a counter, and keeps the change minimal relative to ’s original program.

Recommended Answers

All 7 Replies

Well why don't you try something. Your teacher wouldn't give you this assignment prior any lectures on Java, besides it is part of forum rules

Do provide evidence of having done some work yourself if posting questions from school or work assignments

I wouldn't post something if I hadn't tried already, I'm not looking for any freebies. I asked in my question if anyone could explain how to start it off, or where the code should be edited. I didn't ask for it to be done and given to me. My teacher gave this to us without any lectures, which is why I came here for explanation/help.

Work on the requirements one at a time:

. Count of numbers input

Define an int variable as a counter and set it to 0. Be sure to define it outside of the loop
Add one to the counter as each number is input.

okay so something like count = something? I don't really know what to put, I haven't been taught it :|

i'll look into it, thanks

There is a lot of material in the tutorial you need to read.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.