ParseException error

Asked

Viewed 248 times

-1

I have a problem about DATE. This giving error on line 58 about: Exception in thread "main" java.text.Parseexception: Unparseable date: "" at java.text.Dateformat.parse(Dateformat.java:366) exsecao14a.Exsecao14a.main(Exsecao14a.java:58) C: Users pedro Appdata Local Netbeans Cache 8.2rc executor-snippets run.xml:53: Java returned: 1 CONSTRUCTION FAILURE (total time: 14 seconds)

public static void main(String[] args) throws ParseException {
    
    Scanner dados = new Scanner(System.in);

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

    List<Product> pdc = new ArrayList<>();

    System.out.print("Entre com o numero de produto: ");
    int numeroP = dados.nextInt();

    for (int i = 1; i <= numeroP; i++) {
        System.out.println("Produto #" + i + " Data:");
        System.out.print("Comum, usado ou importado (c/u/i)? ");
        char cui = dados.next().charAt(0);

        System.out.print("Nome: ");
        dados.next();
        String nome = dados.nextLine();

        System.out.print("Preco: ");
        double preco = dados.nextDouble();

        if (cui == 'i') {
            System.out.println("Taxa alfandega: ");
            double taxa = dados.nextDouble();
            pdc.add(new ImportedProduct(taxa, nome, preco));

        } else if (cui == 'u') {
            System.out.print("Data de fabricação: dd/mm/yyyy: ");
            dados.next();
            String formatoNormal = dados.nextLine();
            Date formato = sdf.parse(formatoNormal); // ESSA É A LINHA 58

            pdc.add(new UsedProduct(formato, nome, preco));

        } else if (cui == 'c') {
            pdc.add(new Product(nome, preco));
        }

    }

}
  • What happens if you remove the line "data.next();", two lines before line 58?

  • data Exception in thread "main" java.text.Parseexception: Unparseable date: "" Manufacturing date: dd/mm/yyyy: at java.text.Dateformat.parse(Dateformat.java:366) at exsecao14a.main(Exsecao14a.java:58) @Joãovictorsierra

  • It "skips" the line where you scan the formatNormal right? what I imagine is that it is consuming a enter that has been hung in some of the other readings of the scanner, the string will be empty and consequently an empty string when trying to be parsed from Exception

  • System.out.print("Manufacturing date: dd/mm/yyyy: "); Date format = sdf.parse(data.next());

1 answer

1

The problem is basically what is described here: when mixing calls from nextLine and next with nextInt and nextDouble can cause some unexpected problems. This is because methods that read numbers, such as nextInt and nextDouble, do not consume line break (which corresponds to the ENTER that the user types). So if you call nextLine right after a nextInt or nextDouble, it consumes the line break and the result is an empty string.

That’s what the exception is complaining about, that you passed an empty string to parse.

One solution is to force the Scanner to consume the line break, calling nextLine right after reading the number (and removing calls to next):

int numeroP = dados.nextInt();
dados.nextLine(); // <-------- aqui

for (int i = 1; i <= numeroP; i++) {
    System.out.println("Produto #" + i + " Data:");
    System.out.print("Comum, usado ou importado (c/u/i)? ");
    char cui = dados.nextLine().charAt(0);

    System.out.print("Nome: ");
    String nome = dados.nextLine();

    System.out.print("Preco: ");
    double preco = dados.nextDouble();
    dados.nextLine(); // <-------- aqui

    if (cui == 'i') {
        System.out.println("Taxa alfandega: ");
        double taxa = dados.nextDouble();
        dados.nextLine(); // <-------- aqui
        etc...
    } else if (cui == 'u') {
        System.out.print("Data de fabricação: dd/mm/yyyy: ");
        String formatoNormal = dados.nextLine();
        Date formato = sdf.parse(formatoNormal); // agora vai funcionar
etc...

Another alternative is to read the line as string and then convert to number (so the line break is consumed and you do not need to call nextLine afterward):

System.out.print("Entre com o numero de produto: ");
int numeroP = Integer.parseInt(dados.nextLine()); // <-------- aqui

for (int i = 1; i <= numeroP; i++) {
    System.out.println("Produto #" + i + " Data:");
    System.out.print("Comum, usado ou importado (c/u/i)? ");
    char cui = dados.nextLine().charAt(0);

    System.out.print("Nome: ");
    String nome = dados.nextLine();

    System.out.print("Preco: ");
    double preco = Double.parseDouble(dados.nextLine()); // <-------- aqui

    if (cui == 'i') {
        System.out.println("Taxa alfandega: ");
        double taxa = Double.parseDouble(dados.nextLine()); // <-------- aqui
        etc...
    } else if (cui == 'u') {
        System.out.print("Data de fabricação: dd/mm/yyyy: ");
        String formatoNormal = dados.nextLine();
        Date formato = sdf.parse(formatoNormal);
etc...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.