I am writing a simple start-up java program. The problem I am facing is that the company name is not being added properly.
public class Company {
public static BufferedReader br;
public static BufferedReader br1;
public static String numberOfCompanies;
public static void main(String[] args) {
// TODO Auto-generated method stub
CompanyDetails qw = new CompanyDetails();
try{
//Scanner in = new Scanner(System.in);
br = new BufferedReader(new InputStreamReader(System.in));
br1 = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number of companies: ");
numberOfCompanies = br.readLine();
int G = Integer.parseInt(numberOfCompanies);
for (int i = 1; i <= G; i++) {
qw = new CompanyDetails();
System.out.println("Enter name of the company: ");
String company = br1.readLine();
qw.company(company, i);
}
for (int i = 0; i <= G; i++) {
qw.companySummary(G);
}
} catch (IOException io) {
io.printStackTrace();
}
}
}
class CompanyDetails {
String company, name;
public String input;
public static BufferedReader br;
public double iE;
public static String numberOfCompanies;
String nameOfCompany;
String[] nameofCompany1 = new String[100];
int ir,i,employee;
ArrayList<String> bulk = new ArrayList<String>();
public String[] company(String input, int i) {
// TODO Auto-generated method stub
//ArrayList bulk = new ArrayList();
//for(int ith = i; ith<= 2; i++){
nameOfCompany = i+input;
bulk.add(nameOfCompany);
bulk.add(nameOfCompany);
// }
return nameofCompany1;
}
public void employee(double d) {
// TODO Auto-generated method stub
ir = (int)d;
}
public void companySummary(int G) {
System.out.println("Number of companies: " + G);
System.out.println("Name of company: " +bulk +" ");
System.out.println("Number of employees: "+ir);
}
}
The output I am getting is

Why aren’t I getting 234 at the position 1 of the arraylist ??
You are creating a new
CompanyDetailsobject in each iteration of the loop, there by loosing your earlier object:You are already creating an
CompanyDetailsobject at the start ofmainmethod:So you don’t have to do it again in the for loop.