import java.util.*;
import java.lang.*;
public class MyClass
{
String[] getdata()
{
Scanner in=new Scanner(System.in);
System.out.print("Enter numbers to calculate their average (choose 5 to 10 numbers only : ");
String[] a=in.nextLine().split(" ");
return a;
}
double average(String[] num)
{
double avg;
int tot=0;
int[] numbers = new int[num.length];
int l=num.length;
for(int j=0;j<l;j++)
{
tot=tot+numbers[j];
}
avg=tot/l;
return avg;
}
void results(String[] arr,double avg)
{
int[] numbers1=new int[arr.length];
int ll=arr.length;
System.out.print("The average of the numbers ");
for(int i=0;i<ll;i++)
{
numbers1[i]=Integer.parseInt(arr[i]);
System.out.print(numbers1[i]+" ");
}
System.out.print("is ");
System.out.printf("%.2f",avg);
}
public static void main(String args[])
{
MyClass obj=new MyClass();
String[] x=obj.getdata();
double y=obj.average(x);
obj.results(x,y);
}
}
The code is running successfully but for any given output the average is showing as 0.00.
The code takes in integers with space between them and calculates the average and displays it
Help me fix it. Thanks