同时使用Java类扫描程序

时间:2014-04-14 22:18:15

标签: java class java.util.scanner

错误:

java.util.NoSuchElementException: No line found     
at java.util.Scanner.nextLine(Unknown Source) 
at PaqueteFacturaciones.Principal.main(Principal.java:76)

代码:

public class Principal {
    public static void main(String[] args) {
        int opcion;
        String apeyNom;
        String cuilCuit;

        Scanner input = new Scanner(System.in);
        System.out
                .println("Que operacion desea realizar...?\n1-Factura\n2-Orden de compra\n0-Salir");
        opcion = Integer.parseInt(input.nextLine());

        try {
            while (opcion != 0) {
                if (opcion != 0) {
                    System.out.println("Ingrese Apellido y Nombre");
                    apeyNom = input.nextLine();
                    System.out.println("Ingrese CUIL/CUIT");
                    cuilCuit = input.nextLine();
                    Persona p = new Persona(apeyNom, cuilCuit);

                    switch (opcion) {
                    case 1: {
                        System.out.println("Ingrese numero de cliente");
                        long nroCliente = Long.parseLong(input.nextLine());
                        // long nroCliente=input.nextLong();
                        long nroFactura = 1;
                        Cliente clien = new Cliente(p.getApeyNom(),
                                p.getCuilCuit(), nroCliente);
                        System.out.println("Ingrese Cantidad de articulos");
                        int cantDeItems = Integer.parseInt(input.nextLine());
                        // int cantDeItems=input.nextInt();
                        Factura fact = new Factura(clien, nroFactura,
                                cantDeItems);
                        fact.ingresaItems();
                        System.out.println(fact);
                        fact.imprimeItems();
                    }
                    case 2: {
                        /*
                         * System.out.println("Ingrese numero de proveedor");
                         * long nroProvee=Long.parseLong(input.nextLine()); long
                         * nroOrden=1; Proveedor pr=new
                         * Proveedor(p.getApeyNom(),p.getCuilCuit(),nroProvee);
                         * System.out.println("Ingrese Cantidad de articulos");
                         * int cantDeItems=Integer.parseInt(input.nextLine());
                         * OrdenDeCompra orden=new
                         * OrdenDeCompra(pr,nroOrden,cantDeItems);
                         * orden.ingresaItems(); orden.imprimeItems();
                         */

                    }
                    }
                }
                System.out
                        .println("Que operacion desea realizar...?\n1-Factura\n2-Orden de compra\n0-Salir");
                opcion = Integer.parseInt(input.nextLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

2 个答案:

答案 0 :(得分:2)

在致电hasNextLine()

之前,您必须先检查nextLine()方法,检查是否有方法。

答案 1 :(得分:0)

在使用java.util.Scanner阅读时,如果“有任何内容需要阅读”,则应始终检查while循环。您在调用while(myScanner.hasNextLine())方法之前使用nextLine()执行此操作。 此外,您应该在使用完{1}}

后关闭扫描仪对象