Java 使用字符串数组删除方法

Java 使用字符串数组删除方法,java,arrays,Java,Arrays,我正在尝试编写一个方法来删除CD(CD包括艺术家姓名、专辑标题和曲目标题)。。有5张CD,我想删除其中的一张。。。这就是该方法应该做的: void delete()将1)要求用户提供艺术家和标题,然后尝试查找带有 匹配艺术家和标题,2)如果找到,则显示CD,或告诉用户找不到,3) 如果找到,请用户确认删除(这需要键盘输入),删除CD 如果用户确认,则输入 这是我的代码: public void delete() { Scanner deleteInput = new Scanner(Sy

我正在尝试编写一个方法来删除CD(CD包括艺术家姓名、专辑标题和曲目标题)。。有5张CD,我想删除其中的一张。。。这就是该方法应该做的: void delete()将1)要求用户提供艺术家和标题,然后尝试查找带有 匹配艺术家和标题,2)如果找到,则显示CD,或告诉用户找不到,3) 如果找到,请用户确认删除(这需要键盘输入),删除CD 如果用户确认,则输入

这是我的代码:

public void delete() {
    Scanner deleteInput = new Scanner(System.in);
    System.out.println("Which artist you would like to delete? ");
    System.out.println("Enter artist name and title to be deleted:");
    String artist = deleteInput.nextLine();
    String title = deleteInput.nextLine();

    for (int i = 0; i <= CDlist.length - 1; i++) {

        if ((CDlist[i].getArtist().equals(artist))
                && (CDlist[i].getTitle().equals(title))) {
            System.out.println("Found: " + CDlist[i].getArtist() + " "
                    + CDlist[i].getTitle());
            if (CDlist[i] == null) {
                continue;
            }

            System.out.println("Would you like to delete it? Yes 0 No 1");                
            if (deleteInput.nextInt() == 1) {
                CDlist[i] = null;
                cdnum--;
            }
        } else {
            System.out.println("CD not found in the list.");
        }
    }
public void delete(){
扫描仪deleteInput=新扫描仪(System.in);
System.out.println(“您想删除哪个艺术家?”);
System.out.println(“输入要删除的艺术家名称和标题:”);
字符串艺术家=deleteInput.nextLine();
String title=deleteInput.nextLine();

for(int i=0;i这就是我的意思。另外,在for循环开始之后,您的第一个
if
中有一个额外的
{
。我希望这不会引起问题

我根据一些评论添加了忽略案例部分

public void delete() {
    Scanner deleteInput = new Scanner(System.in);
    System.out.println("Which artist you would like to delete? ");
    System.out.println("Enter artist name and title to be deleted:");
    String artist = deleteInput.nextLine();
    String title = deleteInput.nextLine();

    boolean found = false;
    int idx = -1;

    System.err.println("DEBUG: Input Data");
    System.err.println("Artist Name: "+artist+" Length of String: "+artist.length());
    System.err.println("Title: "+artist+" Length of String: "+title.length());
    System.err.println();

    for (int i = 0; i <= CDlist.length - 1; i++) {

        if (CDlist[i]!=null) {
            System.err.println("DEBUG: Checking Index "+i);
            System.err.println("Artist Name: "+CDlist[i].getArtist()+" Length of String: "+CDlist[i].getArtist().length() + " Matches: "+CDlist[i].getArtist().equalsIgnoreCase(artist));
            System.err.println("Title: "+CDlist[i].getTitle()+" Length of String: "+CDlist[i].getTitle().length() + " Matches: "+CDlist[i].getTitle().equalsIgnoreCase(title));
            System.err.println();
        }

        if (CDlist[i]!=null && CDlist[i].getArtist().equalsIgnoreCase(artist) && CDlist[i].getTitle().equalsIgnoreCase(title)) {
            System.out.println("Found: " + CDlist[i].getArtist() + " " + CDlist[i].getTitle());
            found = true;
            idx = i;
                    break;
        }
    }

    if (found) {
        System.out.println("Would you like to delete it? Yes 0 No 1");                
        if (Integer.parseInt(deleteInput.nextLine()) == 1) {
            CDlist[idx] = null;
            //I am assuming cdnum is a variable of the class that can be accessed.
            cdnum--;
        }
    } else {
        System.out.println("CD not found in the list.");
    }
}
public void delete(){
扫描仪deleteInput=新扫描仪(System.in);
System.out.println(“您想删除哪个艺术家?”);
System.out.println(“输入要删除的艺术家名称和标题:”);
字符串艺术家=deleteInput.nextLine();
String title=deleteInput.nextLine();
布尔值=false;
intidx=-1;
System.err.println(“调试:输入数据”);
System.err.println(“艺术家名称:“+Artist+”字符串长度:“+Artist.Length()”);
System.err.println(“Title:+artist+”字符串长度:“+Title.Length());
System.err.println();

for(int i=0;我是对的,因此在尝试执行任何操作之前,它将检查null。您可以将其单独放置,但需要将其放置在第一个if语句之前。

这看起来像Java,因此我已将其标记为Java。如果我不正确,请随意修复它。如果您适当地标记内容,您将获得更多响应。
getArtist()
是否返回字符串?调试:请参见
artist
CDlist[i].getArtist()中的值
并查看不匹配的原因。尝试不区分大小写的匹配。还要添加空检查是否在错误的位置。是getArtist返回艺术家,它是一个string@Danny我不能让它保持原样吗?例如,我有一张名为Micheal Jackson Thriller的CD…当我使用删除方法时,有人问我CD的名称是什么,我输入了麦克风heal Jackson Thriller,它应该找到并删除它,但是[i]System.out.println(“是否要删除它?是0否1”);如果(deleteInput.nextInt()==1){CDlist[i]=null;cdnum--;}找不到,这是我遇到的错误。好吧,我测试了代码,但在我的输出列表中仍然没有找到CD…而且CD没有被删除。它没有问我是否要删除CD。它应该问我是的,没有问题。我正在输入一个调试版本,该版本可以帮助你自己解决所有问题。秒。对不起,我是一个newbie与计算机编程…你为什么要使idx=-1?它可以是任何东西。它只是索引的占位符,我们可以在其中找到匹配的CD。它仅在find=true时使用。我们需要idx,因为我不存在于循环之外。