Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Exception in thread “main” java.util.InputMismatchException

–1 vote
asked Mar 8, 2021 by Juliana Eleutério (110 points)

Main Project

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) {
        
        int op;
        
        Scanner ler = new Scanner (System.in);
        
        Canetas pen1 = new Canetas();
        System.out.println("Digite o modelo, cor, ponta e carga da primeira caneta: ");
        pen1.modelo = ler.nextLine();
        pen1.cor = ler.nextLine();
        pen1.ponta = ler.nextDouble();
        pen1.carga = ler.nextInt();
        pen1.tampar();
        pen1.rabiscar();
        pen1.status();
        
        Canetas pen2 = new Canetas();
        System.out.println("Digite o modelo, cor, ponta e carga da segunda caneta: ");
        pen2.modelo = ler.nextLine();
        pen2.cor = ler.nextLine();
        pen2.ponta = ler.nextDouble();
        pen2.carga = ler.nextInt();
        pen2.destampar();
        pen2.rabiscar();
        pen2.status();
    }
}

My Class

public class Canetas{

    String modelo;
    String cor;
    double ponta;
    int carga;
    boolean tampada;

    void tampar(){
        this.tampada = true;

    }

    void destampar(){
        this.tampada = false;
    }

    void rabiscar(){
        if (tampada== false){
            System.out.println("A caneta rabiscará...");
        }else{
            System.out.println("Erro!! A caneta não rabiscará...");
        }
        if (carga >= 10){
           System.out.println("A caneta rabiscará..."); 
        }else{
            System.out.println("Erro!! A caneta não rabiscará...");
        }
    }
    void status(){
     System.out.println("_");
     System.out.println("modelo = " + this.modelo );
     System.out.println("cor = " + this.cor );
     System.out.println("ponta= " + this.ponta );
     System.out.println("carga = " + this.carga );
     System.out.println("tampada = " + this.tampada );

    }
}

What did I do wrong I my code?

1 Answer

+1 vote
answered Mar 15, 2021 by Peter Minarik (86,720 points)
It would be a lot more easier if you'd write your code in English, not Portuguese. Others would understand your code and what you're trying to do.

Please, have a look at https://docs.oracle.com/javase/7/docs/api/java/util/InputMismatchException.html

It says your input is wrong. E.g. you may enter a float but your expecting an int. Or you enter a string and a number was expected.

I'd suggest reviewing your input lines of codes.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...