Im attempting to remove an object form an array list but if fails everytime.
Im using a function to add and a seperate one to remove. The add function is working but the remove is not. I cant see where it is going wrong and failing.
public void unregister( Basics classToRemove){
if(checkIsRegistered(classToRemove)){
getClassList().remove(classToRemove);
System.out.println(getClassList().remove(classToRemove));
System.out.println("You have unregistered from " + classToRemove.className + " class.");
setTotalTuition();
} else{
System.out.println("\nYou are not currently registered for " + classToRemove.className + " class.\n");
}
}
public void register(Basics classToAdd){
if(!checkIsRegistered(classToAdd)){
getClassList().add(classToAdd);
System.out.println("You have registered for " + classToAdd.className + " class.");
setTotalTuition();
}else{
System.out.println("\nYou are already registered for " + classToAdd.className + " class.\n");
}
}
I have attempted using this.arrayList to remove the object and using getters to get the object and remove it.
You can not just print a class-object with System.out.printline. I assume you want to print the attributes of the class, however you are printing the memory address of the object.
Additionally, this doesn't make sense to me:
getClassList().remove(classToRemove);
System.out.println(getClassList().remove(classToRemove));
You can't access and print an Object you just removed.
If this is not helpful, consider providing a minimal example and explaining what exactly is not working.
Related
I've decided to programm a search system for finding students and teachers in a school via GUI. It is an OOP and need some tweaking here and there, but there is one issue which doesn't seem logical to me. When I'm searching for a teacher, I have to type there name or surname into a JTextField and press the Search button which runs a method that loops through an ArrayList of teacher-objects and checks if their names match with the one in the Textfield. Then it checks if these teachers have multiple subjects and grades and it goes through nested if-statements. After the teacher is found, their information is displayed on a GUI with several Texfields. Theoretically if the name I typed into the TextField doesn't match one from the teacher objects, a Error Message should pop-up saying the teacher I'm looking for isn't in the system. But even though I type in the correct name and get all the information displayed, it sends me to the Error Message everytime. I tried to fix it with a break statement but that didn't work either. Can someone please help me with this.
Here is the code I'm talking about:
public void lehrerSuche()
{
String lehrername = tfSuchfeldLehrer.getText();
for(int i = 0; i < td.getFachliste(0).getListenLaengeLehrerListe();i++)
{
if(td.getFachliste(0).getLehrerliste(i).getName().equals(lehrername) || td.getFachliste(0).getLehrerliste(i).getNachname().equals(lehrername))
{
if(td.getFachliste(0).getLehrerliste(i).isMehrerefaecher() && td.getFachliste(0).getLehrerliste(i).isMehrereklassen())
{
tfNameLehrer.setText(td.getFachliste(0).getLehrerliste(i).getName() + " " + td.getFachliste(0).getLehrerliste(i).getNachname());
tfKürzelLehrer.setText(td.getFachliste(0).getLehrerliste(i).getKuerzel() + ".");
tfKlasse_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlasse().getBezeichnung());
tfKlasse_2Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlass2().getBezeichnung());
tfFach_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach().getFachbezeichnung());
tfFach_2Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach2().getFachbezeichnung());
}
if(td.getFachliste(0).getLehrerliste(i).isMehrerefaecher() == false && td.getFachliste(0).getLehrerliste(i).isMehrereklassen())
{
tfNameLehrer.setText(td.getFachliste(0).getLehrerliste(i).getName() + " " + td.getFachliste(0).getLehrerliste(i).getNachname());
tfKürzelLehrer.setText(td.getFachliste(0).getLehrerliste(i).getKuerzel() + ".");
tfKlasse_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlasse().getBezeichnung());
tfKlasse_2Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlass2().getBezeichnung());
tfFach_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach().getFachbezeichnung());
}
if(td.getFachliste(0).getLehrerliste(i).isMehrerefaecher() && td.getFachliste(0).getLehrerliste(i).isMehrereklassen()==false)
{
tfNameLehrer.setText(td.getFachliste(0).getLehrerliste(i).getName() + " " + td.getFachliste(0).getLehrerliste(i).getNachname());
tfKürzelLehrer.setText(td.getFachliste(0).getLehrerliste(i).getKuerzel() + ".");
tfKlasse_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlasse().getBezeichnung());
tfFach_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach().getFachbezeichnung());
tfFach_2Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach2().getFachbezeichnung());
}
if(td.getFachliste(0).getLehrerliste(i).isMehrerefaecher() == false && td.getFachliste(0).getLehrerliste(i).isMehrereklassen()==false)
{
tfNameLehrer.setText(td.getFachliste(0).getLehrerliste(i).getName() + " " + td.getFachliste(0).getLehrerliste(i).getNachname());
tfKürzelLehrer.setText(td.getFachliste(0).getLehrerliste(i).getKuerzel() + ".");
tfKlasse_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlasse().getBezeichnung());
tfFach_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach().getFachbezeichnung());
}
break;
}
else
{
switchPanels_3(panelErrorLehrer);
}
}
}
I've uploaded my project to GitHub. Methods and variables are written in German, so I'm really sorry if you can't understand what I have written. If u have questions please hit me up. I use Eclipse to code.
This link should direct you to my GitHub:
https://github.com/Gonzo-CR/Home-Projects.git
If the link doesn't work, look for Gonzo-CR on GitHub and check out my Home-projects repository where I uploaded all the files.
For better undestanding these are the Object oriented classes:
Person(Abstract)
Schueler
Lehrer
Fach
Schulklasse
Spezial
Sprecher
GUI classes:
Suchsystem
Testdaten(A class which generates all my objects)
The problem is likely that if td.getFachliste(0).getLehrerliste(i).getName().equals(lehrername) is not true the very first time the loop runs, switchPanels_3(panelErrorLehrer); will be triggered - regardless of whether the condition is met on a later iteration of the loop.
What you need is to check a sentinel value after the loop finishes - e.g.:
bool lehrerGefunden = false;
for(int i = 0; i < td.getFachliste(0).getListenLaengeLehrerListe();i++){
if(td.getFachliste(0).getLehrerliste(i).getName().equals(lehrername) || td.getFachliste(0).getLehrerliste(i).getNachname().equals(lehrername)){
//etc.
lehrerGefunden = true;
break;
}
}
if(!lehrerGefunden){
switchPanels_3(panelErrorLehrer);
}
That way, you check every entry in the list before deciding whether to show the error.
EDIT: Full Code for people who need it: https://hastebin.com/tovazutuni.cs
ok so im working on a big burger shop challenge, but to keep it simple, here is a similair problem im facing:
public class Burger {
private double priceWithAdditions = 5.00;
private double price = 5.00;
private String addition1;
private double addition1Price;
switch(burgersAdditionScanner()){
case "mayo":
this.addition1 = "Mayo";
this.addition1Price = 0.40;
this.priceWithAdditions += this.addition1Price;
System.out.println("Added Additions " + this.addition1 + " for the price of $" +this.addition1Price);
break;
default: System.out.println("Get Out");
Later down in the code, I have a method to print the receipt, but the values of the variables changed inside the switch do not update outside of it:
public void Receipt() {
System.out.println("You Bought " + this.name + " burger for the price of $" + this.price + " with the following \nAdditions:");
if(this.addition1 != null ) {
System.out.println("Addition " + this.addition1 + " for $" + this.addition1Price);
}
System.out.println("The final price is " + this.priceWithAdditions); }
Considering the values of addition1 and priceWithAdditions didnt change in the case, addition 1 is null and priceWithAdditions is still 5.00 instead of 5.40. Does anyone know how to make the values of these variables update ourside of the switch. If you can help me Id really appreciate it, please and thank you
Console Output: (There's a scanner so I can type my choices)
add addition 1
Choose Addition 1
Lettuce - $0.50
Cheese - $1.00
Ketchup - $0.20
Mayo - $0.40
mayo
Added Addition Mayonaise for the price of $0.4
Add Additions or none
Add Addition 1
Add Addition 2
Add Adition 3
Add Adition 4
Receipt
receipt
You Bought Amer's Booty burger for the price of $5.0 with the following
Additions:
The final price is 5.0
Get Out
The problem is you are calling Receipt() on the wrong instance of Burger. For all calls to Receipt() (that I'm seeing), you are calling it on a new instance of a Burger, like so:
case "receipt":
HealthyBurger burgerReceipt = new HealthyBurger();
burgerReceipt.Receipt();
burgerReceipt is a new instance of a HealthyBurger, and not the one that you called any of your addition operators on. As such, it will contain the default values of your HealthyBurger. You can fix this, by calling Receipt on the same instance of HealthyBurger that was mutated. For example:
HealthyBurger burger = new HealthyBurger()
burger.addAddition6()
burger.Receipt()
Your code is a bit too messy for me to give you an exact solution to your problem, but you are creating new instances of Burger all over the place, in a Restaurant class which also happens to be a Burger itself. I think if you take the time to read up on basic inheritance/object oriented principles, you should be able to simplify your code significantly.
I have this little bit of code
_grid[4][4].setText(_square[4][4].getTopColor() + ": "
+ _square[4][4].getHeight());
Eventually in my program, the text will change because the value of get.Height will change. Is there a way to write a simple program that sets text based on the coordinates of the multidimensional array?
So if the method was called, updateText, I could just do _grid[4][4].updateText(); and it would be the same as the code above. Or if I did _grid[0][12].updateText(), it would do the same as this:
_grid[0][12].setText(_square[0][12].getTopColor() + ": "
+ _square[0][12].getHeight());
It's easy enough to refactor that line into a method in the same class.
private void updateText(int row, int col) {
_grid[row][col].setText(_square[row][col].getTopColor() + ": "
+ _square[row][col].getHeight());
}
If you want to make it a method of the grid items, you'll have to give more details. Do the items in the grid know about their corresponding _squares?
I have an ArrayList called participatingUsers. Person has Person.money and Person.name that are interesting.
What I want to do is check the ArrayList against itself...
So I have this code
for (Person debtHaver : this.participatingUsers) {
// If they're in debt...
if (debtHaver.getMoney() < 0) {
// With someone...
for (Person personToPay : this.participatingUsers) {
// That's not themselves...
if (!debtHaver.getName().equals(personToPay.getName())) {
// See if the personToPay is ranked higher than the
// debtHaver...
if (personToPay.getMoney() > 0) {
// If the debtee can pay the debter in full
if (-debtHaver.getMoney() <= personToPay.getMoney()) {
payment += debtHaver.getName() + " has to pay " + personToPay.getName() + " " + -debtHaver.getMoney() + "\n";
debtHaver.increaseMoney(-debtHaver.getMoney());
personToPay.decreaseMoney(-debtHaver.getMoney());
}
if (-debtHaver.getMoney() > personToPay.getMoney())
{
//But if he can't pay in full... Just pay the small bit you can pay.
payment += debtHaver.getName() + " has to pay " + personToPay.getName() + " " + personToPay.getMoney() + "\n";
debtHaver.increaseMoney(personToPay.getMoney());
personToPay.decreaseMoney(personToPay.getMoney());
}
}
}
}
}
}
return payment;
Basically, I have a double for loop where I check each person against itself. If someone is in debt and has a negative amount of money, seek if there is someone who they can pay, then pay that person. The thing is, personToPay is not being updated in the arrayList debtHaver is in. I'm basically editing two different ArrayLists instead of the same one. What's the best way to deal with this issue?
You are editing the same list. The problem is probably in this code:
debtHaver.increaseMoney(-debtHaver.getMoney());
personToPay.decreaseMoney(-debtHaver.getMoney());
You are putting debtHaver's amount to zero by the first line. And then you try to modify personToPay with zero amount. Just swap two lines of code and it should work:
personToPay.decreaseMoney(-debtHaver.getMoney());
debtHaver.increaseMoney(-debtHaver.getMoney());
I am doing some practice work by expanding on a homework project I recently wrote. This is not for a grade, but I want to add some error checking to my code.
The program requires you to enter a name, select from a dropdown, select from a listbox and to select a radio button. My goal is to populate an error messagebox if any of the required items is blank.
The code I have so far for the error checking is below, but Im not sure how to take the individual lacking item and populate that to the message box since all of the error checking is in a single "if" statement.
Error checking code:
// Listener to handle the print button.
class ButtonListener implements ActionListener
{
ButtonListener() {}
public void actionPerformed(ActionEvent e)
{
JFrame frame1 = new JFrame("Show Message Dialog");
// Checks for required entries
if (error == 0 || name == "" || flag == 0)
{
JOptionPane.showMessageDialog(frame1,
"You must complete the form: " + missing, "ERROR",
JOptionPane.ERROR_MESSAGE);
}
else
{
// Get values from fields
setText();
System.out.println("Passenger's Name: " + name + "\n");
System.out.println("Age Group: " + ageValue + "\n");
for (int i = 0; i < value.length; i++)
{
System.out.println("Destination: " + value[i] + "\n");
}
System.out.println("Departure Day: " + day + "\n");
}
}
}
Thanks!
It looks like they can actually have multiple things wrong, so why only show one? I'm normally a C# dev so I'm not going to try to get the syntax right on typing this myself, but here's the concept:
Create a collection of some sort, like a list of strings.
Make 3 if statements, one for each of those errors, and put the field
names in for each one that fails. if (name.isEmpty()) {
errorList.Add("name"); }
Check to see if the count of items in the list is greater than 0. If
it is throw your error and put the name of the bad fields from the
collection into the string that you generate.