java - How to assign a number to an object in an array? -


package chapter10;

import java.util.arraylist; import java.util.list; import java.util.scanner;  public class customer {     private string name;     private  string streetaddress;        private  string phonenumber;     private  int total;      public string getname() {         return name;     }      public void setname(string name) {         this.name = name;     }            public  string getstreetaddress(){         return streetaddress;     }      public void setstreetaddress(string streetaddress) {         this.streetaddress = streetaddress;     }      public string getphonenumber() {         return phonenumber;     }        public void setphonenumber(string phonenumber) {         this.phonenumber = phonenumber;     }      public int gettotal(){           return total;            }      public void settotal(int total){         this.total = total;      }      public static void assign(){         int = (int) (math.random() + 10);          int r = (int) (math.random() + 10);          int f = (int) (math.random() + 10);          system.out.println(a + r + f + "x" + "x" + "x");     }      public static void main(string[] args)     {         scanner in = new scanner(system.in);         arraylist <customer > customerlist = new arraylist <customer>();         char ans;                 {             customer customer = new customer();              system.out.print("customer name ");             customer.setname(in.next()); int = 0; ++i;             system.out.print("street address ");             customer.setstreetaddress(in.next());              system.out.print("phone number ");             customer.setphonenumber(in.next());              customerlist.add(customer);             system.out.println("enter total sales ");             customer.settotal(in.nextint());              system.out.println("would enter in new customer       (    y/n)? ");             string answer = in.next();             ans = answer.charat(0);            ((string) customerlist).concat("")          } while(ans == 'y');          for(customer c: customerlist){             system.out.print(c.getname() + "\n" + "phone number         "            +c    .getphonenumber() +"\n" + "total sales "+ c.gettotal() + "\n" + "address       is"+ c.getstreetaddress());          }          for(int i=0; i<customerlist.size(); i++){             //system.out.print(customerlist.get(i).getname());         }     } } 

i need assign number each value in arraylist getting error says have convert string (arraylist). how add it?

if gather comments correct believe want:

your current assign() incorrect if want random values 1-10, should instead:

public string assign(){     random rand = new random();     int = rand.nextint(10) + 1;     int r = rand.nextint(10) + 1;     int f = rand.nextint(10) + 1;     return a+r+f+"xxx"; } 

customer this:

public class customer {     private string name;     private string customernumber;     private  string streetaddress;        private  string phonenumber;     private  int total;     ...     ...     ...     public string getcustomernumber() { return this.customernumber; }     public void setcustomernumber(string cnumber) { this.customernumber = cnumber; } 

and assigning numbers should this:

    for(customer c : customerlist) {         c.setcustomernumber(assign());     } 

also avoid line of code, it's bad idea:

    ((string) customerlist).concat("") 

you should rename customernumber customerid.


Comments