使用Scanner对象的Close方法后,程序报错:NoSuchElementException
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 | package com.imooc2.course.Course5_1_Shopping; // 商品类 属性:商品ID,商品名称,商品价格,商品描述 public class Goods { private String goodsId; // 商品ID private String goodsName; // 商品名称 private double price; // 商品价格 private String goodsDesp; // 商品描述 public Goods(String goodsId, String goodsName, double price, String goodsDesp) { super (); this .setGoodsId(goodsId); this .setGoodsName(goodsName); this .setPrice(price); this .setGoodsDesp(goodsDesp); } public String getGoodsId() { return goodsId; } public void setGoodsId(String goodsId) { this .goodsId = goodsId; } public String getGoodsName() { return goodsName; } public void setGoodsName(String goodsName) { this .goodsName = goodsName; } public double getPrice() { return price; } public void setPrice( double price) { this .price = price; } public String getGoodsDesp() { return goodsDesp; } public void setGoodsDesp(String goodsDesp) { this .goodsDesp = goodsDesp; } @Override public String toString() { return "商品信息 [编号:" + goodsId + ", 名称:" + goodsName + ", 价格:" + price + ", 描述:" + goodsDesp + "]" ; } @Override public int hashCode() { final int prime = 31 ; int result = 1 ; result = prime * result + ((goodsId == null ) ? 0 : goodsId.hashCode()); return result; } @Override public boolean equals(Object obj) { if ( this == obj) return true ; if (Goods. class == obj.getClass()) { Goods goods = (Goods) obj; return this .getGoodsId().equals(goods.getGoodsId()); } return false ; } } //购物车中的商品类:属性:商品,数量 package com.imooc2.course.Course5_1_Shopping; public class GoodsInCart { private Goods goods; private int num; public GoodsInCart() { } public GoodsInCart(Goods goods, int num) { super (); this .setGoods(goods); this .setNum(num); } public Goods getGoods() { return goods; } public void setGoods(Goods goods) { this .goods = goods; } public int getNum() { return num; } public void setNum( int num) { this .num = num; } @Override public String toString() { return "商品编号:" + goods.getGoodsId() + ", 商品名称:" + goods.getGoodsName() + ", 商品价格:" + goods.getPrice() + ", 商品描述:" + goods.getGoodsDesp() + ", 数量:" + this .getNum(); } } //商品管理类 package com.imooc2.course.Course5_1_Shopping; import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class GoodsManager { private Set<Goods> goodsSet; public GoodsManager() { } public GoodsManager(Set<Goods> goodsSet) { super (); this .goodsSet = goodsSet; } public Set<Goods> getGoodsSet() { if (goodsSet == null ) goodsSet = new HashSet<Goods>(); return goodsSet; } public void setGoodsSet(Set<Goods> goodsSet) { this .goodsSet = goodsSet; } //导入商品 public void importGoods() { Goods good1 = new Goods( "goods004" , "手机" , 2300.0 , "android手机" ); Goods good2 = new Goods( "goods002" , "饮水机" , 299.0 , "带净化功能的饮水机" ); Goods good3 = new Goods( "goods003" , "笔记本电脑" , 4999.0 , "15寸笔记本电脑" ); Goods good4 = new Goods( "goods001" , "水杯" , 56.0 , "不锈钢水杯" ); this .getGoodsSet().add(good1); this .getGoodsSet().add(good2); this .getGoodsSet().add(good3); this .getGoodsSet().add(good4); System.out.println( "商品信息导入成功" ); } //展示商品 public void showGoods() { if ( this .getGoodsSet().isEmpty()) { System.out.println( "还没有商品,记得导入商品信息哟" ); return ; } System.out.println( "所有商品信息为:" ); Iterator<Goods> it = this .getGoodsSet().iterator(); while (it.hasNext()) { System.out.println(it.next()); } } } //购物车管理类 package com.imooc2.course.Course5_1_Shopping; import java.util.HashMap; import java.util.InputMismatchException; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; import java.util.Set; public class ShoppingCart { private Map<String, GoodsInCart> shoppingCart; public ShoppingCart() { } public Map<String, GoodsInCart> getShoppingCart() { if (shoppingCart == null ) shoppingCart = new HashMap<String, GoodsInCart>(); return shoppingCart; } public void setShoppingCart(Map<String, GoodsInCart> shoppingCart) { this .shoppingCart = shoppingCart; } //添加商品到购物车 public void addGoodsToShoppingCart(GoodsManager goodsManager) { if (goodsManager.getGoodsSet().isEmpty()) { System.out.println( "商城中还没有商品,请先导入系统预定商品!" ); return ; } goodsManager.showGoods(); String goodNum = "" ; //接收加入购物车的商品编号 int shopNum = 0 ; //接收将商品加入购物车的数量 GoodsInCart goodsInCart = new GoodsInCart(); // 购物车中商品信息,含有商品名称和商品数量 Scanner sc = new Scanner(System.in); boolean iDIsexit = false ; //判断输入的ID是否已经添加到了购物车 do { System.out.println( "请输入商品编号:" ); goodNum = sc.next(); if ( this .getShoppingCart().containsKey(goodNum)) { System.out.println( "该商品已添加至购物车中,如需修改,请选择修改购物车中的商品数量" ); return ; // 如果使用continue要求重新输入编号的话,当购物车中添加了所有编号的商品时,会进入死循环; } for (Goods it : goodsManager.getGoodsSet()) { if (it.getGoodsId().equals(goodNum)) { goodsInCart.setGoods(it); // 将对应编号的商品,添加到购物车 iDIsexit = true ; break ; } } if (!iDIsexit) { System.out.println( "你输入的商品编号不存在,请重新输入:" ); continue ; } else { while ( true ) { try { System.out.println( "请输入购买数量:" ); shopNum = sc.nextInt(); break ; } catch (InputMismatchException e) { System.out.println( "请输入整数:" ); sc.next(); continue ; } catch (Exception e) { e.printStackTrace(); break ; } } goodsInCart.setNum(shopNum); // 将输入的商品数量,保存到购物车 break ; } } while ( true ); this .getShoppingCart().put(goodNum, goodsInCart); // 将商品编号和对应的购买信息存入当前集合map; // sc.close();此处不能使用close()方法,否则执行此方法后,再次遇到sc.next()会报错,为什么呢?我明明只关闭的是当前方法的Scanner对象呀! System.out.println( "商品添加购物车成功!" ); } //修改购物车中的商品数量 public void editNumInShopping() { String goodsId = "" ; int shoppingNum = 0 ; if ( this .getShoppingCart().isEmpty()) { System.out.println( "当前购物车是空的,请先添加商品到购物车" ); return ; } Scanner sc = new Scanner(System.in); do { this .showGoodsInShooping(); System.out.println( "请输入要修改的商品编号:" ); goodsId = sc.next(); if (! this .shoppingCart.containsKey(goodsId)) { System.out.println( "在购物车中找不到你输入的商品编号,请重新输入:" ); continue ; } else { while ( true ) { try { System.out.println( "请输入新的购买数量" ); shoppingNum = sc.nextInt(); break ; } catch (InputMismatchException e) { System.out.println( "请输入整数:" ); sc.nextLine(); continue ; } catch (Exception e) { e.printStackTrace(); break ; } } if (shoppingNum == 0 ) { System.out.println( "购物车中的" + shoppingCart.get(goodsId).getGoods().getGoodsName() + "已清空" ); shoppingCart.remove(goodsId); } else shoppingCart.get(goodsId).setNum(shoppingNum); break ; } } while ( true ); // sc.close(); } //显示购物车中的商品信息 public void showGoodsInShooping() { System.out.println( "显示购物车中的所有商品信息" ); if ( this .getShoppingCart().isEmpty()) { System.out.println( "还没有商品加入到购物车" ); return ; } Iterator<GoodsInCart> it = shoppingCart.values().iterator(); while (it.hasNext()) { System.out.println(it.next()); } } //结算购物车中的商品总价 public void countPrice() { System.out.println( "结算" ); if ( this .getShoppingCart().isEmpty()) { System.out.println( "还没有商品加入到购物车" ); return ; } int sumPrice = 0 ; //购物车中的商品总价 for (GoodsInCart goodsInCart : shoppingCart.values()) { sumPrice += goodsInCart.getNum() * goodsInCart.getGoods().getPrice(); } System.out.println( "商品的总价为:" + sumPrice); showGoodsInShooping(); // 方法1:通过便利得到key值,再使用HashMap的remove(key)的方法清空该集合,这样在删除多个元素的场景会报错:concurrentmodificationexception /** * for (String key : this.getShoppingCart().keySet()) { * this.getShoppingCart().remove(key); * } **/ //方法二,将HashMao转化为Set集合,再使用Set集合中的removeAll方法可以清空集合 Set<Entry<String, GoodsInCart>> set = this .getShoppingCart().entrySet(); set.removeAll(set); } } //主方法: package com.imooc2.course.Course5_1_Shopping; import java.util.InputMismatchException; import java.util.Scanner; public class Test { //展示主菜单 static void showMasterMeau() { System.out.println( "*******************************" ); System.out.println( " **主菜单**" ); System.out.println( " 1--商品管理" ); System.out.println( " 2--购物车" ); System.out.println( " 0--退出" ); System.out.println( "*******************************" ); System.out.println( "请选择对应数字进行操作:" ); } //展示商品管理菜单 static void showGoodsManagerMeau() { System.out.println( "*******************************" ); System.out.println( " **商品管理**" ); System.out.println( " 1--商品信息导入" ); System.out.println( " 2--显示所有商品信息" ); System.out.println( " 9--返回上一级菜单" ); System.out.println( "*******************************" ); System.out.println( "请选择对应数字对商品进行管理:" ); } //展示购物车菜单 static void showShoppingManagerMeau() { 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( "请输入对应的数字对购物车进行管理:" ); } GoodsManager goodsManager = new GoodsManager(); //实例化商品管理类 ShoppingCart shoppingCart = new ShoppingCart(); //实例化购物车管理类 Scanner sc = new Scanner(System.in); //商品管理相关功能 void goodsManager() { int goodsManagerSelector = 0 ; //存放商品管理菜单输入的指令 do { while ( true ) { try { showGoodsManagerMeau(); goodsManagerSelector = sc.nextInt(); break ; } catch (InputMismatchException e) { System.out.println( "请输入整数:" ); sc.nextLine(); continue ; } catch (Exception e) { e.printStackTrace(); break ; } } if (goodsManagerSelector == 1 ) { goodsManager.importGoods(); } else if (goodsManagerSelector == 2 ) { goodsManager.showGoods(); } else if (goodsManagerSelector == 9 ) { return ; } else { System.out.println( "你输入的指令不存在请重新输入:" ); continue ; } } while ( true ); } //购物车相关功能 void shopManager() { int shoppingManagerSelector = 0 ; //存放购物车菜单相关指令 do { while ( true ) { try { showShoppingManagerMeau(); shoppingManagerSelector = sc.nextInt(); //如果添加商品的方法中又sc.close(),当购物车中添加商品的方法执行后,再次执行到此处会报错:NoSuchElementException break ; } catch (InputMismatchException e) { System.out.println( "请输入整数:" ); sc.nextLine(); continue ; } catch (Exception e) { e.printStackTrace(); break ; } } switch (shoppingManagerSelector) { case 1 : shoppingCart.addGoodsToShoppingCart(goodsManager); break ; case 2 : shoppingCart.editNumInShopping(); break ; case 3 : shoppingCart.showGoodsInShooping(); break ; case 4 : shoppingCart.countPrice(); break ; case 9 : return ; default : System.out.println( "你输入的指令不存在请重新输入:" ); continue ; } } while ( true ); } public static void main(String[] args) { int masterMeauSelector = 0 ; //存放主菜单选择的指令 Test test = new Test(); Scanner sc = new Scanner(System.in); do { while ( true ) { try { showMasterMeau(); masterMeauSelector = sc.nextInt(); break ; } catch (InputMismatchException e) { System.out.println( "请输入整数:" ); sc.nextLine(); continue ; } catch (Exception e) { e.printStackTrace(); break ; } } if (masterMeauSelector == 1 ) { test.goodsManager(); } else if (masterMeauSelector == 2 ) { test.shopManager(); } else if (masterMeauSelector == 0 ) { break ; } else { System.out.println( "没有该指令对应的操作,请重新选择:" ); continue ; } } while ( true ); sc.close(); } } |
=====================================================
各位大侠,通过这个作业,
我一共有三个疑问:
问题1:为什么使用Scanner对象的Close方法后,再次遇到其他Scanner对象的next()方法时程序会报错?
问题2:为了保证输入的数字是整数,我使用了很多的TryCatch语句块,感觉太繁琐了,有没有更加简洁的办法?
问题3:当输入的字符被定义为字符串时,是不是就不会有什么异常,不需要TryCatch语句块去处理了?
关于本题实现,如果有其他建议,希望大家指正~
======================
新手上路,多多关照~
1
收起
正在回答
2回答
1、NoSuchElementException是没有元素可以迭代异常。sc.close();进行关闭处理,会把System.in也关闭了,System.in是InputStream的对象,并且关掉之后不能再打开。所以建议不要关闭Scanner对象
,一般是不关闭Scanner对象的。如果一定要关,建议你在停止整个程序的时候,再关掉。
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧