从用户定义的字符串数组中删除用户定义的元素

时间:2018-04-26 14:10:13

标签: java arrays

我有约6个小时的作业,我真的需要帮助。我熬夜工作,但我似乎无法解决这个问题。

基本上我需要做的是,我有一个用户定义的数组。我应该采用该数组并将该字符串操作为各种各样的东西。我已经掌握了大部分内容,但我似乎无法删除用户定义字符串中的用户定义元素。我尝试使用for循环来尝试找到用户想要删除的特定字符,但我似乎无法正确编译或编写它。到目前为止,这是我的代码:

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);
            }
        }
    }
}

此时我唯一真正担心的部分是

else if(choice.equals("Remove")

代码部分。

2 个答案:

答案 0 :(得分:-1)

进一步向下解释

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();

            }

这是我更改的部分.equalsIgnoreCase比较他们忽略了预期的情况。然后我确保用户只输入了一个char,如果更多则忽略它们。然后将char remove更改为只有第一个char的字符串(因为你不能用char替换它需要是一个字符串)然后我替换了要在字符串中删除的char而没有基本上删除它并设置用户字符串到新用户字符串

            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
            }

结束部分

            else if (choice.equals("Quit") || choice.equals("quit")) {
                System.exit(0);
            }
        }
    }
}

答案 1 :(得分:-2)

字符串的替换应该这样做 -

public class Test
{
  public static void main(String [] args) {
    String s = "Hello World";
    s = s.replace(" ", "");
    System.out.println(s);
  }
}