Hi, Im new to java and I can not figure out where I am going wrong in this code. I need to take the value from the arguments in the run configuration. I the values are in args[] array and if there is more than one argument I need to print error, but I just am stuck on that part of the code. Here is what I have so far:

public class Numbers {
	static int f(int n) {
		int product = 1;
		while (n > 0) {
			product *= n;
			n -= 1;
		}
		return product;
	}

	public static void main(String[] args) {
		for (int i = 1; i <= 10; i++)
			System.out.println(f(i));
}		
}

Thank you for your time

Recommended Answers

All 4 Replies

Treat the args array that is passed to the main method just like you treat or use any other array.
How do you get/check the number of elements in an array? The array object has a property that you can use to get that number.

Okay: Before your for loop try out this code;

BufferedReader bf = new BuffereReader(new InputStreamReader(System.in));
String enteredNumber = bf.readLine()
int enteredValue = Integer.parseInt(enteredNumber);
for(int i=0; i<enteredValue; i++)

// please do not forget that you need to import java.io.* at the beginning of your program

@edensigauke: the OP's requirement is to get the value from the runtime parameter(s) args[], so your suggestion about System.in is more likely to confuse than help.

@coco24: although @edensigauke's code is not what you need, there is one line that you should have a look at - it's concerned with converting a String (like an element of args[]) into an int (like your method f needs)

@coco24----what are you trying to achieve through your code...? As you said " I need to take the value from the arguments in the run configuration" So you are passing the value of counter variable i to method f() instead of taking the value from user at run time.

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.