1

First of all, sorry if code looks messy. I have to make a To do list on Java where the user can add and delete chores, without using regex. I can only use methods from the String class. Every chore entered should end with "\n":

list = list + addedChore + "\n"

If user wants to delete the first chore for example:

endofChore1 = list.indexOf("\n", 0);
chore1 = list.substring(0, endofChore1);
chore1 = ""; 

What i tried to do was to get the index of the first \n and make it a substring starting from index 0 to the index of the first \n, and finally replace that substring with an empty string that way it deletes the chore from the list. However, it is not working. What can I do to fix this? Thank you.

Edit: This is what i wrote to delete any given chore. I don't see what's wrong with it.

int choreToDelete;   // number associated with chore user wants to delete
int begin = 0;
int i = 0;
int end;
String stringToDelete;


  if (choreToDelete >= 1 && choreToDelete <= numberOfChores){
   while (i < choreToDelete - 1) {
     end = list.indexOf("\n", begin);
     begin = end;
     i = i + 1;
   }
  }
stringToDelete = list.indexOf("\n", begin);
list = list.substring(stringToDelete);
2
  • 1
    Where is the rest of your code? Also, note that you extracted part of the list, leaving the list exactly as it was before. Then you assign the variable you extracted that part to a new value, so you've now a) not touched list and b) made the chore1 = list.... instruction a noop. Commented Nov 7, 2018 at 0:40
  • @babyprog Strings are immutable Commented Nov 7, 2018 at 0:42

1 Answer 1

1

Strings are immutable.

Replace

chore1 = list.substring(0, endofChore1);
chore1 = ""; 

with

list = list.substring(endofChore1);

Here is your full code:-

public static void main(String[] args) {
    String list = "choreA\nchoreB\n";
    int endofChore1 = list.indexOf("\n", 0);
    list = list.substring(endofChore1);
    System.out.println(list);
}

PS - Not trying to come up with the best logic, just fixing the current issue the OP is having with their code.

Edit:

Since you have shown effort of making a generic delete method, here is the full code. It handles cases like if choreToDelete is less than 1 and if it is more than the number of chores.

public static void main(String[] args) {
    String list = "choreA\nchoreB\nchoreC\n";
    list = deleteChore(list, 2);
    System.out.println(list);
}

private static String deleteChore(String list, int choreToDelete) {
    if (choreToDelete < 1 || choreToDelete > list.split("\n").length) return list;
    if (choreToDelete == 1) return list.substring(list.indexOf("\n") + 1);
    if (!list.endsWith("\n")) list = list + "\n";
    int startIndexOfChoreToDelete = nthIndexOf(list, "\n", choreToDelete - 1);
    int endIndexOfChoreToDelete = nthIndexOf(list, "\n", choreToDelete);
    return list.substring(0, startIndexOfChoreToDelete) + list.substring(endIndexOfChoreToDelete);
}

private static int nthIndexOf(String str, String substr, int n) {
    int pos = str.indexOf(substr);
    while (--n > 0 && pos != -1)
        pos = str.indexOf(substr, pos + 1);
    return pos;
}
Sign up to request clarification or add additional context in comments.

5 Comments

What would happen if chore1 was a substring of chore2 e.g. String list = "choreA\nchoreB\nchoreAB\n";
@ScaryWombat I made a change, it will work for the first chore, so it's still not a generic method that can delete any chore, but will do the current task the OP is trying to do. I'll write a generic one if no one else does.
@Kartik even a hint would be very appreciated, i have been trying to write a generic one since yesterday but to no avail. thank you very much.
@babyprog say your string is choreA\nchoreB\nchoreC. Now you want to remove the second chore. You can loop and find indexes of first and second \n. Then take the substring before first \n and join it with the substring after second \n. Let me know if you get stuck.
@babyprog since you have tried, I have added the complete solution to the answer. Make sure you understand it and not just copy it. Good luck!

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.