0

I'm encountering an issue in this program that I'm unable to explain. Specifically, when I select options 3, 4, or 5 and complete the operation, the options menu gets printed on the screen twice.

I've attempted to address this by using both if statements and switch case statements, but the problem persists.

This is the Class:

package esercizio4;

public class Automobile {
    
    private String marca;
    private String modello;
    private double benzina;
    private boolean accesa;
    private double velocita;
    
    public Automobile(String marca, String modello) {
        this.marca = marca;
        this.modello = modello;
        setBenzina(1);
        setAccesa(false);
        setVelocita(0);
    }
    
    public String getMarca() {
        return marca;
    }
    
    public void setMarca(String marca) {
        this.marca = marca;
    }
    
    public String getModello() {
        return modello;
    }
    
    public void setModello(String modello) {
        this.modello = modello;
    }
    
    public double getBenzina() {
        return benzina;
    }
    
    public void setBenzina(double benzina) {
        this.benzina = benzina;
    }
    
    public boolean getAccesa() {
        return accesa;
    }
    
    public void setAccesa(boolean accesa) {
        this.accesa = accesa;
    }
    
    public double getVelocita() {
        return velocita;
    }
    
    public void setVelocita(double velocita) {
        this.velocita = velocita;
    }
    
    public void accendi() {
        if(getAccesa() == false && benzina > 0) {
            setAccesa(true);
            System.out.println("Hai acceso la macchina");
        } 
        else if(getAccesa() == true){ 
            System.out.println("La macchina è già accesa");
        }
        else if(getAccesa() == false && benzina == 0) {
            System.out.println("Errore: non c'è benzina");
        }
    }
    
    public void spegni() {
        if(getAccesa() == true) {
            setAccesa(false);
            System.out.println("Hai spento la macchina");
        } else {
            System.out.println("La macchina è già spenta");
        }
    }
    
    public boolean isAccesa() {
        if(getAccesa() == true) {
            System.out.println("La macchina è accesa");
        } else {
            System.out.println("La macchina è spenta");
        }
        return accesa;
    }
    
    public void addBenzina(int benzina) { //Problema: ripropone 2 volte il menu opzioni
        int livMaxSerbatoio = 10;
        if (benzina < livMaxSerbatoio) {
            this.benzina += benzina;
            System.out.println("Hai inserito " + benzina + " litri");
        } 
        else if(benzina > livMaxSerbatoio){
            System.out.println("Non c'è abbastanza spazio");
        } 
    }
    
    public void viewBenzina() {
        if(getBenzina() == 10) {
            System.out.println("Il serbatoio è pieno: 10 litri presenti");
        } else {
            System.out.println("Il livello di benzina è: " + getBenzina() + " litri");
        }
    }
    //DA MIGLIORARE
    public void accelera(int kmh) { //Problema: ripropone 2 volte il menu opzioni
        if(velocita < 200 && accesa == true) {
            setVelocita(getVelocita() + kmh);
            System.out.println("Hai accelerato di " + kmh + "kmh");
            System.out.println("Stai andando a " + getVelocita() + "kmh");
        }
        if(accesa == false) {
            System.out.println("Errore: la macchina è spenta");
        }
        else if(velocita > 200 && accesa == true) {
            System.out.println("Errore: non puoi andare a piu di 200 kmh");
        }
    }
    //DA MIGLIORARE
    public void decelera(int kmh) { //Problema: ripropone 2 volte il menu opzioni
        if(velocita > 0 && accesa == true) {
            setVelocita(getVelocita() - kmh);
            System.out.println("Hai decelerato di " + kmh + "kmh");
            System.out.println("Stai andando a " + getVelocita() + "kmh");
        }
        if(accesa == false) {
            System.out.println("Errore: la macchina è spenta");
        }
        else if(kmh > getVelocita()) {
            setVelocita(0);
        }
    }
    
    public void ferma() {
        if(velocita > 0 && accesa == true) {    
            for(int i = (int) getVelocita(); i >= 0; i--) {
                setVelocita(i);
                //System.out.println("Stai andando a " + getVelocita() + " kmh");               
            }
            System.out.println("Ti sei fermato");
        } else {    
            System.out.println("Sei già fermo");
        }
    }
    
    public boolean isInMoto() {
        if(getVelocita() > 0) {
            System.out.println("La macchina sta andando a " + getVelocita() + " kmh");
            return true;
        } else {
            System.out.println("La macchina è ferma");
        }       
        return false;
    }
    
    public String toString() {
        String toString = "Informazioni:" + "\n" +
                          "Marca: " + getMarca() + "\n" +
                          "Modello: " + getModello() + "\n" +
                          "Livello serbatoio: " + getBenzina() + " litri \n" +
                          "Stato accensione: " + getAccesa() + "\n" +
                          "Velocità: " + getVelocita() + " kmh";        
        return toString;
    }
    
    public static void stampaOpzioni() {
        System.out.println("Premi 1 per accendere la macchina");
        System.out.println("Premi 2 per spegnere la macchina");
        System.out.println("Premi 3 per aggiungere la benzina");
        System.out.println("Premi 4 per accelerare");
        System.out.println("Premi 5 per decelerare");
        System.out.println("Premi 6 per fermarti");
        System.out.println("Premi 7 per visualizzare lo stato attuale");
        System.out.println("Premi EXIT per terminare la simulazione");
    }

}

This is the ClassTest:

package esercizio4;

import java.util.Scanner;

public class TestAutomobile {

    public static void main(String[] args) {
        
        System.out.println("Benvenuto in questa sessione di simulazione di guida");
        System.out.println("Crea la tua macchina");
        
        Scanner input = new Scanner(System.in);
        
        System.out.println("Inserisci la marca");
        String marca = input.nextLine();
        
        System.out.println("Inserisci il modello");
        String modello = input.nextLine();
        
        Automobile auto = new Automobile(marca, modello);
        
        String opzione = "";
        while(!opzione.equalsIgnoreCase("exit")) {

            Automobile.stampaOpzioni();         
            
            opzione = input.nextLine();
            
            switch(opzione) {
                case "1":
                    auto.accendi();
                    break;
                case "2":
                    auto.spegni();
                    break;
                case "3":
                    System.out.println("Quanta benzina vuoi aggiungere? (capienza max 10 litri)");
                    int benzina = input.nextInt();
                    auto.addBenzina(benzina);
                    break;
                case "4":
                    System.out.println("Di quanto vuoi accelerare? (velocità max 200 kmh)");
                    int kmh = input.nextInt();
                    auto.accelera(kmh);
                    break;
                case "5":
                    System.out.println("Di quanto vuoi decelerare?");
                     kmh = input.nextInt();
                    auto.decelera(kmh);
                    break;
                case "6":
                    auto.ferma();
                    break;
                case "7":
                    System.out.println(auto);
                    break;
            }
        }//Chiusura while
        
        input.close();
        System.out.println("Hai terminato la simulazione");
        System.out.println("Arrivederci");
        
        
    }

}

This is what it appears to me:

Benvenuto in questa sessione di simulazione di guida Crea la tua macchina
Inserisci la marca
Fiat
Inserisci il modello
Punto
Premi 1 per accendere la macchina
Premi 2 per spegnere la macchina
Premi 3 per aggiungere la benzina
Premi 4 per accelerare
Premi 5 per decelerare
Premi 6 per fermarti
Premi 7 per visualizzare lo stato attuale
Premi 3 EXIT per terminare la simulazione
Quanta benzina vuoi aggiungere? (capienza max 10 litri)
Hai inserito 8 litri
Premi 1 per accendere la macchina
Premi 2 per spegnere la macchina
Premi 3 per aggiungere la benzina
Premi 4 per accelerare
Premi 5 per decelerare
Premi 6 per fermarti
Premi 7 per visualizzare lo stato attuale
Premi EXIT per terminare la simulazione
Premi 1 per accendere la macchina
Premi 2 per spegnere la macchina
Premi 3 per aggiungere la benzina
Premi 4 per accelerare
Premi 5 per decelerare
Premi 6 per fermarti
Premi 7 per visualizzare lo stato attuale
Premi EXIT per terminare la simulazione

not_a_nerd
  • 39
  • 1
  • 7
  • 1
    To clarify why your question is a duplicate to the linked one: in option 3-4-5 you use `input.nextInt();` which will result in the next `opzione = input.nextLine();` to only read in the carriage return from your previous input. For more information and how to fix this problem see the linked stackoverflow question. – OH GOD SPIDERS Aug 31 '23 at 13:49
  • Yes, I solved it, Thank you very much; – Bartolomeo Cenisio Aug 31 '23 at 14:51

1 Answers1

0

Based on the code you provided, I noticed that after executing the Automobile.stampaOpzioni() method, you use the nextInt() method to read user input. Note that the nextInt() method will only read an integer, but after reading the integer, line breaks may remain in the input stream. This means that the next time input.nextLine() is called, it reads the newline character entered earlier without waiting for the user to enter a new string.