2 Replies - 1372 Views - Last Post: 25 February 2012 - 02:04 PM Rate Topic: -----

#1 moon alharbi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-February 12

why is it error ?

Posted 25 February 2012 - 11:52 AM

When you run a Java program called Foo, anything typed on the command line after “java Foo” is passed to the main method in the args parameter as an array of strings.
1. Write a program Average.java that just prints the strings that it is given at the command line, one per line. If nothing is given at the command line, print “No arguments”.
2. Modify your program so that it assumes the arguments given at the command line are integers. If there are no arguments, print a message. If there is at least one argument, compute and print the average of the arguments. Note that you will need to use the parseInt method of the Integer class to extract integer values from the strings that are passed in. If any non-integer values are passed in, your program will produce an error, which is unavoidable at this point.


I tried to solve it. but there is error

package foo;

public class Foo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		{ int[] ConvertArguments(String[] args1) ;

		throws NumberFormatException { int[] nums = new int[args.length];

		for(int i=0; i<args.length;++i)
		{ nums[i] = Integer.parseInt(args[i]); }
		return nums; } 

		private static void PrintArgumentsStr(ing[] args2) 
		{ for(int i=0; i<args.length;++i) 
		System.out.println(args[i]); } 

		private static double calculateAverage int[] nums
		{ double sum = 0;
		for(int i = 0; i<nums.length; ++i)
		sum += nums[i];
		return sum/nums.length;
		} 

		public static void mainString[] args;
		{ if(args.length > 0)
		{ PrintArguments(args); int[]
		nums = ConvertArguments(args);
		System.out.println("The average of the numbers is :"+ calculateAverage(nums)); }
		else
			System.out.println("No arguments! "); 		
} 
}
}

}
 


Is This A Good Question/Topic? 0
  • +

Replies To: why is it error ?

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: why is it error ?

Posted 25 February 2012 - 11:58 AM

Lines 10, 18, 22, and 29 are odd. Can you explain what they're supposed to be, and perhaps we can help you fix them.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: why is it error ?

Posted 25 February 2012 - 02:04 PM

Seems that uou are trying to create a method int[] ConvertArguments(String[])

A method cannot be inside a method (in your case main())
The method definition does not end with a ;
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1