This is my first time making a GUI and we're making a stopwatch...I figured everything out except for how to make the time elapsed have one decimal place ex: 1.1 seconds

this is what I have so far

public void actionPerformed(ActionEvent ae){

		stop.setEnabled(true);

		long endTime;
		if(ae.getActionCommand().equals("Start")){
			display.setText("Start Button was Pressed");
			startTime=System.currentTimeMillis();
			return;
		}

		display.setText("Stop Button was Pressed");
		endTime=System.currentTimeMillis();
		DecimalFormat df = new DecimalFormat("#.#");
		
		String i=df.format(((endTime-startTime)/1000));
		
		display.setText("Seconds : " + i);//milliseconds-->seconds
		stop.setEnabled(false);

	}

Any help is appreciated guys!

Maybe this will help you.
A long/long is long. long/int is long. double/int is double. double/long is double. long/double is double ... and so on.

Apply this to line 16 of your code.

Hope this helps.

String i=df.format(((endTime-startTime)/1000));

I assume startTime is a long.
(endTime-startTime) produces a long. Therefore (end-start)/1000 is integer division. Integers in java are a closed set under division.

What is, for example, 3452342L/1000?

You need to convince the machine to divide floating points, not integers.

EDIT: in other words, what keikkaishi said.

This works for me:

public static void main(String[] args) {
        long time = System.currentTimeMillis();
        try {
            Thread.sleep(3740);
        } catch (InterruptedException e) {
        }
        long time2 = System.currentTimeMillis();
        NumberFormat nf = NumberFormat.getInstance();
        DecimalFormat df = null;
        if(nf instanceof DecimalFormat) {
            df = (DecimalFormat) nf;
            double seconds = (time2 - time)/1000d;
            df.applyPattern("#.#");
            System.out.println(df.format(seconds));
        } else {
            System.out.println(nf.getClass().toString());
        }
    }
}

Thanks, I got it to work by just casting it to a double.

Thanks for your input guys!

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.