Kirdid
Kirdid

Reputation: 11

Printing objects with bool true from an arraylist JAVA

The getArrayStringListFalse method should be printing out the objects with the bool false

And the getArrayStringListTrue method should do the same with the objects with bool true

import java.util.*;


public class Issue {

private static ArrayList<newIssue> list = new ArrayList<>();


public static ArrayList<newIssue> getArrayStringListFalse(){

    for(int i = 0; i < list.size();i++){
        if (!list.get(i).returned){
            System.out.println("["+"["+i+"] "+list.get(i)+"]");
        }else {
            System.out.println("You have no unsolved issues!");
        };
    };
    return null;
}

public static ArrayList<newIssue> getArrayStringListTrue(){

    for(int i = 0; i < list.size();i++){
        if (list.get(i).returned){
            System.out.println("["+"["+i+"] "+list.get(i)+"]");
        }else {
            System.out.println("You have no solved issues!");
        };
    };
    return null;
}



public static void removeIssue(){
    for (int i = 0; i<list.size(); i++){;
        System.out.println("["+"["+i+"] "+list.get(i)+"]");
    }
    Scanner scan = new Scanner(System.in);
    System.out.println("Which one would you like to mark as solved?");
    int choice = scan.nextInt();
    newIssue issue = list.get(choice);
    issue.returned = true;
}

public static void addIssue(){
    System.out.println("---Create a new issue---");
    System.out.println("Describe the issue: ");
    Scanner scan = new Scanner(System.in);
    String text= scan.nextLine();
    newIssue issue = new newIssue(text);
    list.add(issue);
}
}

It is working sort of except im printing out the "issue + the bool value" so it looks something like this. [I have no catsfalse].

Also the for loop should only print out the true/false depending on the method the amount of existing true/false objects now if I have 2 true objects and 1 false and use the false method it prints out one false then the else statement.

Hope you understand what Im trying to do

public class newIssue {

public String issueText;
public boolean returned = false;

public newIssue(String issueText){
    this.issueText = issueText;
}

public String toString(){
    return issueText + returned;
}



}

Upvotes: 0

Views: 1903

Answers (2)

Anton Balaniuc
Anton Balaniuc

Reputation: 11739

You can simplify it a lot, if you can use java-8 and steams:

// print all elements with returned == true    
list.stream().filter(i -> i.returned).forEach(System.out::println);

and

// print all elements with returned == false    
list.stream().filter(i -> !i.returned).forEach(System.out::println);

Upvotes: 1

Tharun
Tharun

Reputation: 311

You can modify like this:

public static ArrayList<newIssue> getArrayStringListFalse(){
boolean flag=false;
for(int i = 0; i < list.size();i++){
    if (!list.get(i).returned){
        System.out.println("["+"["+i+"] "+list.get(i)+"]");
        flag = true;
    }
}
if(!flag){
    System.out.println("You have no unsolved issues!");
}
return null;
}

public static ArrayList<newIssue> getArrayStringListTrue(){
boolean flag=false;
for(int i = 0; i < list.size();i++){
    if (list.get(i).returned){
        System.out.println("["+"["+i+"] "+list.get(i)+"]");
        flag = true;
    }
}
if(!flag){
        System.out.println("You have no solved issues!");
};
return null;
}

Upvotes: 0

Related Questions