Following a UML class diagram. Stuck and need help!

So, I have to create this small program in Java right? I finished pretty much all of it but there's this problem with the Main class that I'm pretty much stumped with. You can see my code below. Basically as far as I understand it, this is technically the way to do it? But it doesn't work that way since it's asking the variables to be static or something. But I can't make them static because the UML doesn't say to make them static. I asked ChatGPT to what to do with this and she told me something like creating a new Main object or something but I don't think I can do that with this UML diagram? (since it doesn't say to make one). Wondering what exactly to do here... Or even whether its doable with this diagram. Or maybe I'm missing something completely obvious. I'm really just a few weeks into this class! This is like, a question from last year's one. The code pretty much works other than that. Thanks in advance!<3 import java.util.LinkedList; public class Main { public LinkedList<Account> accounts; public LinkedList<String> firstNames; public LinkedList<String> lastNames; public LinkedList<Integer> accountList; public LinkedList<Integer> balanceList; public String file; public ReadAccounts readAccounts = new ReadAccounts(file); public Transaction transfer = new Transaction(); public static void main(String[] args) { file = "C:/Users/KatsPC/Desktop/readthis.csv"; } } Oh, the error is at file = "C:/Users/KatsPC/Desktop/readthis.csv";
No description
29 Replies
JavaBot
JavaBot4mo ago
This post has been reserved for your question.
Hey @Run! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Голди
Голди4mo ago
Hi. You've accessed a field that is only available after class initialization. If you want to work with it, you need to mark the "file" field as public static String file;. I don't know what you're doing, but you'd better declare this variable in the "main" method.
ayylmao123xdd
ayylmao123xdd4mo ago
if you want to access these fields in the main method then you need them to be static you could make another class thats not named main and have these fields without them being static though
Run
RunOP4mo ago
@Голди @ayylmao123xdd hii thanks for responding! Wouldn't the UML diagram need to have, like underlines underneath the variables and stuff for me to make them static? Not me pinging the wrong person. Sorry 😭
JavaBot
JavaBot4mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Голди
Голди4mo ago
What do you mean by underlining the variables?
Run
RunOP4mo ago
In the UML diagram
ayylmao123xdd
ayylmao123xdd4mo ago
ok so to run the app you need this code in some class
public static void main(String[] args) {
}
public static void main(String[] args) {
}
Run
RunOP4mo ago
Isn't there supposed to be a line there underneath to say they are static
ayylmao123xdd
ayylmao123xdd4mo ago
so you could make a class lets say AppRunner and put the main method there and then make the main class with those fields let me test that
Run
RunOP4mo ago
I mean, I still can't do that because it goes outside of the UML diagram (I think)
ayylmao123xdd
ayylmao123xdd4mo ago
can you show how you run the application btw do you use the main class for anything i think the problem is with the naming of the uml because most of the time the main class is used to run the application
public class AppRunner {
public static void main(String[] args) {
Main main = new Main();
main.setFile("path");
}
}


public class Main {
public LinkedList<Account> accounts;
public LinkedList<String> firstNames;
public LinkedList<String> lastNames;
public LinkedList<Integer> accountList;
public LinkedList<Integer> balanceList;
public String file;
public ReadAccounts readAccounts = new ReadAccounts();
public Transaction transfer = new Transaction();

public void setFile(String file) {
this.file = file;
}
}
public class AppRunner {
public static void main(String[] args) {
Main main = new Main();
main.setFile("path");
}
}


public class Main {
public LinkedList<Account> accounts;
public LinkedList<String> firstNames;
public LinkedList<String> lastNames;
public LinkedList<Integer> accountList;
public LinkedList<Integer> balanceList;
public String file;
public ReadAccounts readAccounts = new ReadAccounts();
public Transaction transfer = new Transaction();

public void setFile(String file) {
this.file = file;
}
}
you can have something like this
Run
RunOP4mo ago
I think the naming is fine because the main class is the place where the guideline says to run/test the program from
ayylmao123xdd
ayylmao123xdd4mo ago
is it supposed to be 1 file per program because if you do a static field for file for example
Run
RunOP4mo ago
Like .java files?
ayylmao123xdd
ayylmao123xdd4mo ago
then you can have only one file no the variable string file
Run
RunOP4mo ago
Yeah only 1 file It's like made to just read 1 file
ayylmao123xdd
ayylmao123xdd4mo ago
then the fields should be static i guess either you make the fields static or you have to do the example i showed maybe the person that made the uml forgot to say that theyre supposed to be static fields
Run
RunOP4mo ago
Chatgpt said to do something similar to what you said but the problem is that the UML doesn't say anything about making another class 😭 I'm just here wondering what's less "wrong", making another class or making the variables and stuff static Maybe, yeah lol
ayylmao123xdd
ayylmao123xdd4mo ago
i think static is the better option if you run the program from the main class that means you cant do the example like i showed
Run
RunOP4mo ago
I think so too. Thanks for your time!
JavaBot
JavaBot4mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Run
RunOP4mo ago
I'll just keep this thread open just in case someone has a magical answer or something
ayylmao123xdd
ayylmao123xdd4mo ago
ok heres a hilarious gif then
Run
RunOP4mo ago
Literally me
JavaBot
JavaBot4mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Marko
Marko4mo ago
The UML diagram doesn't say that Main should have the main static method Are there are other requirements?
JavaBot
JavaBot4mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?