0

I have this code:

import java.util.*; 

public class Vormpjes {

    public static void main(String[] args){
        String vorm;
        String nogmaals;
        double diameter;
        double basis;
        double hoogte;

        Scanner keyboard = new Scanner(System.in);

        do {
            System.out.println("Kies uit de 'cirkel', 'driehoek' of 'vierhoek':");
            vorm = keyboard.nextLine();

            if (vorm.equals("cirkel")){
                System.out.println("Geef een diameter op (in cm)");
                diameter = keyboard.nextDouble();

                cirkel C1 = new cirkel();
                C1.setDiam(diameter);
                System.out.println("De diameter = " + C1.getDiam() + " cm");
                System.out.println("De straal = " + C1.getRadius() + " cm");
                System.out.println("De oppervlakte van de cirkel = " + C1.berekenOpp() + " cm2");
                System.out.println("");
            }

            if (vorm.equals("driehoek")){
                System.out.println("Geef de basis van de driehoek (in cm)");
                basis = keyboard.nextDouble();

                driehoek D1 = new driehoek();
                D1.setBasis(basis);
                System.out.println("Geef de hoogte van de driehoek (in cm)");
                hoogte = keyboard.nextDouble();
                D1.setHoogte(hoogte);
                System.out.println("De oppervlakte = " + D1.berekenOppDriehoek() + " cm2");
                System.out.println("");
            }

            System.out.println("Typ 'J' in als u het programma nogmaals wilt uitvoeren, druk anders op een andere wilekeurige toets.");
            nogmaals = keyboard.nextLine();
        }
        while (nogmaals.equalsIgnoreCase("J"));
    }
}

And the class 'cirkel' (that's how you spell circle in Dutch haha)

public class cirkel extends Vormpjes{

    public double diam;
    public double r;

    //constructor
    public cirkel(){

    }

    public double getDiam(){
        return this.diam;
    }

    public void setDiam(double diam){
        this.diam = diam;
    }

    public double getRadius(){
        r = 0.5 * getDiam();
        return this.r;
    }

    public double berekenOpp(){
        return Math.PI * r * r; 
    }

    public double berekenOmtrek(){
        return 2 * Math.PI * r;
    }

}

When the program asks the user if he wants to run the program again or not, I'm unable to type anything after while condition.

Why is it not working? I don't get errors or anything...

user3024879
  • 267
  • 5
  • 13
  • 1
    What do you mean "unable to type"? Did the program terminate? – Aify Jun 04 '15 at 19:37
  • It's really unclear what's not working here. If you answer `J` or `j` the loop will continue. If you answer something else the program terminates, as expected, right? – aioobe Jun 04 '15 at 19:38
  • @WhyCry Google translate :"Type 'Y' if you want to run the program again, otherwise press another key wilekeurige.". And yes in dutch is "Ja". – J Atkin Jun 04 '15 at 19:42
  • @Aify I can't type anything after the program asks me if I want to continue.. – user3024879 Jun 04 '15 at 19:42
  • @aioobe I can't type anything after the program asks me if I want to continue.. – user3024879 Jun 04 '15 at 19:42
  • 1
    @user3024879, this is because when you've entered `5` and read a double, you still have `` in the to-read buffer. Last in your loop, you read a line (to get `J` or something else), and at this point you'll get the trailing part of the previous input that has not yet been read. The solution is to add a `nextLine()` after each time you've read a double. – aioobe Jun 04 '15 at 19:45
  • 1
    http://stackoverflow.com/questions/7877529/java-string-scanner-input-does-not-wait-for-info-moves-directly-to-next-stateme – theduck Jun 04 '15 at 19:46
  • Alternatively, you could readLine() and parseDouble() instead of reading doubles directly.\ – Aify Jun 04 '15 at 19:48
  • @aioobe can you give me an example in the answer area please? – user3024879 Jun 04 '15 at 19:49

2 Answers2

2

Each time you call nextDouble() the user will enter a double and press enter. This will put a double and a newline in the buffer. The double is consumed by nextDouble(), but the newline character lingers around until you call nextLine().

In your case this becomes a problem, because when you ask the user for a J (or something else) the "old" newline character is consumed before the user manages to enter anything.

In other words, simply change

diameter = keyboard.nextDouble();

to

diameter = keyboard.nextDouble();
keyboard.nextLine();

to consume the newline character that was entered after the double.

aioobe
  • 413,195
  • 112
  • 811
  • 826
1

Use the following code, for reading the J/N value, to continue or stop the program.

nogmaals = keyboard.next();
Verhagen
  • 3,885
  • 26
  • 36