-1

I have an assignment due in about 6 hours and I really need help. I stayed up all night working on this but I can't seem to figure it out.

Basically what I need to do is, I have a user defined array. I'm supposed to take that array and manipulate that string into a variety of things. I've gotten most of it but I can't seem to remove a user defined element in the user defined string. I've tried using a for loop to try and find the specific character that the user wants to remove but I can't seem to get it to compile or write properly. This is my code so far:

import java.util.Scanner;
import java.util.Arrays;

public class StringManipulator {

    public static void main(String[] args) {
        String userStr;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the string to be manipulated");
        userStr = input.nextLine();

        while (true) {
            System.out.println("Enter your command");
            System.out.println("Print Reverse");
            System.out.println("Replace All");
            System.out.println("Replace Single");
            System.out.println("Remove");
            System.out.println("Quit");
            String choice = input.nextLine();
            String[] array = userStr.split("");


            if (choice.equals("Print Reverse") || choice.equals("print reverse")) { //reverses the string
                for(int i = array.length - 1;i >= 0; i --) {
                    System.out.print(array[i]);
                }
                System.out.println();
            }


            else if (choice.equals("Replace All")) { //Replaces all input letters with new letters that user inputs
                System.out.println("What letter would you like to replace?");
                String ridOf = input.nextLine();
                System.out.println("What letter do you want to replace it as?");
                String replace = input.nextLine();
                String[] newArray = array;
                for (int i = 0; i < array.length; i++) {
                    if(array[i].equals(ridOf)) {
                        array[i] = replace;
                    }
                }
                System.out.println("");
                for(int i = 0; i < array.length; i++) {
                    System.out.print(array[i]);
                }
                System.out.println("");
            }

            else if (choice.equals("Replace Single") || choice.equals("replace single")) {
                System.out.println("Enter the character to replace?");
                String ridOf1 = input.nextLine();
                System.out.println("Enter the new character");
                String replace1 = input.nextLine();
                System.out.println("Which " + ridOf1 + " would you like to replace?");
                int choice1 = input.nextInt();

            }

            else if (choice.equals("Remove") || choice.equals("remove")) {
                System.out.println("Enter the character to remove");
                String ridOf2 = input.nextLine();
                char charRemove = ridOf2.charAt(0);
                for (int i = 0; i < array.length; i++) {
                    if(userStr.charAt(i) != ridOf2) {
                        userStr += userStr.charAt(i);
                    }
                }
            }
            else if (choice.equals("Quit") || choice.equals("quit")) {
                System.exit(0);
            }
        }
    }
}

The only section that i'm actually worried about at this moment is the

else if(choice.equals("Remove")

section of the code.

leed38
  • 314
  • 1
  • 4
  • 14
  • what do you think the line ``userStr += userStr.charAt(i);`` does? – f1sh Apr 26 '18 at 14:13
  • This does not resolve your problem but instead of doing `choice.equals("Remove") || choice.equals("remove")` you can use [`String.equalsIgnoreCase()`](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#equalsIgnoreCase(java.lang.String)) – vincrichaud Apr 26 '18 at 14:14
  • Your code does not compile because the if case on line 64 is comparing a character and a String. It should be `if(userStr.charAt(i) != charRemove)` – Paul Benn Apr 26 '18 at 14:17
  • @f1sh I was thinking it would go along the lines of rewriting the string without the variable ridOf2. Tbh, I wasn't paying that much attention in class when the lecturer went over it. – leed38 Apr 26 '18 at 14:18
  • @vincrichaud Thank you! I was actually going to have to do some research on how to ignore the capital and lower case letters. You just sped up my research alot. Thank you! – leed38 Apr 26 '18 at 14:19
  • @PaulBenn That definitely fixed it. I can't believe I missed such a simple mistake... It's been a long night. Thanks for your help! – leed38 Apr 26 '18 at 14:22
  • 1
    Please read [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) before attempting to ask more questions. –  Apr 26 '18 at 14:27
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). Your question can be answered very quickly and easily with your step-debugger. You should always try and solve your problems with a step debugger before coming to StackOverflow. –  Apr 26 '18 at 14:28

2 Answers2

-1

Give this a shot explanation further down

import java.util.Scanner;

public class Main{

    public static void main(String[] args) {
        String userStr;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the string to be manipulated");
        userStr = input.nextLine();

        while (true) {
            System.out.println("Enter your command");
            System.out.println("Print Reverse");
            System.out.println("Replace All");
            System.out.println("Replace Single");
            System.out.println("Remove");
            System.out.println("Quit");
            String choice = input.nextLine();
            String[] array = userStr.split("");

            if (choice.equalsIgnoreCase("Print Reverse")) { //reverses the string
                for(int i = array.length - 1;i >= 0; i --) {
                    System.out.print(array[i]);
                }
                System.out.println();
            }

            else if (choice.equalsIgnoreCase("Replace All")) { //Replaces all input letters with new letters that user inputs
                System.out.println("What letter would you like to replace?");
                String ridOf = input.nextLine();
                System.out.println("What letter do you want to replace it as?");
                String replace = input.nextLine();
                String[] newArray = array;
                for (int i = 0; i < array.length; i++) {
                    if(array[i].equals(ridOf)) {
                        array[i] = replace;
                    }
                }
                System.out.println("");
                for(int i = 0; i < array.length; i++) {
                    System.out.print(array[i]);
                }
                System.out.println("");
            }

            else if (choice.equalsIgnoreCase("Replace Single")) {
                System.out.println("Enter the character to replace?");
                String ridOf1 = input.nextLine();
                System.out.println("Enter the new character");
                String replace1 = input.nextLine();
                System.out.println("Which " + ridOf1 + " would you like to replace?");
                int choice1 = input.nextInt();

            }

This is the section I changed .equalsIgnoreCase compares them ignoring case as expected. Then I made sure the user only entered one char and if more ignore them. Then changed the char remove to be a string of only the first char(because you cannot replace with a char it needs to be a string) then I replaced the char to be removed in the string with nothing essentially removing it and set the user string to the new user string

            else if (choice.equalsIgnoreCase("Remove")) {
                System.out.println("Enter the character to remove");
                String ridOf2 = input.nextLine();
                String charRemove = String.valueOf(ridOf2.charAt(0));
                userStr = userStr.replaceAll(charRemove, "");//This will replace the first char the enter with nothing
            }

End of section

            else if (choice.equals("Quit") || choice.equals("quit")) {
                System.exit(0);
            }
        }
    }
}
Matt
  • 3,052
  • 1
  • 17
  • 30
  • also if whoever downvoted me cares to elaborate that would be great thanks if not enjoy your -1 rep aswell – Matt Apr 26 '18 at 14:35
  • @Matt Thank you Matt. I really appreciate your help. – leed38 Apr 26 '18 at 14:36
  • No problem if this answered your question please accept the answer so others don't think this is still open – Matt Apr 26 '18 at 14:37
-2

String's replace should do it -

public class Test
{
  public static void main(String [] args) {
    String s = "Hello World";
    s = s.replace(" ", "");
    System.out.println(s);
  }
}
Shawn Eion Smith
  • 417
  • 4
  • 18
  • I can't say I follow this code all the way. Could you break it down please? To be specific, I'm not sure what line 5 of your code does. – leed38 Apr 26 '18 at 14:23
  • See the [String class documentation for this method](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#replace-char-char-) – Paul Benn Apr 26 '18 at 14:27
  • I'm just using replace to remove a character. In the simple example I used replace to replace a blank space " " with nothing "". Replace the blank space with the character you want to replace in your code. – Shawn Eion Smith Apr 26 '18 at 15:52