Is something wrong with the method? [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
Improve this question
So that's my method and i got Main class from my teacher that uses this method and it should return only 1 Suspect but it returns the same one for 8 times or something i am really close on my deadline please help
public ArrayList<Suspect> getCommonPartners(Suspect aSuspect) {
ArrayList<Suspect> commonpartners = new ArrayList<>() ;
for(Suspect s: partners) {
for(Suspect sus: aSuspect.getPartners()) {
if(s.getCodename().equals(sus.getCodename())) {
commonpartners.add(s);
}
}
}
return (commonpartners);
}

Where is 'partners' coming from? You're running a for loop on it. Did you mean aSuspect?
If you are getting multiple of the same value. Do a check on the arraylist before adding it.
public ArrayList<Suspect> getCommonPartners(Suspect aSuspect) {
ArrayList<Suspect> commonpartners = new ArrayList<>() ;
for(Suspect s: partners) {
for(Suspect sus: aSuspect.getPartners()) {
if(s.getCodename().equals(sus.getCodename())) {
if(!commonpartners.contains(s)) {
commonpartners.add(s);
}
}
}
}
return (commonpartners);
}

Related

I need help, printing to the console these methods(class functions ) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 hours ago.
Improve this question
this is my second question, thankfully I resolved the first question I made. OK so this time I am having difficulty printing to the console this Main method and Duck class, I am supposed to print out "waddle waddle" and Quack into the console, I think its a problem with the refrence varible: any help will be greatly appreciated I alredy tried everything to get the code to run but the Java error said
"exit status 1;"cannot find symbol
String waddling = donald.waddle();
symbol: variable donald.
location: class Duck."
class Duck {
public String quack() {
return "*waddle waddle*";
}
#Override
public String toString() {
return "quack";
}
class Main {
public static void main(String[] args) {
class Duck {
public String waddle() {
return "*waddle waddle*";
String waddling = donald.waddle();
System.out.println(waddling);
}
}
}
}

Java list of string don't display all elements [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 days ago.
Improve this question
i'm trying to add elements in a list inside a method and after get it from another method.
Here is the code :
public class ChatMessageRepository {
List<String> msgs = new ArrayList<String>();
ChatMessageRepository addChatMessage(String message) {
msgs.add(message);
System.out.println(msgs);
return null;
}
List<String> getLastTenMessages() {
List<String> firstNElementsList = msgs.stream()
.limit(10)
.collect(Collectors.toList());
return firstNElementsList;
}
}
The problem is when I try to display the firstNElementsList it's alaway empty and also when i print the list from ChatMessageRepository Method it's display me only the last element
Any one have an idea guys ?

Should I avoid returning null in my java function? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I don't find the appropriate solution for my question. As far as I know returning null is a not a good way to write clean code, as the book Clean Code says. However there are many different opinions about this practice, and I am not sure about which one fits on my function.
private EArea getSimilarExistingArea(EImportArea importedArea) {
for (EArea existingArea : exsitingAreas) {
EList<EPoint> importedAreaPoints = importedArea.getPoly().getPoints();
EList<EPoint> existingAreaPoints = existingArea.getPoly().getPoints();
for (EPoint importedAreaPoint : importedAreaPoints) {
for (EPoint existingAreaPoint : existingAreaPoints) {
if (importedAreaPoint.equals(existingAreaPoint))
return existingArea;
}
}
}
return null;
}
What should I return if there is not an existing similar area?
PD: For optimize my code I am breaking the loops with the return if an existing area is founded.
You should take a look at the Optional class!
Make your method return type Optional<EArea> and simply return Optional.ofNullable(existingArea) you will have to slightly modify your Code but the benefits of Optional are really worth it!
Finally I used Optional Class to solve my problem.
Here is the code:
private Optional<EArea> getSimilarExistingArea(EImportArea importedArea) {
for (EArea existingArea : baseLineService.getBaseLine().getAreas()) {
EList<EPoint> importedAreaPoints = importedArea.getPoly().getPoints();
EList<EPoint> existingAreaPoints = existingArea.getPoly().getPoints();
for (EPoint importedAreaPoint : importedAreaPoints) {
for (EPoint existingAreaPoint : existingAreaPoints) {
if (importedAreaPoint.equals(existingAreaPoint))
return Optional.of(existingArea);
}
}
}
return Optional.empty();
}
And here is how I check the returned value:
if (getSimilarExistingArea(importedArea).isPresent())

How to debug an if statement that is not working? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
import java.util.*;
public class ass {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[]c={"sid","is","cool"};
List<String>list1=new ArrayList<String>();
for(String w:c){
list1.add(w);
}
String[]q={"is"};
List<String>list2=new ArrayList<String>();
for(String t:q){
list2.add(t);
}
EditList(list1,list2);
for(int i=0;i<list1.size();i++){
System.out.printf("%s ", list1.get(i));
}
}
public static void EditList(Collection<String>l1, Collection<String>l2){
Iterator<String>it=l1.iterator();
while(it.hasNext()){
if(l2.contains(it.next()));
it.remove();
}
}
}
In this programme I have two lists. I wanted to remove the items that are common in the first and second list from the first list and print it. I don't want a workaround or any other code suggestions. Can someone please explain why my code is not working?
I am following New Boston's tutorials.
Here:
if(l2.contains(it.next()));
it.remove();
That semicolon after if is a real statement.
Thus it.remove() happens always; like if ... that if not there!
Thus the real answer: always always always use
if (){
stuff
}
... even for single statements! Same for loops!
try
public static void EditList(Collection<String>l1, Collection<String>l2){
Iterator<String>it=l1.iterator();
while(it.hasNext()){
String current=it.next();
if(l2.contains(current)){
i1.remove(current); // assuming u wish to remove from l1
}
}
}

Pass boolean value to another class [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
In my main class I want to check if there was a change by using a boolean variable:
public class Main {
private boolean change = false;
public boolean getChange() {
return change;
}
public void setChange(boolean change) {
this.change = change;
}
private void method1() {
// some command
setChange(true);
method1();
}
If I want to get this boolean value in my second class, I always get returned "false", no matter if my method1 ran or not.
public class BoolTest {
Main m = new Main();
System.out.println(m.getChange()); // returns "false"
}
You must have two instances of Main. Use the same one. Example:
Main m = new Main();
System.out.println(m.getChange());
m.setChange(true);
System.out.println(m.getChange());
You probably want to share the same instance over multiple classes. Pass the instance to the other classes and use them as expected.

Categories

Resources