Hy everyone,
I am programming boggle in Java. I have some problems though. method guess() should be repeated for 3 minutes, when the 3 minutes are over method endGame() should be executed. How do I have to do this? I think i need to use the java.util.Timer but I have no idea how to put it in a working code. Could anybody help plz:)

Greetz.

Stroper

int timeLeft = 180;
Timer countdown = new Timer(1000,new ActionListener(){
timeLeft--;
if(timeLeft > 0){
guess();
}
});

and where to place the endGame() method?:)

int timeLeft = 180;
Timer countdown = new Timer(1000,new ActionListener(){
timeLeft--;
if(timeLeft > 0){
guess();
}else{
endGame();
}
});

I feel guilty for just giving you the code so you can copy and paste it instead of actually learning it...

It still doesn't work though; What's wrong?

package Boggle;

import java.awt.event.ActionListener;
import java.util.Timer;

/**
 * Created by IntelliJ IDEA.
 * To change this template use File | Settings | File Templates.
 */
public class Boggle {
    public void guess(){
        System.out.println("Guess");  
    }
    public void endGame(){
        System.out.println("End game");  
    }

    int timeLeft = 180;
    Timer countdown = new Timer(1000, new ActionListener() {
        timeLeft--;
        if(timeLeft<=0)

        {
            endGame();
        }

        else

        {
            guess();
        }
    });

Sigh, put in the main method/constructor countDown.start(); . This means you are starting the timer. When you want it to stop, use countDown.stop();

Sigh, put in the main method/constructor countDown.start(); . This means you are starting the timer. When you want it to stop, use countDown.stop();

thx for the effort but I still don't understand.

Show me your whole code.

I dont have a whole code..:d I really suck; I've just started. I need method guess() to run for 3 minutes.

public void guess(){
        System.out.println("Guess");

When the 3 minutes are over I want method endGame() to run.

public void endGame(){
        System.out.println("End game");  
    }

For the rest I don't seem to know what to do exactly.

You don't have to help me if you don't want to:d i appreciate the effort though:)

Forget about the timer for the moment. What does the guess() method look like? That'll depend on whether this is running in a command-line or a GUI situation, but they're pretty much the same at bottom: you want to enter a state in which the user enters words and they are packed away into some data structure (an ArrayList would be what I'd use) for later use, until it's told.

Try writing that. In a CLI environment, it might be easiest to wrap your input in a loop and check whether you're finished before entering each input:

do
{
  get input;
  if (not done yet) 
    put input into a data structure 
} while (not done yet)

In a GUI, you can actually tell the input window to dispose() of itself when time's up, so that's a bit easier, but then you're dealing with Swing, which is a pain.

Either way you should put some sort of manual way to send a terminate signal - a simple JButton, if that's not beyond your capabilities, would be ideal - so you can test your terminate method.

Get that stuff in order, and then you can think about what your timer will look like.

You don't have to use Swing, awt.Frame works.
You can use JOptionPane.showInputDialog to get the users input but that's Swing.

If you just want the game in the Output box (no GUI(Graphical User Interface)) then don't bother with what I said about awt.Frame and GUI.

You need a main method.

public static void main(String[] args){

}

You can put stuff like System.out.println("Enter a number"); or something here and put your Timer in here and everything else apart from your Guess() and endGame() methods.

For GUI, you can use JOptionPane.showInputDialog to get your users input.

You could learn everything at http://download.oracle.com/javase/tutorial/index.html
Happy Javaring

I've put the guess() method in a do..while
Now I want to repeat the whileloop for 3 minutes instead of 8 times.
What do I have to do now?:)

public void guess() {
       Scanner scanner = new Scanner(System.in);
        String answered = "test";
        int i = 0;
        do {
            System.out.println("Enter your word.");
            answered = scanner.nextLine();
            CheckWoord cw = new CheckWoord(ingave);
            boolean check1 = cw.checkLength();
            boolean check2 = cw.checkList();
            boolean check3 = cw.checkMatrix();       

            System.out.println(check1);
            System.out.println(check2);
            System.out.println(check3);
            //System.out.println(answered);      

            i++;
        } while (i<8); //gets repeated 8x instead of counting x minutes..
    }

We're not going to tell you how to make a Boggle game you know -.-

A while loop will break when its condition evaluates to false. So either you can have it alter that condition within the loop (as you do here, incrementing a variable until the condition is false) or you can alter it from some other method (by having a method like "endLoop()" which sets a boolean variable to false.

You can use a Timer to set a flag, which would then tell the while loop that it was done, or you could get the system time before you enter the loop, and then check whether elapsed time (current time - start time) has exceeded 180,000 millis - that would be checking within your loop.

No but my question is still the same as it was in the beginning! I just wanted to know how to put my method in a downcounting loop!

We already told you to use a Timer.

We already told you to use a Timer.

I knew that when I asked my question, but that wasn't the question.

I think i need to use the java.util.Timer but I have no idea how to put it in a working code. Could anybody help plz

I posted a link to a tutorial...

commented: True. No homework freebies here. +16
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.