不不爱写博客 2021-09-25 22:04 采纳率: 100%
浏览 163
已结题

nextInt()报错NoSuchElementException

自己用Java写了一个顺序表的实现,但总是在while循环的第二次执行到nextInt()时报错NoSuchElementException,我是在循环结束之后关闭流,请问该如何修改,代码如下

package com.数据结构;
 
import java.util.Scanner;
 
public class SeqList {
    private int[] date = new int[10];
    private int Lenght;
    private Integer x;
 
    public SeqList() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    public Integer getX() {
        return x;
    }
 
    public void setX(Integer x) {
        this.x = x;
    }
 
    public int[] getDate() {
        return date;
    }
 
    public void setDate(int[] date) {
        this.date = date;
    }
 
    public int getLenght() {
        return Lenght;
    }
 
    public void setLenght(int lenght) {
        Lenght = lenght;
    }
 
    /* 初始化顺序表 */
    public void InitList() {
        this.setLenght(0);
    }
 
    /* 建立顺序表 */
    public void createList(int n) {
        System.out.print("请输入" + n + "个整数:");
        int[] date = new int[n];
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < n; i++) {
            date[i] = input.nextInt();
        }
        input.close();
        this.setDate(date);
        this.setLenght(n);
    }
 
    /* 查找操作 */
    public int getElem(int i, int x) {
        if (i < 1 || i > this.getLenght()) {
            System.out.println("查找位置出错");
            return 0;
        } else {
            x = this.getDate()[i - 1];
            return 1;
        }
    }
 
    /* 查找下标 */
    public int locate(int x) {
        for (int i = 0; i < this.getLenght(); i++) {
            if (this.getDate()[i] == x) {
                return i - 1;
            } else {
                System.out.println("查无此元素");
                return 0;
            }
        }
        return 0;
    }
 
    /* 插入元素 */
    public int insElem(int i, int x) {
        if (this.getLenght() >= 10) {
            System.out.println("顺序表满,插入失败");
            return 0;
        }
        if (i - 1 >= this.getLenght()) {
            System.out.println("插入位置出错");
            return 0;
        }
        if (i - 1 == this.getLenght()) {
            this.getDate()[i - 1] = x;
            System.out.println("在表尾插入");
            return 1;
        }
        for (int j = this.getLenght(); j > i - 1; j--) {
            this.getDate()[j - 1] = this.getDate()[j];
        }
        this.getDate()[i - 1] = x;
        this.setLenght(this.getLenght() + 1);
        System.out.println("表中插入成功");
        return 1;
    }
 
    /* 删除元素 */
    public int delElem(int i, Integer x) {
        if (this.getLenght() == 0) {
            System.out.println("表为空,无法删除");
            return 0;
        }
        if (i < 1 || i - 1 > this.getLenght()) {
            System.out.println("删除位置出错");
            return 0;
        }
        x = this.getDate()[i - 1];
        for (int j = i; j <= this.getLenght(); j++) {
            this.getDate()[j - 1] = this.getDate()[j];
        }
        this.setLenght(this.getLenght() - 1);
        System.out.println("删除成功");
        return 1;
    }
 
    /* 输出元素 */
    public void dispList() {
        for (int x : this.getDate()) {
            System.out.print(x + " ");
        }
        System.out.println();
    }
 
    public static void main(String[] args) {
        SeqList sl = new SeqList();
        boolean ex = true;
        int number = 0;
        Scanner input = new Scanner(System.in);
        while (ex) {
            System.out.println("======================================================");
            System.out.println("                 1------建立顺序表                       ");
            System.out.println("                 2------插入元素                        ");
            System.out.println("                 3------删除元素                        ");
            System.out.println("                 4------按位置查找元素                    ");
            System.out.println("                 5------按值查找元素位置                  ");
            System.out.println("                 6------顺序表长度                       ");
            System.out.println("                 0------退出                           ");
            System.out.println("======================================================");
            System.out.print("输入菜单号:");
//==============代码在这里报错====================
            number = input.nextInt();
//===============================================
            switch (number) {
            case 1:
                System.out.print("线性表成员个数:");
                sl.createList(input.nextInt());
                System.out.print("创建的顺序表为:");
                sl.dispList();
                break;
            case 2:
                System.out.print("请输入插入位置和需要插入的元素:");
                sl.insElem(input.nextInt(), input.nextInt());
                System.out.println("\n插入后的表为:");
                sl.dispList();
                break;
            case 3:
                System.out.print("请输入删除位置:");
                sl.delElem(input.nextInt(), sl.getX());
                System.out.println("\n删除后的表为:");
                sl.dispList();
                break;
            case 4:
                System.out.print("请输入位置:");
                int i = input.nextInt();
                sl.getElem(i, sl.getX());
                System.out.println("\n在第" + i + "位查找到的元素为:" + sl.getX());
                break;
            case 5:
                System.out.print("请输入需要查找的元素:");
                sl.setX(input.nextInt());
                System.out.println("\n该元素下标为:" + sl.locate(sl.getX()));
                break;
            case 6:
                System.out.println("该顺序表长度为:" + sl.getLenght());
                break;
            case 0:
                ex = false;
                break;
            default:
                System.out.print("菜单号输入有误,请重新输入:");
            }
            /*
             * if(number != 0) { System.out.println("\n按任意键返回菜单,输入1退出");
             * 
             * }
             */
        }
        input.close();
    }
 
}

  • 写回答

2条回答 默认 最新

  • 偷窃月亮的贼 2021-09-26 10:00
    关注

    注释掉52行的 input.close(); ,可以去看下源码,里面对 System.in 变量的定义:

        /**
         * Default input stream
         */
        public static final InputStream in = null;
    

    所以 in 全局只有一个,当你在新建顺序表后调用了 close() 方法:

        /**
         * Closes this scanner.
         *
         * <p> If this scanner has not yet been closed then if its underlying
         * {@linkplain java.lang.Readable readable} also implements the {@link
         * java.io.Closeable} interface then the readable's {@code close} method
         * will be invoked.  If this scanner is already closed then invoking this
         * method will have no effect.
         *
         * <p>Attempting to perform search operations after a scanner has
         * been closed will result in an {@link IllegalStateException}.
         *
         */
        public void close() {
            if (closed)
                return;
            if (source instanceof Closeable) {
                try {
                    ((Closeable)source).close();
                } catch (IOException ioe) {
                    lastException = ioe;
                }
            }
            sourceClosed = true;
            source = null;
            closed = true;
        }
    

    可以看出调用 close()sourceClosedtrue ,然后抛出异常。
    sourceClosedScanner 类中的变量,定义如下:

        // Boolean is true if source is done
        private boolean sourceClosed = false;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月4日
  • 已采纳回答 9月26日
  • 创建了问题 9月25日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度