DEV Community

Zelyo
Zelyo

Posted on

Hello guys help me out in java xD

Hello guys i am trying to write some code by java >create an acc with randome number and store it in ArrayList > i made this code > but not working

import java.util.ArrayList;
import java.util.Random;
public class BankAcc {
public String name;
public int age;
public int id;

public BankAcc(String name,int age, int id){
    this.name=name;
    this.age=age;
    this.id=id;

}
public String getName(){
    return name;
}
public int getAge(){
    return age;
}
public int getId() {
    return id;
}
Random random =new Random();
ArrayList<Integer>arrayList=new ArrayList<Integer>();
while (ArrayList.size()>6){
    int a=random.nextInt(49)+1;
    if (!arrayList.contains(a)){
        arrayList.add(a);
    }
}
Enter fullscreen mode Exit fullscreen mode

}

Top comments (3)

Collapse
 
shadowdevil profile image
Shadow Devil

I think you wanted to do something like this:

public class BankAcc {
    private String name;
    private int    age;
    private int    id;
    Random             random    = new Random();
    ArrayList<Integer> arrayList = new ArrayList<Integer>();

    public BankAcc(String name, int age, int id) {
        this.name = name;
        this.age = age;
        this.id = id;
        for(int i = 0; i < 6; i++) {
            int a = random.nextInt(49) + 1;
            if (!arrayList.contains(a)) {
                arrayList.add(a);
            }
        }
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public int getId() {
        return id;
    }
}
Enter fullscreen mode Exit fullscreen mode

You cant use a while loop outside of a method or codeblock.

Collapse
 
zelyo profile image
Zelyo

this class safe the data

import java.util.ArrayList;
public class storeAcc {
private ArrayList BankAcc;
public storeAcc(){
BankAcc =new ArrayList();
}
public void addBankAcc(BankAcc acc){
BankAcc.add(acc);
}
}

Collapse
 
joelbonetr profile image
JoelBonetR πŸ₯‡

Can't you provide an example using any online java editor or similar? or at least tell us the error you get when compiling or whatever...