How to intialise objects in the array list - java

I am trying to initialize objects in my ArrayList
The following is my code and what it does is that it receives an ArrayList, already defined in the main method. However, it is not executing the readInput method in the code below. readInput is a method declared in another class called customer that takes in keyboard input from the user. Its a complete class BTW. I believe that there is no object in the ArrayList at the moment as i have run some test to see whats the problem. Im new to using Arraylist. Therefoere i am unsure of how to solve this proble.
What custs.set(i, new customer()); does is actually to initialize the object in index.
public static void freeCustomer(ArrayList<customer> custs,
ArrayList<supplement>supps)
{
for(int i=0;i<custs.size();i++)
{
custs.set(i, new customer());
custs.get(i).readInput();
for(int s=0;s<supps.size();s++)
{
supps.set(s, new supplement());
supps.get(s).readInput();
}
}
}

If I understood right,you want to add user input from a customer method.I would use add method.I just made a fast example of add method.From here you can expand to loops,etc.
public static void freeCustomer(ArrayList<customer> custo,customer c)
{
custo.add(new customer(c.readInput()));
}
public static void main(String[] args) {
ArrayList<customer> custo = new ArrayList<customer>();
customer c=new customer("");
freeCustomer(custo,c);
}
}

Related

ArrayList errors

I have a code
class testArrayList
{
ArrayList<String> auto = new ArrayList<String>();
auto.add("MITSUBISHI");
auto.add("Hyundae");
auto.add("Ford");
auto.add("Ferrari");
auto.add("Mazda");
auto.add("Mustang");
auto.add("Lamborghini");
for(String cars : auto)
{
System.out.println(cars);
}
}
but when I compiled it, theres an error saying
Im confused why it has an error saying IDENTIFIER EXPECTED or ILLEGAL START OF TYPE tho I already imported import java.util.ArrayList;
You have to change your code as below. You need to add array list inside to the main method.
public class testArrayList {
public static void main(String[] args) {
ArrayList<String> auto = new ArrayList<String>();
auto.add("MITSUBISHI");
auto.add("Hyundae");
auto.add("Ford");
auto.add("Ferrari");
auto.add("Mazda");
auto.add("Mustang");
auto.add("Lamborghini");
for(String cars : auto)
{
System.out.println(cars);
}
}
}
You can put logic ONLY inside methods. The main-method is the method that will be executed when u run the program. You can only put declarations and methods inside a class, thats why you get the runtime errors. I won't do a duplicate, just take Anuradha's solution.
auto is not a keyword and it’s fine in java. Change theSyntax of arrayList it to something
List<String> auto= new ArrayList<>();

Java ArrayList a good way to access same list from many classes

Hello i am trying to familiarize myself with Java by doing a very simple "bankaccount" application and it doesn't even save to db or something so it resets all data on rerun.
The problem i am trying to find a good way of doing is that i have an ArrayList of accounts that i want to be able to access from any class so that during runtime for example after an deposit if i access that account later when i want to get balance i get an that account from the ArrayList and it is updated to the deposit value.
When googling i found this solution but i dont like it since it uses static ArrayList. is there any more elegant way than this for an applicaiton that only saves the state/data during runtime.
Simple class that adds the test accounts and so on where first value is acountId and second is balance
public class AccountsModel {
private ArrayList<AccountModel> listOfAccounts;
public AccountsModel() {
listOfAccounts = new ArrayList<AccountModel>();
listOfAccounts.add(new AccountModel(1,0));
listOfAccounts.add(new AccountModel(2,0));
listOfAccounts.add(new AccountModel(3,0));
listOfAccounts.add(new AccountModel(4,0));
}
public ArrayList<AccountModel> getListOfAccounts(){
return listOfAccounts;
}
}
Then in my main class i just do this
static AccountsModel accounts = new AccountsModel();
public static ArrayList<AccountModel> listOfAccounts = accounts.getListOfAccounts();
this "works" as i can get the same list from anywhere within the application. But is there any simple and elegant way of doing this some other way?
You said you dislike the static solution but to me "It needs to be accessed by many classes" screams static variables.
Basically, you create a wrapper for your ArrayList which carries out operations:
class AccountsModel {
private static ArrayList<AccountModel> singleton;
// a static constructor also wouldn't be a bad idea here
public static void init() {
/* add a bunch of AccountModels here*/
}
public static ArrayList<AccountModel> getAccounts() {
return singleton;
}
}
An example of a main method:
public static void main(String[] args) {
ArrayList<AccountModel> accounts = AccountModels.getAccounts();
}

How to define a method that would take an object as a parameter and return that into another object?

I have to classes, one is a SmartCard and another one is CardLock.
The SmartCard class creates a new object with a name and staff status that can be true or false.
Now, CardLock class is supposed to have a method where I can swipe a card and get the information from the last card that was swiped.
My code looks like this:
public class CardLock{
SmartCard lastCard;
public SmartCard swipeCard(SmartCard newCard){
lastCard = newCard;
}
public SmartCard getLastCardSeen(){
return lastCard;
}
public static void main(String[] args){
System.out.println("Swiping card");
SmartCard cardA = new SmartCard("Anna Undergrad", false);
swipeCard(cardA);
System.out.println(cardA.getOwner() + " card swiped.");
SmartCard cardB = new SmartCard("Dr. John Turpentine", true);
System.out.println("The last card swiped was by " + cardA.getLastCardSeen().getOwner());
}
Now, I get an error "non-static method SwipeCard(SmartCard) cannot be referenced from a static context", which I have difficulties in understanding.
Another error is down at cardA.getLastCardSeen().getOwner() where it fails to locate getOwner method even though it is in SmartCard and is public.
Thanks for the thelp.
The swipeCard method is defined for an CardLock object. You need a CardLock object on which to invoke that method.
I don't exactly get what you are trying to achieve but the reason you are getting the "non-static method SwipeCard(SmartCard) cannot be referenced from a static context" error is because main method is static and your swipeCard() method is not. Make it static and the error will go away. You can only reference static methods from withing a static method.
Also cardA object is of SmartCard type. Since you have not posted that class I can't really be sure but I think SmartCard class doesn't have that method. Can you be a bit more specific of what you are trying to do and post the code for the SmartCard class. I edited your code below see if this helps.
public class CardLock {
SmartCard lastCard;
public static void swipeCard(SmartCard newCard) {
lastCard = newCard;
}
public static SmartCard getLastCardSeen() {
return lastCard;
}
public static void main(String[] args) {
System.out.println("Swiping card");
SmartCard cardA = new SmartCard("Anna Undergrad", false);
swipeCard(cardA);
System.out.println(cardA.getOwner() + " card swiped.");
SmartCard cardB = new SmartCard("Dr. John Turpentine", true);
System.out.println("The last card swiped was by " + getLastCardSeen().getOwner());
}
}
First your function SmartCard swipeCard(SmartCard newCard) is not well defined.
You should define it like:
public void swipeCard(SmartCard newCard)
if you want it to perform as you write it.
Next, you can't call swipeCard(cardA); like this because it is not a static method. If you want to do it so you have to add the keyword static to your function like:
public static void swipeCard(SmartCard newCard)
You can read more about static methods here Static methods vs instance methods java
And finally, it will be helpful to show us a bit of your SmartCard class.

Android Studio public object arraylist (object database)

I'm making a game on Android Studio and it will have all sorts of items with their own values. I need to make a list of objects which can be modified from other classes. For example, I have class Tool and I want to make a list of Tool objects which I can modify from other classes.
This is what I've got so far:
public void StartGame() {
ArrayList<Tool> tools = new ArrayList<>(); //ID, Name, Owned, Fight, Resource, Building, Crafting, Clothing
tools.add(new Tool(1,"Hatchet",false,2,0,0,0,0));
tools.add(new Tool(2,"Pocket Knife",false,0,0,0,0,0));
}
For now I can create all the items when I start the game and modify the list from within the StartGame() itself. But what I want to achieve is that whenever the game starts I set all the list items value owned = false and whenever I need to, I can modify them once again.
Make Utils class like this.
public class Utils{
public static List<Tools> myList;
public static List<Tools> getMyList() {
return myList;
}
public static void setMyList(List<Tools> myList) {
test.myList = myList;
}
public static void changeLogic(){
//change ligic
}
}
and use Utils class where ever you want
Utils.setList(myList);

JAVA - can't call my array method

Eclipse says: 'chiffres cannot be resolved to a variable', how to fix the call method ?
public class Table {
public static void main(String[] args) {
Tableau1 table = new Tableau1();
table.CreerTable();
table.AfficherTable(chiffres);
}}
part:
and class Tableau1 with array: to declare it
public class Tableau1 {
int [][] chiffres;
int nombre;
public void CreerTable(){
int[][] chiffres= {{11,01,3},
{12,02,4},
{12,03,5}};
//edited
this.chiffres=chiffres;
}
public int[][] AfficherTable(int[][] chiffres){
this.nombre=12;
for(int i=0;i<2;i++){
System.out.println("essai"+chiffres[i][1]);
if(chiffres[i][0]==nombre){System.out.println("ma ligne ="+chiffres[i][0]+","+chiffres[i][1]+","+chiffres[i][2]);
};
}
return chiffres;
}
}
Thanks a lot
You have 3 problems here.
Problem 1 :
1) You method AfficherTable(chiffres) need not to pass an argument, since it is an instance member.
You can simply call
table.AfficherTable();
That solves your problem.
Before doing that problem no 2
Problem 2:
2) You delcared chifferes as a instance member int [][] chiffres;
and you are initializing it's in constructor
public void CreerTable(){
int[][] chiffres= {{11,01,3},
{12,02,4},
{12,03,5}};
}
But if you closely look, you are creating new array again. That won't work, since you are creating new array and forgot your instance member.
Change your constructor to
public void CreerTable(){
chiffres= new int[3][3] {{11,01,3},
{12,02,4},
{12,03,5}};
}
Problem 3 :
After changing that constructor, since you are using it in the same class member, you need not to receive it. Hence you change your method declaration as
public int[][] AfficherTable(){
You'll be fine I guess now.
table.CreerTable();
table.AfficherTable(chiffres);
By resolving chiffres it searches in the Class Table as you don't specify that chiffres comes from Tableau1.
Therefore the solution is:
table.CreerTable();
table.AfficherTable(table.chiffres);
chiffers is neither a local variable of the main method, nor a field of the class Table, that's why the error.

Categories