jsbintask22
2016-10-13 02:45
采纳率: 100%
浏览 8.1k
已采纳

Scanner中异常: java.util.NoSuchElementException?

public void RemoveTest() {
Scanner input = new Scanner(System.in);
System.out.println("Please input Id wanted to delete:");
String idString=null;
while (true) {
idString = input.next();
Student student = map.get(idString);
if (student == null) {
System.out.print("Not exists this student,Please input again:");
continue;
} else {
map.remove(idString);
break;
}
}
input.close();
}

这个方法里面  idString = input.next();一直出现上述错误,Student是我写的一个类,没问题
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

9条回答 默认 最新

  • 默默前行的蜗牛 2016-10-13 03:08
    已采纳

    你在控制台输入id了吗?我测试了一下
    String idString=null;
    idString = input.next();
    System.out.println(idString);
    是没问题的,要不就是Scaner引入的包不是java.util.Scanner

    点赞 打赏 评论
  • blownewbee 2016-10-13 02:51

    idString = input.nextLine();

    点赞 打赏 评论
  • blownewbee 2016-10-13 02:52

    while (true)
    ->
    while (input.hasNext())

    点赞 打赏 评论
  • jsbintask22 2016-10-13 02:57

    改了nexLine()还是会出现Exception in thread "main" java.util.NoSuchElementException: No line found ,感觉就是没接收到字符串啊

    点赞 打赏 评论
  • blownewbee 2016-10-13 03:03

    map.remove(idString);
    你的map里有idString么

    点赞 打赏 评论
  • 小草mlc 2016-10-13 03:13

    运行了你的代码,没有报错,正常运行的

    点赞 打赏 评论
  • jsbintask22 2016-10-13 03:21

    我百度了一下,感觉我这句代码确实没问题,是不是我上面的方法里面也有nexLine 这样的方法 ,影响下面的输入,我还是把代码都贴出来吧:
    package com.jay.List_test;

    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.Map.Entry;

    import java.util.Scanner;
    import java.util.Set;

    public class Map_test {

    private Map<String, Student> map;
    
    public Map_test() {
        this.map = new HashMap<String, Student>();
    }
    
    public void Puttest() {
        Scanner input = new Scanner(System.in);
        String studentId;
        int i = 0;
        while (i < 3) {
            System.out.print("Please input Student'Id:");
            studentId = input.next();
            // Student student = map.get(studentId);
            if (map.get(studentId) == null) {
                System.out.print("Please input student's name:");
                String name = input.next();
    
                Student teStudent = new Student(studentId, name);
                map.put(studentId, teStudent);
                System.out.println("Successful!");
                // input.nextLine();
            } else {
                System.out.println("The StudentId is exists!");
                continue;
            }
            i++;
        }
        input.close();
    }
    
    public void KeySettest() {
        Set<String> set = new HashSet<>();
        set = map.keySet(); // keyset方法返回一个关键字key的集合; 而entryset返回的是一个键值对的集合;
        System.out.println("The total student numbers is:" + set.size());
        for (String string : set) {
            Student student = map.get(string);
            if (student != null) {
                System.out.println("The Student is:" + student.getId() + "  " + student.getName());
            }
    
        }
    }
    
    // 删除map中的元素,测试remove
    public void RemoveTest() {
        Scanner input = new Scanner(System.in);
        System.out.println("Please input Id wanted to delete:");
        String idString=null;
        while (true) {
            idString = input.nextLine();
            Student student = map.get(idString);
            if (student == null) {
                System.out.print("Not exists this student,Please input again:");
                continue;
            } else {
                map.remove(idString);
                break;
            }
        }
        input.close();
    }
    
    // 通过entryset方式遍历map
    public void EntrySettest() {
        Set<Entry<String, Student>> entries = map.entrySet();
        for (Entry<String, Student> entry : entries) {
            System.out.println("The student id:" + entry.getKey());
            System.out.println("The student name:" + entry.getValue().getName());
        }
    }
    
    public static void main(String[] args) {
        Map_test mTest = new Map_test();
        mTest.Puttest();
        mTest.KeySettest();
        mTest.RemoveTest();
        mTest.EntrySettest();
    
    }
    

    }

    Student类如下: package com.jay.List_test;

    import java.util.HashSet;
    import java.util.Set;

    public class Student {

    private String name;
    private String id;
    private Set<Course> set;
    
    public Student(String id,String name) {
        this.name=name;
        this.id=id;
        set=new HashSet<>();
    
    }
    
    public String getName() {
        return name;
    }
    
    public String getId() {
        return id;
    }
    public Set<Course> getSet() {
        return set;
    }
    
    public void TestForeach(){
        for (Course course : set) {
            System.out.println("Course:"+course.getId()+"  "+course.getName());
    
        }
    }
    

    }

    异常: Please input Student'Id:1
    Please input student's name:jay
    Successful!
    Please input Student'Id:2
    Please input student's name:tom
    Successful!
    Please input Student'Id:3
    Please input student's name:jack
    Successful!
    The total student numbers is:3
    The Student is:1 jay
    The Student is:2 tom
    The Student is:3 jack
    Please input Id wanted to delete:
    Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1540)
    at com.jay.List_test.Map_test.RemoveTest(Map_test.java:63)
    at com.jay.List_test.Map_test.main(Map_test.java:89)

    点赞 打赏 评论
  • Gavin_Lim 2021-06-24 15:32

    IO流关闭的位置不对,你把所有的input.close()去掉然后执行就不会报错,然后在去考虑在哪里关闭IO流合适

    点赞 打赏 评论
  • 花草澍沐 2019-02-14 17:27

    Scanner input = new Scanner(System.in) 中Scanner打开的是标准流, 一旦执行关闭在下次循环中将无法打开,建议使用全局配置,最后在main方法中一次性关闭

    点赞 打赏 评论

相关推荐 更多相似问题