请老师看看为什么1-播放列表管理导出歌单功能和2-播放器管理中删除播放列表功能抛出异常
package com.imooc.Music; //song类 public class Song { private String id; private String name; private String singer; public void setId(String id) { this.id = id; } public String getId() { return this.id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSinger() { return singer; } public void setSinger(String singer) { this.singer = singer; } public Song(String id, String name, String singer) { this.setId(id); this.setName(name); this.setSinger(singer); } public Song() { super(); // TODO Auto-generated constructor stub } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((singer == null) ? 0 : singer.hashCode()); return result; } @Override public boolean equals(Object obj) { if(this==obj) return true; if(obj.getClass()==Song.class) { Song s=(Song)obj; return (this.getId().equals(s.getId()))&&(this.getName().equals(s.getName()))&&(this.getSinger().equals(s.getSinger())); } else return false; } @Override public String toString() { return "歌曲信息:id为:" + id + ",歌曲名称为:" + name + ",演唱者为:" + singer ; } } package com.imooc.Music; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import java.util.Scanner; //PlayList类 public class PlayList implements Serializable{ private String playListName; private List<Song> musicList = new ArrayList<Song>(); Scanner sc = new Scanner(System.in); public String getPlayListName() { return playListName; } public void setPlayListName(String playListName) { this.playListName = playListName; } public List<Song> getMusicList() { return musicList; } public void setMusicList(List<Song> musicList) { this.musicList = musicList; } public PlayList(String playListName) { this.setPlayListName(playListName); this.setMusicList(musicList); } public PlayList(String playListName, List<Song> musicList) { super(); this.setPlayListName(playListName); this.setMusicList(musicList); } public PlayList() { } // 添加歌曲到播放列表的方法 public void addToPlayList(Song song) { this.musicList.add(song); } // 显示播放列表歌曲的方法 public void displayAllSong() { System.out.println(this.getPlayListName()); System.out.println("播放列表中的所有歌曲为:"); for (int i = 0; i < this.musicList.size(); i++) System.out.println((Song)this. musicList.get(i)); } // 通过id查询歌曲的方法 public Song searchSongById(String id) { Song song = new Song(); boolean flag = false; for (int i = 0; i < this.musicList.size(); i++) { if ((this.musicList.get(i).getId()) .equals(id)) { song = (Song)this. musicList.get(i); flag = true; break; } } if (flag == false) // 编程常犯错误:将赋值当等于 (flag=false) System.out.println("未找到该歌曲。"); return song; } // 通过歌名查询歌曲的方法 public Song searchSongByName(String n) { Song song = new Song(); boolean flag = false; for (int i = 0; i < musicList.size(); i++) { if ((musicList.get(i).getName()).equals(n)) { song = (Song) musicList.get(i); flag = true; break; } } if (flag == false) System.out.println("未找到该歌曲。"); return song; } // 修改歌曲的方法 public void updateSong(String id) { boolean flag = false; for (int i = 0; i < musicList.size(); i++) { if ((musicList.get(i).getId()).equals(id)) { flag = true; System.out.println("修改的歌曲信息为:"); System.out.println((Song) musicList.get(i)); System.out.println("请输入修改后的歌曲名称"); (musicList.get(i)).setName(sc.next()); System.out.println("请输入修改后的歌曲演唱者"); (musicList.get(i)).setSinger(sc.next()); System.out.println("修改成功!"); break; } } if (flag == false) System.out.println("未找到该歌曲。"); } // 通过id删除歌曲的方法 public void deleteSong(String id) { for (int i = 0; i < musicList.size(); i++) { if((musicList.get(i).getId()).equals(id)) musicList.remove(i); } System.out.println("删除后的播放列表为:"); for (int i = 0; i < musicList.size(); i++) { System.out.println((Song)musicList.get(i)); } } //导出歌单 public void exportPlayList() { try { FileOutputStream fos=new FileOutputStream(this.getPlayListName()+"的歌单.txt"); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(musicList); oos.flush(); oos.close(); fos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } package com.imooc.Music; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.Map.Entry; import java.util.Scanner; //PlayListCollection类 public class PlayListCollection { Map<String,PlayList> playListMap=new HashMap<String,PlayList>(); public Map<String, PlayList> getPlayListMap() { return playListMap; } public void setPlayListMap(Map<String, PlayList> playListMap) { this.playListMap = playListMap; } public PlayListCollection(Map<String, PlayList> playListMap) { this.setPlayListMap(playListMap); } //添加播放列表 public void addPlayList(PlayList playList) { playListMap.put(playList.getPlayListName(), playList); } //删除播放列表 public void deletePlayList(PlayList playList) { playListMap.remove(playList.getPlayListName()); } //通过名字查询播放列表 public PlayList searchPlayListByName(String playListName) { Set<Entry<String, PlayList>> entrySet = playListMap.entrySet(); PlayList p=new PlayList(); for(Entry<String, PlayList> entry:entrySet) { if(entry.getKey().equals(playListName)) p=entry.getValue(); } return p; } public boolean search(String playListName) { Set<Entry<String, PlayList>> entrySet = playListMap.entrySet(); boolean flag=false; for(Entry<String, PlayList> entry:entrySet) { if(entry.getKey().equals(playListName)) flag=true; } return flag; } //显示所有播放列表名称 public void displayPlayListName(){ System.out.println("播放列表名称为:"); Set<Entry<String, PlayList>> entrySet = playListMap.entrySet(); for(Entry<String, PlayList> entry:entrySet) { System.out.println(entry.getKey()); } } } package com.imooc.Music; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; import java.util.Map.Entry; //Test类 public class TestDemo { public void mainMenu() { System.out.println("****************************************"); System.out.println(" **主菜单**"); System.out.println(" 1--播放列表管理"); System.out.println(" 2--播放器管理"); System.out.println(" 0--退出"); System.out.println("****************************************"); System.out.println("请输入对应数字进行操作:"); } public void playListMenu() { System.out.println("****************************************"); System.out.println(" **播放列表管理**"); System.out.println(" 1--将歌曲添加到主播放列表"); System.out.println(" 2--将歌曲添加到普通播放列表"); System.out.println(" 3--通过歌曲id查询播放列表中的歌曲"); System.out.println(" 4--通过歌曲名称查询播放列表中的歌曲"); System.out.println(" 5--修改播放列表中的歌曲"); System.out.println(" 6--删除播放列表中的歌曲"); System.out.println(" 7--显示播放列表中的所有歌曲"); System.out.println(" 8--导出歌单"); System.out.println(" 9--返回上一级菜单"); System.out.println("****************************************"); System.out.println("请输入对应数字对播放列表进行管理:"); } public void playerMenu() { System.out.println("****************************************"); System.out.println(" **播放器管理**"); System.out.println(" 1--向播放器添加播放列表"); System.out.println(" 2--从播放器删除播放列表"); System.out.println(" 3--通过名字查询播放列表信息"); System.out.println(" 4--显示所有播放列表名称"); System.out.println(" 9--返回上一级菜单"); System.out.println("****************************************"); System.out.println("请输入对应数字对播放器进行操作:"); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); PlayList p1 = new PlayList("主播放列表"); Map<String, PlayList> playListMap = new HashMap<String, PlayList>(); playListMap.put("主播放列表", p1); Set<Entry<String, PlayList>> entrySet = playListMap.entrySet(); PlayListCollection player = new PlayListCollection(playListMap); TestDemo t = new TestDemo(); while (true) { t.mainMenu(); int x = sc.nextInt(); switch (x) { case 1: { while (true) { t.playListMenu(); int y = sc.nextInt(); switch (y) { case 1: { System.out.println("请输入要添加的歌曲数量"); int num = sc.nextInt(); for (int i = 0; i < num; i++) { System.out.println("请输入第" + (i + 1) + "首歌曲:"); System.out.println("请输入第" + (i + 1) + "首歌曲的id:"); String id = sc.next(); System.out.println("请输入第" + (i + 1) + "首歌曲的名称:"); String name = sc.next(); System.out.println("请输入第" + (i + 1) + "首歌曲的歌手:"); String singer = sc.next(); Song song = new Song(id, name, singer); p1.addToPlayList(song); } continue; } case 2: { System.out.println("请输入要添加的播放列表名称:"); String listName = sc.next(); boolean flag = player.search(listName); if(!flag) {System.out.println("该播放列表未找到,请先在播放器中添加该播放列表。"); continue; } if (flag) { System.out.println("请输入要添加的歌曲数量"); int num = sc.nextInt(); while (true) { for (int i = 0; i < num; i++) { System.out.println("请输入第" + (i + 1) + "首歌曲:"); System.out.println("请输入第" + (i + 1) + "首歌曲的id:"); String id = sc.next(); if (p1.getMusicList().get(i).getId().equals(id)) { player.searchPlayListByName(listName).addToPlayList(p1.searchSongById(id)); continue; } else { System.out.println("该歌曲在主播放列表中不存在,请输入该歌曲的其他信息"); System.out.println("请输入第" + (i + 1) + "首歌曲的名称:"); String name = sc.next(); System.out.println("请输入第" + (i + 1) + "首歌曲的歌手:"); String singer = sc.next(); Song song = new Song(id, name, singer); player.searchPlayListByName(listName).addToPlayList(song); } } break; } p1.displayAllSong(); player.searchPlayListByName(listName).displayAllSong(); continue; } } case 3: { System.out.println("输入播放列表名称:"); String listName = sc.next(); for (Entry<String, PlayList> entry : entrySet) { if (entry.getKey().equals(listName)) { System.out.println("请输入查询歌曲的id"); String id = sc.next(); System.out.println("查询到的歌曲信息为:"); System.out.println(entry.getValue().searchSongById(id)); break; } else System.out.println("该歌曲在" + listName + "中不存在。"); } continue; } case 4: { System.out.println("输入播放列表名称:"); String listName = sc.next(); for (Entry<String, PlayList> entry : entrySet) { if (entry.getKey().equals(listName)) { System.out.println("请输入查询歌曲的名称"); String name = sc.next(); System.out.println("查询到的歌曲信息为:"); System.out.println(entry.getValue().searchSongByName(name)); break; } else System.out.println("该歌曲在" + listName + "中不存在。"); } continue; } case 5: { System.out.println("请输入修改歌曲的id"); String id = sc.next(); for (Entry<String, PlayList> entry : entrySet) { entry.getValue().updateSong(id); } continue; } case 6: { System.out.println("请输入删除歌曲的id"); String id = sc.next(); for (Entry<String, PlayList> entry : entrySet) { entry.getValue().deleteSong(id); } continue; } case 7: { for (Entry<String, PlayList> entry : entrySet) { entry.getValue().displayAllSong(); } continue; } case 8: p1.exportPlayList(); continue; case 9: } break; } continue; } // **播放器管理**" // 1--向播放器添加播放列表; // 2--从播放器删除播放列表; // 3--通过名字查询播放列表信息; // 4--显示所有播放列表名称; // 9--返回上一级菜单; case 2: { while (true) { t.playerMenu(); int y = sc.nextInt(); switch (y) { case 1: { System.out.println("请输入要添加的播放列表名称"); String listName = sc.next(); PlayList p = new PlayList(listName); player.addPlayList(p); continue; } case 2: { System.out.println("请输入要删除的播放列表名称"); String listName = sc.next(); for (Entry<String, PlayList> entry : entrySet) { if (entry.getKey().equals(listName)) player.deletePlayList(entry.getValue()); } continue; } case 3: { System.out.println("请输入要查询的播放列表名称"); String listName = sc.next(); PlayList ps = player.searchPlayListByName(listName); System.out.println("该播放列表存在!该播放列表的名称为:"); System.out.println(ps.getPlayListName()); continue; } case 4: player.displayPlayListName(); continue; case 9: } break; } continue; } case 0: break; } break; } } }
0
收起
正在回答
1回答
同学你好,删除播放列表中的歌曲报错如下:
这个错误是因为同学在遍历的过程中对播放列表进行了删除。所以删除播放列表时,建议在输入播放列表名称后,调用通过名字查询播放列表信息的方法查询播放列表是否存在,存在则删除,这样就不必对播放器进行遍历并删除,避免抛出异常。
2、导出歌单时报错如下:
这个异常是提示Song类需要实现序列化,如:
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星