# Number Programming NumberFor

• November 10th, 2012, 06:15 AM
programming-beginner
Number Programming NumberFor
Hi,
I've only just started java and I am trying to to make a program so that when a any line of digits are added it prints the count N for the numbers (how many numbers are in the line), the minumum and maximum of the line of numbers, their sum, and the average of all the numbers. Each of these specifications has to be printed on a new line.
The program has a to be called NumberFor
eg.
>NumberFor 22.0 42.5 15.2 -2 6
4
-2
42.5
77.1
19.275

Currently I have:

public class NumberFor {
public static void main(String[] args) {

double value = Double.parseDouble(args[0]);
double sum = 0.0;

//count N

// first value read initialized min and max
double min = max;

// read in the data, keep track of min and max
{
if (value > max) max = value;
if (value < min) min = value;
}

System.out.println(+ min);
System.out.println(+ max);

// sum of values
double N = Double.parseDouble(args[0]);
for (double i = 0.0; i < N; i++) {
sum += value;
}

System.out.println(+ sum);

//average
double cnt = 0;
while (!StdIn.isEmpty()){
sum += value;
cnt ++;
}
double average = sum / cnt;
System.out.println(+ average);
}

}

and the only output i can get to work is the max
Read more at numbers programming - CodingForums.com
• November 10th, 2012, 10:24 AM
Zaphod_b
Re: Number Programming NumberFor
Quote:

Originally Posted by programming-beginner
Hi

Hi.

Quote:

Originally Posted by programming-beginner
I've only just started java and I am trying to to make a program...

Well, no one was born knowing this stuff. Have you gone through any introductory material in a textbook or class notes or on-line tutorials or anything?

Quote:

Originally Posted by programming-beginner
trying to to make a program so that when a any line of digits are added ...

Trying to decipher the rest of your post, I infer that by "line of digits are added" you mean that your program is supposed to process command-line arguments.

So, for example if you execute

java NumberFor 22.0 42.5 15.2 -2 6

You want the program to treat 22.0, 42.5, 15.2, -2, and 6 as numbers. (Find min, max, sum, average of these five values, right?)

Well, when your main method is defined like this:
Code java:

` public static void main(String [] args)`
the command-line arguments are stored in an array of Strings named args

Here's how you "get at" the arguments in a program:
Code java:

```// // PrintArgs.java // public class PrintArgs { public static void main(String [] args) { System.out.println("Number of command-line arguments = " + args.length); for (int i = 0; i < args.length; i++) { System.out.println("args[" + i + "] = " + args[i]); } } // End main } // End class definition```

Here's a run. Program output is blue, user input is black:

java PrintArgs 22.0 42.5 15.2 -2 6
Number of command-line arguments = 5
args[0] = 22.0
args[1] = 42.5
args[2] = 15.2
args[3] = -2
args[4] = 6

Now that you know how to get to them, how would you complete your program?

Maybe wiith a loop that goes through all of the arguments, parsing each into a numeric value so that the values can be added together (one at a time inside the loop). Or some such thing.

Can you take it from there?

Cheers!

Z
• November 10th, 2012, 12:08 PM
minju
Re: Number Programming NumberFor
Quote:

Originally Posted by programming-beginner
// read in the data, keep track of min and max
{
if (value > max) max = value;
if (value < min) min = value;
}

You have braces{} but nothing before it, like if, while, for.