This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Create a counter that increments once a second until a given number of seconds is reached Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Create a counter that increments once a second until a given number of seconds is reached" Watch "Create a counter that increments once a second until a given number of seconds is reached" New topic
Author

Create a counter that increments once a second until a given number of seconds is reached

Luke Stamper
Ranch Hand

Joined: Jun 19, 2011
Posts: 64
I am trying to get user input to start a counter and end a counter. Right now, the error I am getting is...

F:\Object Java\Assignments\Assignment 7\TimerJavaRanch.java:58: error: int cannot be dereferenced
number.setValue(number);
^

Also, I have not inserted a loop to to restart the program (I didn't want to loop anything until I got the program working).


Here is the Example Output for how my program should run:

--------------------Configuration: <Default>--------------------
This progam tests a timer.
Enter a start number of seconds
5
Enter an end number of seconds
11

CounterController Starting.

6...
7...
8...
9...
10...
11...

Continue?(y/n)
y
Enter a start number of seconds
0
Enter an end number of seconds
4

CounterController Starting.

1...
2...
3...
4...

Continue?(y/n)
n

Process completed.



Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
change line 50 to this as the method argument is shadowing the ObjectReference number

Luke Stamper
Ranch Hand

Joined: Jun 19, 2011
Posts: 64
Currently, my code compiles, but my counter is not working. The user input is basically ignored. The counter starts at zero and gets to 1, then it keeps repeating 1.

My goal is to start the counter at the first user input and end the counter at the second user input. Can anyone lend a helping hand and help me get my code straightened out?

Here is the example output....

--------------------Configuration: <Default>--------------------
This program will test a timer.
Enter a start number of seconds.
2
Enter an end number of seconds.
9
Time Is : 00
Time Is : 01
Time Is : 01
Time Is : 01

Process interrupted by user.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

You pass values 2 and 9 to the NumberDisplay constructor. However, that constructor uses the 2 as the limit (end) and 9 as the start. Swap the parameters around in the constructor.

After that your program will go to 8, then go back to 1. The reason is simple - startValue will never be the same as endSec, because if it would the mod (% endSec) turns it into 0 again.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Luke Stamper
Ranch Hand

Joined: Jun 19, 2011
Posts: 64
I swapped the parameters around and my counter still starts at zero and goes to infinity. There must be a problem somewhere else in the code i'm not seeing?

To stop the counter, I am trying to figure out a way to have the counter number = endSec. Logically, I think that makes sense to stop the program.

Luke Stamper
Ranch Hand

Joined: Jun 19, 2011
Posts: 64
So i've switched my code around....here is what i'm trying to do

In this program, there will be 3 classes. Your test program will input a start and end number of seconds. Call the CounterController only from your test program. The controller will create an instance of CounterModel with the initial value and call increment until the end value is reached. The CounterModel will wait 1 second, increment its counter and then display its counter by calling CounterView. Use Thread.sleep(1000); to delay 1 second.

I am having a lot of trouble with my methods in various classes.

I'm currently getting these errors:

--------------------Configuration: <Default>--------------------
F:\Object Java\Assignments\Assignment 7\CounterController\src\CounterController.java:16: error: cannot find symbol
Display();
^
symbol: method Display()
location: class CounterController
F:\Object Java\Assignments\Assignment 7\CounterController\src\CounterController.java:21: error: cannot find symbol
counterValue.setCounterValue(startValue);
^
symbol: method setCounterValue(int)
location: variable counterValue of type CounterView
F:\Object Java\Assignments\Assignment 7\CounterController\src\CounterController.java:22: error: cannot find symbol
Display();
^
symbol: method Display()
location: class CounterController
F:\Object Java\Assignments\Assignment 7\CounterController\src\CounterController.java:44: error: cannot find symbol
System.out.println("Time Is : " + counter.getTime());
^
symbol: method getTime()
location: variable counter of type CounterController
F:\Object Java\Assignments\Assignment 7\CounterController\src\CounterController.java:45: error: cannot find symbol
counter.counterMode();
^
symbol: method counterMode()
location: variable counter of type CounterController
5 errors

Process completed.

--------------------Configuration: <Default>--------------------
F:\Object Java\Assignments\Assignment 7\CounterController\src\CounterModel.java:23: error: cannot find symbol
Display();
^
symbol: method Display()
location: class CounterModel
F:\Object Java\Assignments\Assignment 7\CounterController\src\CounterModel.java:29: error: cannot find symbol
Display();
^
symbol: method Display()
location: class CounterModel
F:\Object Java\Assignments\Assignment 7\CounterController\src\CounterModel.java:38: error: cannot find symbol
Display();
^
symbol: method Display()
location: class CounterModel
3 errors

Process completed.


John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Take one error at a time.

F:\Object Java\Assignments\Assignment 7\CounterController\src\CounterController.java:16: error: cannot find symbol
Display();
^
symbol: method Display()
location: class CounterController


It says the method named Display() is not present or not resolved in the class CounterController.

1. Check you have a method named Display() in the class you are using.

2. Do you wish to call a Display() method in the other classes CounterModel or CounterView?

3. If so is the method Display() is declared static or non-static.

4. If the Display() method is declared static you call it like classname.Display()

5. If the Display() method is non-static, you shall call it like object.Display(). So you have to create an object for the class first before invoking the method.
I think you intend to call the Display() method in the CounterView class - So it would be something like


6. To make a method accessible outside the class, it should not be marked private. So change the access specifier from private to public for the method Display() in CounterView class.

And after resolving this error you can take up the next...
Luke Stamper
Ranch Hand

Joined: Jun 19, 2011
Posts: 64
So this is my CounterController class (oh yeah, i'm down to three errors!!!)

I did what you suggested and created a new object (counter) of class CounterController, similar to what was done to get the Display() method to work, however, I am getting the errors....
I don't understand why the same strategy isn't working for these two methods.

--------------------Configuration: <Default>--------------------
F:\Object Java\Assignments\Assignment 7\CounterController\src\CounterController.java:46: error: cannot find symbol
System.out.println("Time Is : " + counter.getTime());
^
symbol: method getTime()
location: variable counter of type CounterController
F:\Object Java\Assignments\Assignment 7\CounterController\src\CounterController.java:47: error: cannot find symbol
counter.counterModel();
^
symbol: method counterModel()
location: variable counter of type CounterController
2 errors

Process completed.


John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Now see below error -
F:\Object Java\Assignments\Assignment 7\CounterController\src\CounterController.java:46: error: cannot find symbol
System.out.println("Time Is : " + counter.getTime());
^
symbol: method getTime()
location: variable counter of type CounterController


You are calling the getTime() method using an instance created for the CounterController class. So do you have the getTime() method in the CounterController class?

Which class the getTime() method is present?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
My above post shall be used to resolve this error too -

F:\Object Java\Assignments\Assignment 7\CounterController\src\CounterController.java:47: error: cannot find symbol
counter.counterModel();
^
symbol: method counterModel()
location: variable counter of type CounterController


The method counterModel() is not present in the CounterController class.
But it's not present in CounterModel class too which I think you intend to call
Luke Stamper
Ranch Hand

Joined: Jun 19, 2011
Posts: 64
John, thank you so much for helping me out.

I hope this is the last error...


--------------------Configuration: <Default>--------------------
This program will test a timer.
Enter a start number of seconds.
1
Enter an end number of seconds.
3
Exception in thread "main" java.lang.NullPointerException
at CounterView.Display(CounterView.java:33)
at CounterController.<init>(CounterController.java:19)
at CounterController.main(CounterController.java:42)

Process completed.

Looking through my code, I think the error has something to do with my counter object i created from the CounterController class.
Luke Stamper
Ranch Hand

Joined: Jun 19, 2011
Posts: 64
I forgot to include my modified code...


John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
NullPointerException occurs when you try to do some operation on a reference that points to null. This is a Runtime Exception which you receive when you run your code.

Exception in thread "main" java.lang.NullPointerException
at CounterView.Display(CounterView.java:33)
at CounterController.<init>(CounterController.java:19)
at CounterController.main(CounterController.java:42)


The Exception says which line it has occurred - at CounterView.Display(CounterView.java:33) - it tells the Exception occurred at Display method in the CounterView class in the line # 33.
The other lines displayed beneath it is just a trace of the Exception and it leads to the line CounterController class's # 42...

So check the CounterView class in the line #33 which is the source of the NullPointerException. Can you tell which reference in that particular line might be pointing to null?
Luke Stamper
Ranch Hand

Joined: Jun 19, 2011
Posts: 64
I think it has something to do with the counterValue not being instantiated in the CounterView class, but when I tried to remedy that, it did not work. I am not really sure if that is the problem, but looking at my code, I think I need something more to use the counterValue.getDisplayValue(); properly.
Luke Stamper
Ranch Hand

Joined: Jun 19, 2011
Posts: 64
I figured it out! I had a friend shed some light on everything I was doing wrong and he helped me clean up my program. Thanks again for your help. I love the JavaRanch!

 
Consider Paul's rocket mass heater.
 
subject: Create a counter that increments once a second until a given number of seconds is reached
 
Similar Threads
Please help me this. Im new to this.
Running...tally...?
Timer problem...
One DIM array ??? continued
getting a clock to update correctly