Java多个扫描仪 [英] Java Multiple Scanners

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

问题描述

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

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;
    }
}

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

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();
        }
    }
}

应用程序应该允许用户输入添加次数,但在调用 add 方法后出现错误找不到行。

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.

我猜这是因为方法中的 Scanner 尚未关闭,然后在需要时重新打开。这是程序有什么问题,如果是这样,我该如何解决它?

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?

请注意,这个程序还没有完成,因为我将添加一个出售这些股票的卖出方法。这就是我使用while循环的原因。

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

关闭流后,它会关闭基础流,您无法再次使用它。如果你想阻止它再次使用,只关闭System.in。

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?

最佳解决方案是让所有扫描仪在一个地方,一个方法或一个类中使用。您让main()与用户进行所有交互,并将值传递给数据结构。拥有自我初始化的对象是一个不好的做法,如果你开始这样做,它将困扰你的余下你的开发日;)(严重的是你会看到这一次又一次完成,它往往是一场噩梦)

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切勿在没有说明的情况下退出程序。调用 System.exit(0); 甚至没有错误消息也是一场噩梦。我曾经做过一个项目,它有260个调用System.exit(),通常没有错误信息,你可以想象诊断服务器只是停止没有明显原因是多么有趣。

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天全站免登陆