4 Replies - 271 Views - Last Post: 20 September 2011 - 09:47 PM

#1 Tarkenfire  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 115
  • View blog
  • Posts: 314
  • Joined: 04-May 11

Annoying Logic Error

Posted 20 September 2011 - 12:03 PM

Okay, so working on one of my homework problems for this Android class and I have a logic error that I can't figure out and I think it's one of those things where it's a simple fix but I just need a fresh pair of eyes, so, you guys have eyeballs, and I'd like to borrow them...that sounded creepy.

Anyways, the basic idea of the program is to calculate how many seconds it takes to tap a button a certain amount of time (it's only the second homework), eh, I suck at explaining, so I'll just explain visually. The output is fine for the first run of the program:

Posted Image

But after the first run, it's...a bit different:

Posted Image

Here's the relevant code:
	public void onclick(View v)
	{
		if (counter == 0)
			startingTime = (int) (SystemClock.uptimeMillis() / 1000);

		if (counter == interval)
		{
			int endingTime = (int) (SystemClock.uptimeMillis() / 1000);
			displayResults(endingTime - startingTime);
			counter = 0;
			startingTime = 0;
		}

		++counter;
	}

	public void displayResults(int time)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("You tapped " + this.interval + " times in " + time
				+ " seconds.");
		builder.setCancelable(false);
		builder.setNeutralButton("Ok", new DialogInterface.onclickListener()
		{
			@Override
			public void onclick(DialogInterface dialog, int id)
			{
				dialog.dismiss();
			}
		});
		AlertDialog alert = builder.create();
		alert.show();
	}


(interval, counter, startingTime are all private ints)

I really hope it's just me being stupid about something, but I can't find what's causing this. Thanks in advance.


Full source, if needed.

Spoiler


Is This A Good Question/Topic? 0
  • +

Replies To: Annoying Logic Error

#2 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 276
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: Annoying Logic Error

Posted 20 September 2011 - 01:50 PM

Instead of using SystemClock.uptimeMillis() try using System.currentTimeMillis(); and change your startingTime and endingTime to long type.
Was This Post Helpful? 1
  • +
  • -

#3 Tarkenfire  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 115
  • View blog
  • Posts: 314
  • Joined: 04-May 11

Re: Annoying Logic Error

Posted 20 September 2011 - 02:05 PM

Posted Image

I'm gonna hazard a guess that that's about what the epoch time is now, and the 300K number I got before was just returning the uptime...that's strange.

Edit: yeah, that's the Epoch Time.

This post has been edited by Tarkenfire: 20 September 2011 - 02:06 PM

Was This Post Helpful? 0
  • +
  • -

#4 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 276
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: Annoying Logic Error

Posted 20 September 2011 - 07:34 PM

Ok found out where you error was. You need to put a return on line 50 after you set startingTime = 0;
Was This Post Helpful? 1
  • +
  • -

#5 Tarkenfire  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 115
  • View blog
  • Posts: 314
  • Joined: 04-May 11

Re: Annoying Logic Error

Posted 20 September 2011 - 09:47 PM

Huh, that's strange, I wonder why an increment would do that.

Regardless, thanks.

Edit: Okay, yeah, I understand why it works now.

This post has been edited by Tarkenfire: 20 September 2011 - 10:33 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1