ItsAmi YouNeedAMedic ItsAmi YouNeedAMedic - 1 month ago 8
Java Question

remove text object from an Arraylist based on userinput

I am creating a program that allows the user to add issues and remove them (based on a menu).

My first question:
How can I display my "issue" arraylist in another method? (In this case, "your issues are: +issue array)

My second question:
How can I remove an issue from the issue array list WITH user input and store it into a "solved" array list in my second method?

Code snippet:

public class IssueTrackingObject {

int length;
int solved;
Scanner input = new Scanner(System.in);

public void createIssue() {
System.out.println("Enter the number of issues you will enter: ");
length = input.nextInt();
String[] issues = new String[length]; //issues stored in this array
for(int counter = 0; counter < length; counter++){
System.out.println(counter+1 + (") Enter the first issue: "));
issues[counter] = input.next();
}
System.out.println("Your issues are: ");
for(int counter = 0; counter < length; counter++){
System.out.println(issues[counter]);
}
}

void solvedIssue() {
System.out.println("To MARK an issue as solved, please enter the number of the issue: ");
solved = input.nextInt();
String [] issues = new String [solved];
for(int s = 0; s < length; s++){
System.out.println(issues[s]);

}
}
void printSolvedIssue() {
System.out.println("You have SOLVED the following issues: ");
}

void printUnsolvedIssue() {
System.out.println("Your unsolved Issues are: ");
}
}


I have read everything I can find online and in my book but I cannot find a solution that fits what I want my code to do.

Answer

Issue #1
You have to make your issues lists members of the class so that they can be accessible from any method inside the IssueTrackingObject class.

Issue #2
You can add a SolvedIssues list to store those issues removed from the total issues list.

You can try implementing you IssueTrackingObject class as follows:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


    public class IssueTrackingObject {
        Scanner input = new Scanner(System.in);
        // Make your issues lists members of the class so that they
        // can be accessed from all the methods inside the class
        private List<String> _allIssues = new ArrayList<String>();
        // Added a Solved issues list to store the solved issues
        private List<String> _solvedIssues = new ArrayList<String>();

        public void createIssue() {
            System.out.println("Enter the number of issues you will  enter: ");
            int length = input.nextInt();
            for (int counter = 0; counter < length; counter++) {
                System.out.printf("Enter issue number %d\n", counter + 1);
                _allIssues.add(input.next());
            }
            System.out.println("Your issues are: ");
            for (int counter = 0; counter < _allIssues.size(); counter++) {
                System.out.printf("%d. %s\n", counter + 1, _allIssues.get(counter));
            }
        }

        void solvedIssue() {
            if(_allIssues.size() == 0) {
                System.out.println("No issues entered yet. Please enter some issues");
                return;
            }
            System.out.println("To MARK an issue as solved, please enter the number of the issue: ");
            int index = input.nextInt();
            --index; // List starts at zero
            _solvedIssues.add(_allIssues.get(index));
            _allIssues.remove(index);
        }

        void printSolvedIssue() {
            System.out.println("You have SOLVED the following issues: ");
            for(int i = 0; i < _solvedIssues.size(); ++i) {
                System.out.printf("%d. %s\n", i + 1, _solvedIssues.get(i));
            }
        }

        void unsolvedIssue() {
            if(_allIssues.size() == 0) {
                System.out.println("No issues entered yet. Please enter some issues");
                return;
            }
            System.out.println("To MARK an issue as solved, please enter the number of the issue: ");
            int index = input.nextInt();
            _allIssues.remove(index);
        }

        void printUnsolvedIssue() {
            System.out.println("Your unsolved Issues are: ");
            for(int i = 0; i < _solvedIssues.size(); ++i) {
                System.out.printf("%d. %s\n", i + 1, _allIssues.get(i));
            }
        }

        public static void main(String[] args) {
            int exit = 1;
            Scanner input = new Scanner(System.in);
            IssueTrackingObject itracker = new IssueTrackingObject();
            while(exit != -1) {
                System.out.println("Select an Option or -1 to exit\n1. Create Issue\n2. Solve Issue\n"
                                  + "3. View solved issues\n4. View unsolved issues");
                exit = input.nextInt();
                switch (exit) {
                case(0) :
                    System.out.println("Number of issues has to be greater than zero");
                break;
                case(1) :
                    itracker.createIssue();
                break;
                case(2) :
                    itracker.solvedIssue();
                break;
                case(3) :
                    itracker.printSolvedIssue();
                break;
                case(4) :
                    itracker.printUnsolvedIssue();
                break;
                }

            }
            input.close();
            System.out.println("Program finished");
        }

    }

Feel free to make modifications if I didn't quite get what you were trying to do.