-1

Error:

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

Code:

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

    }

}
djm.im
  • 3,295
  • 4
  • 30
  • 45
nimfeld
  • 1
  • 1
  • Do you have a question? – darrengorman Apr 14 '14 at 22:20
  • J'utilise un traducteur pour cela, donc s'il vous plaît parler couramment. S'il vous plaît nous montrer d'où l'erreur est levée. (Il dit la ligne 76, mais je ne sais pas laquelle est la ligne 76). – Vince Apr 14 '14 at 22:23
  • 1
    Did you bother to google the error message first? If you had, the top result is http://stackoverflow.com/questions/7209110/java-util-nosuchelementexception-no-line-found – Mike B Apr 14 '14 at 22:26
  • @MikeB When I call `scanner.nextLine()` in a `true` loop, it blocks until I type something (while using System's InputStream for input). I don't get the `NoSuchElementException`. Why not? – Vince Apr 14 '14 at 22:29

2 Answers2

2

You have to check the hasNextLine() method, to check if there is one, before calling nextLine()

ced-b
  • 3,957
  • 1
  • 27
  • 39
0

while reading with java.util.Scanner you should always check in while loop if "there is anything to read" . You do that using while(myScanner.hasNextLine()) before calling nextLine() method. Also you should close your scanner object after you are done working with it myScanner.close()

Ziker
  • 877
  • 2
  • 10
  • 30