I am trying to initialise the array C in the loop, but it gives error :
C is array of class ipdata and I have declared it and trying to initialise it inside the loop.
import java.io.*;
import java.util.*;
public class cluster_anlysis {
class ipdata{
float a,b;
int cluster;
ipdata()
{
a=0;
}
}
public float modc(float a){
if(a<0.0)
a=-a;
return a;
}
public static void main(String[] args) {
cluster_anlysis obj=new cluster_anlysis();
ipdata C[] = new ipdata[50];
float mean1,mean2,mean3;
int i,j,n1=0,n2=0,n3=0,flag=0;
float ina=0.0f;
float inb=0.0f;
//BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Scanner scan =new Scanner(System.in);
System.out.println("pls enter no of data: ");
Integer no = scan.nextInt();
System.out.println("\nnow enter the x and y values");
for(i=0;i<no;i++)
{
ina=scan.nextFloat();
inb=scan.nextFloat();
System.out.println(ina);
C[i]= new ipdata(); // this line is giving error
C[i].a=ina;
C[i].b=inb;
C[i].cluster=0;
}
}
}
What could be the problem ?
it says :
No enclosing instance of type cluster_anlysis is accessible. Must qualify the allocation with an enclosing instance of type cluster_anlysis (e.g. x.new A() where x is an instance of cluster_anlysis).
you create an instance of inner class from static methods of your outer class using the outer class instance.
as you have alread created cluster_anlysis instance in your first line of main method.
you could simple do.
read about Inner classes in java
but if you want to create an inner class instance from the non-static methods of your outer class you dont need the instance of your Outer Class .
and also follow class name convention as posted by @A.R.S and @JB Nizet in their solutions.