0

I am making a program where the person inputs multiple values and the Arraylist stores it as a single string with option 1. Option two allows you to change a specific part of that string from "Complete" to "Incomplete" and vice-versa but only if the status part of the String is one of the two. I am getting the warning "Result of 'String.replace()' is ignored" and the part of the string isn't updating. Any help would be appreciated!

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

public class Main {
    public static void main(String[] args) {
        Array listObj = new Array();
        Scanner userinput = new Scanner(System.in);
        int arraysize = (listObj.list.size());

        int power = 1;
        while (power < 2) {
            System.out.println("To-Do List / What would you like to do?");
            System.out.println("1 = Add Task / 2 = Mark Task as Done / 3 = Remove Task / 4 = Edit Task / 5 = Display Tasks / 6 = Exit");
            int selection = userinput.nextInt();


            if (selection == 1) {
                for (int i = 0; i <= arraysize; i++) {
                    String title;
                    String date;
                    String status;
                    String description;
                    String id;

                    System.out.print("Enter Title: ");
                    title = userinput.next();
                    System.out.print("Enter Due Date: ");
                    date = userinput.next();
                    System.out.print("Enter Status (Complete or Incomplete): ");
                    status = userinput.next();
                    System.out.print("Enter Description: ");
                    description = userinput.next();
                    listObj.list.add(title + " " + date + " " + status + " " + description);
                    System.out.println();
                    listObj.list.forEach(System.out::println);
                    System.out.println();
                }
            }
            if (selection == 2) {
                int idinput;
                System.out.println("Enter Project ID to Toggle Complete/Incomplete: ");
                idinput = (userinput.nextInt()-1);
                System.out.println();
                System.out.println(listObj.list.get(idinput));
                System.out.println();
                System.out.println("What is the status of this assignment?: ");
                String toggleselect = userinput.next();
                if (toggleselect.equals("Incomplete")) {
                    listObj.list.get(idinput).replace("Incomplete", "Complete");
                } else if (toggleselect.equals("Complete")) {
                    listObj.list.get(idinput).replace("Complete", "Incomplete");
                } else {
                    System.out.println("Status is not Complete/Incomplete");
                }
                System.out.println();
                listObj.list.forEach(System.out::println);
                System.out.println();
            }
        }
    }
}


import java.util.ArrayList;
public class Array {
    ArrayList<String> list = new ArrayList<String>();

    public ArrayList<String> getList() {
        return list;
    }
}`your text`

1
  • The end of your quoted code looks like a copy/paste accident?: "}your text" Commented Dec 7, 2022 at 15:56

1 Answer 1

1
public String replace(char searchChar, char newChar)

demands a String to output to, as you can see by the return type. In this case use

public E set(int index, E element)

from the ArrayList library,

with E return type being your ArrayList,

and E element being your call

listObj.list.get(idinput).replace("Complete", "Incomplete");

So,

listObj.list.get(idinput).replace("Complete", "Incomplete");

becomes

listObj.list.set(idinput, listObj.list.get(idinput).replace("Complete", "Incomplete"));

Also, why are you creating a class called Array that just encapsulates an ArrayList in it?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.