Hi i m having problem...
i want to give each random number to each month..
but it gives erros

public class a
{
public static void main(String[] args)
{
	float average = 0f;
	String[] Year = {"Jan","Feb","Mar","Apr","May","Apr"};
	int[] values = new int[5];

	for(int i=0; i<values.length; i++)
	{
		values[i] = (int)(100*Math.random());
		average+=values[i];

	}
	average=average/values.length;


	for(int j=0; j<Year.length; j++)
	{
		Year[j] = (values[j]);
	}

	System.out.println(average);

}
}

Error is
C:\Users\dell\Desktop\a.java:20: incompatible types
found : int
required: java.lang.String
Year[j] = (values[j]);
^
1 error

Tool completed with exit code 1

Well, yes, Year is an array of 6 Strings, values is an array of 5 ints. What exactly do you want to do? It looks like you are trying to overwrite the initial String values with ramdom ints - does this make sense? Anyway, you can't put ints into an array of Strings, and you can't get the 6th value from an array of length 5.

Well, yes, Year is an array of 6 Strings, values is an array of 5 ints. What exactly do you want to do? It looks like you are trying to overwrite the initial String values with ramdom ints - does this make sense? Anyway, you can't put ints into an array of Strings, and you can't get the 6th value from an array of length 5.

Sir,actually i m learning java and its a exercise practice..the question is..
create an array of string variables and initialize the array with the name of the months from jan to dec.create an array containing 12 random decimal values.dsplay the names of each month alng with the corressponding decimal value.calculate and display the average of the 12 decimal value...

this i want to do it. :(

It looks like you are 90% done. There's no requirement to copy the decimal values to the months, so your lines 18-22 are not needed. Take them out and see what happens next.

Also what you are referring to is not an Array at all. Many languages say an array is a key => value which is what you are saying you want. In Java there is a distinction between an Array (list) and a Map (Array in other languages, so on). So if you want a Map try using a standard HashMap.

Edited 5 Years Ago by Arrorn: n/a

Hi Farhad.idrees
Please don't worry about what Arrorn said. He's making a point that's far too advanced for where you are now. Given the question that you were asked what you did was right.

Comments
correct +1
This article has been dead for over six months. Start a new discussion instead.