Java 多重扫描器 [英] Java Multiple Scanners

查看:31
本文介绍了Java 多重扫描器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建多个 Integer 对象并将它们放入一个 LinkedList 的类,如下所示:

public class Shares实现 Queue<E>{受保护的 LinkedList升;公开股(){L = 新的 LinkedList();}公共布尔添加(E价格){System.out.println("你想要多少份?");扫描仪 scanInt;scanInt = new Scanner(System.in);整数 noShares = scanInt.nextInt();for (int i = 0; i < noShares; i++) {L.addLast(价格);}scanInt.close();返回真;}}

我有一个应用程序,它从控制台扫描输入add",如果找到,则调用方法 add ,如下所示:

公共类应用{私人静态扫描仪扫描;公共静态<E>无效主(字符串[]参数){队列<整数>S = new Shares();scan = new Scanner(System.in);System.out.println("请输入add");字符串句子 = scan.nextLine();while (sentence.equals("quit") == false) {if (sentence.equals("add")) {系统输出.println("您希望以什么价格购买您的股票?");S.add((Integer) scan.nextInt());} 别的System.exit(0);句子 = scan.nextLine();}}}

应用程序应该允许用户根据需要多次输入add",但是在调用 add 方法后会出现错误no line found".

我猜这是因为方法中的 Scanner 尚未关闭,然后在需要时重新打开.这是程序的问题吗?如果是,我将如何修复它?

请注意,该程序尚未完成,因为我将添加一种出售这些股票的出售方法.这就是我使用 while 循环的原因.

解决方案

为任何流使用多个包装器是一种真正混淆自己的好方法.我建议你只包装一次流,除非你真的知道你在做什么.

最简单的方法是在这种情况下使用单例,因为它包装了另一个单例(最好是将 Scanner 作为参数传递)

公共类应用{//在所有其他代码中使用此 Scanner,不要创建另一个.static final Scanner scan = new Scanner(System.in);公共静态<E>无效主(字符串[]参数){

<块引用>

我猜这是因为方法中的scanner还没有关闭

一旦您关闭一个流,它就会关闭底层流并且您不能再次使用它.如果您想防止再次使用它,请仅关闭 System.in.

<块引用>

我将如何修复它?

最好的解决方案是在一个地方、一种方法或一个类中使用所有扫描仪.您让 main() 完成与用户的所有交互并将值传递给您的数据结构.拥有初始化自己的对象是一种不好的做法,如果你开始这样做,它会在你剩下的开发日中困扰你;)(说真的,你会一次又一次地看到这种情况,这通常是一场噩梦)<小时>

顺便说一句,永远不要在没有解释的情况下退出程序.调用 System.exit(0); 甚至没有错误消息也是一场噩梦.我曾经参与过一个项目,该项目对 System.exit() 进行了 260 次调用,并且经常没有错误消息,您可以想象诊断服务器无缘无故停止是多么有趣.

I have a class that creates multiple Integer objects and puts them into a LinkedList as shown below:

public class Shares<E> implements Queue<E> {
    protected LinkedList<E> L;

    public Shares() {
        L = new LinkedList<E>();
    }

    public boolean add(E price) {
        System.out.println("How many of these shares would you like?");
        Scanner scanInt;
        scanInt = new Scanner(System.in);
        Integer noShares = scanInt.nextInt();
        for (int i = 0; i < noShares; i++) {
            L.addLast(price);
        }
        scanInt.close();

        return true;
    }
}

I have an application that scans for the input "add" from the console and if found, invokes the method add as shown below:

public class Application {
    private static Scanner scan;

    public static <E> void main(String[] args) {
        Queue<Integer> S = new Shares<Integer>();
        scan = new Scanner(System.in);
        System.out.println("Please type add");
        String sentence = scan.nextLine();
        while (sentence.equals("quit") == false) {
            if (sentence.equals("add")) {

                System.out
                    .println("What price would you like to buy your shares at?");

                S.add((Integer) scan.nextInt());

            } else
                System.exit(0);

            sentence = scan.nextLine();
        }
    }
}

The application should allow the user to enter "add" as many times as they wish but the error "no line found" appears after the add method has been invoked.

I'm guessing this is because the Scanner in the method, has not been closed and then reopened when needed. Is this what is wrong with the program and if so, how would I go about fixing it?

Please note, this program is not finished, as I will be adding a selling method that sells these shares. That is why I am using a while loop.

解决方案

Having multiple wrappers for any stream is a great way to really confuse yourself. I suggest you only ever wrap a stream once unless you really know what you are doing.

The simplest way to do this is to use a singleton in this case as it wraps another singleton (the best is to pass around the Scanner as an argument)

public class Application { 
    // use this Scanner in all you other code, don't create another one.
    static final Scanner scan = new Scanner(System.in);

    public static <E> void main(String[] args) {

Im guessing this is because the scanner in the method has not been closed

Once you close a stream it closes the underlying stream and you can't use it again. Only close System.in if you want to prevent it being used again.

how would I go about fixing it?

The best solution is to have all your Scanner use in one place, one method or one class. You have your main() do all the interaction with the user and pass the values to your data structure. Having objects which initialise themselves is a bad practice to get into and if you start doing this, it will plague you for the rest of your development days ;) (Seriously you will see this done again and again and its often a nightmare)


BTW Never exit a program without explanation. Calling System.exit(0); without even an error message is also a nightmare. I once worked on a project which has 260 calls to System.exit() often without an error message, you can imagine how much fun it is to diagnose a server just stopping for no apparent reason.

这篇关于Java 多重扫描器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆