• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.



 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
change line 50 to this as the method argument is shadowing the ObjectReference number

 
Luke Stamper
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Luke Stamper
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.



 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot to include my modified code...


 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 64
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

 
What are your superhero powers? Go ahead and try them on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic