#ArrayLists - This may be a stupid question

1 messages ยท Page 1 of 1 (latest)

inner hamlet
#

This may seem like a stupid questing but I've been looking at this issue for ages and cant seem to figure out how to fix it. I have an arrayList set out like it can be seen in the the screenshot, and I'm trying to change the second value (11111 and 22222) with an input I'm getting. I have tried .set but to no avail. Any advice?

gaunt ravineBOT
#

<@&987246399047479336> please have a look, thanks.

latent wagon
#

ull have to share ur code

#

๐Ÿ™‚

inner hamlet
#

all of it?

latent wagon
#

well, all relevant bits

#

id start with the class that has this addBankAccount method and the code where u attempt to change it with .set

inner hamlet
latent wagon
#

zip isnt really accessible to helpers

#

especially people on mobile

#

please just drag and drop the relevant files

#

it should be like 2 or 3 files

#

but yeah, good idea in general ๐Ÿ™‚

inner hamlet
#

ok let me fine the relevent ones as the project has around 25 files

gaunt ravineBOT
#
Spectre's result

Snippets

Snippet 23, VALID

import java.util.ArrayList;```
### Snippet 24, VALID
```java
 import java.util.List;```
### Snippet 25, VALID
```java
  class BankAccount {     private int a;     private int b;     private int c;     private int d;      public BankAccount(int a, int b, int c, int d) {         this.a = a;         this.b = b;         this.c = c;         this.d = d;     }      public int getA() {         return a;     }      public void setA(int a) {         this.a = a;     }      public int getB() {         return b;     }      public void setB(int b) {         this.b = b;     }      public int getC() {         return c;     }      public void setC(int c) {         this.c = c;     }      public int getD() {         return d;     }      public void setD(int d) {         this.d = d;     }      @Override     public String toString() {         return "BankAccount {a=" + a + ", b=" + b + ", c=" + c + ", d=" + d + "}";     } }```
### Snippet 26, VALID
```java
  List<BankAccount> bankAccounts = new ArrayList<>();```
jshell> `[]`
### Snippet 27, VALID
```java
 bankAccounts.add(new BankAccount(1, 2, 3, 4));```
jshell> `true`
### Snippet 28, VALID
```java
 bankAccounts.add(new BankAccount(2, 3, 4, 5));```
jshell> `true`
### Snippet 29, VALID
```java
 bankAccounts.add(new BankAccount(3, 4, 5, 6));```
jshell> `true`
### Snippet 30, VALID
```java
 bankAccounts.add(new BankAccount(4, 5, 6, 7));```
jshell> `true`
### Snippet 31, VALID
```java
 for (BankAccount bankAccount : bankAccounts) {     bankAccount.setB(10000); }```
### Snippet 32, VALID
```java
 bankAccounts.forEach(System.out::println);```
## System out

BankAccount {a=1, b=10000, c=3, d=4}
BankAccount {a=2, b=10000, c=4, d=5}
BankAccount {a=3, b=10000, c=5, d=6}
BankAccount {a=4, b=10000, c=6, d=7}

clear flame
# gaunt ravine

do you mean something like that? i am not sure that i understand what you are tying to achive

#
import java.util.ArrayList;
import java.util.List;

class BankAccount {
    private int a;
    private int b;
    private int c;
    private int d;

    public BankAccount(int a, int b, int c, int d) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
    }

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public int getB() {
        return b;
    }

    public void setB(int b) {
        this.b = b;
    }

    public int getC() {
        return c;
    }

    public void setC(int c) {
        this.c = c;
    }

    public int getD() {
        return d;
    }

    public void setD(int d) {
        this.d = d;
    }

    @Override
    public String toString() {
        return "BankAccount {a=" + a + ", b=" + b + ", c=" + c + ", d=" + d + "}";
    }
}

List<BankAccount> bankAccounts = new ArrayList<>();
bankAccounts.add(new BankAccount(1, 2, 3, 4));
bankAccounts.add(new BankAccount(2, 3, 4, 5));
bankAccounts.add(new BankAccount(3, 4, 5, 6));
bankAccounts.add(new BankAccount(4, 5, 6, 7));
for (BankAccount bankAccount : bankAccounts) {
    bankAccount.setB(10000);
}
bankAccounts.forEach(System.out::println);
inner hamlet
#

im now getting an error that non static method cannot be referenced from a static contect. how would i go about fixing this?

latent wagon
#

a static setter makes little sense

#

Bank.setPasswd is wrong

#

there could be a million banks. u have to call it on the bank instance in question

#

someBank.setPasswd(...)

inner hamlet
#

i have a class called Bank

#

how would i reference that better

latent wagon
#

u have to understand OOP better

#

ur creating instances of classes

#

Bank someBank = new Bank(...);

#

by calling the constructor

#

u could create 500 bank instances

#

so. which bank do u want to set the password?

#

which of the possibly 500?

#

u need to have hands on the instance

#

on the variable holding that bank

coral nexus
# inner hamlet i have a class called Bank

Yeah, but think about it. If your Bank class has a method setName.
If setName is a normal, i.e. non-static, method, then the setName would affect a single, specific instance of a Bank. This makes sense, for example we can have Consorsbank, Sparkasse, etc.

If setName were a static method of Bank, then calling that would change the names of all the banks there are.

inner hamlet
#

ok so ive looked throught the other code and referenced it the same as ive refered the withdraw part of the code, but it is now saying that the code within the Bank class in unreachable. which is confusing me evenmore

coral nexus
#

(preferably as text)

#

See the bot embed if you don't know how to format code on Discord (Discord uses a variant of Markdown):

gaunt ravineBOT
#

Please use this format for posting code:

```java
// Example java program
int value = 5;
System.out.println(value);
```

Which results in:

// Example java program
int value = 5;
System.out.println(value);

For syntax highlighting, you have to add the name of the language after the three backticks, like ```java. Please make sure to use exactly this format, so no space between the backticks and the language name, and a newline before the code starts. If done right, the syntax highlighting will even be applied to your text as you type, before sending.

inner hamlet
#

sorry only just seen this, thought i got somewhere, all parts are reachable now but it still aint working, going to have a break, new error so going to look at it myself first but thanks anyways

inner hamlet
#

ive been working on it and there is no errors but its not working lmao