Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Identifier expected error

807598May 7 2006 — edited May 7 2006
Hello,

I am having an issue with an identifier error. I originally wrote the following code for a beginning java class at my University and it worked without any problem. The issue is when I try to compile it on my laptop at home, i get the identifier error at the list declaration and the public List<Duty> method . I suppose i could be using a newer version of the JRE but I have no way of checking at the moment. Any ideas on what is wrong here?
import java.util.*;

public abstract class Employee
{
	protected String 	 employeeName;
	protected char   	 status;
	protected int    	 employeeID;
	protected List<Duty> dutyList = new ArrayList<Duty>();
	private static int   key = 0;


	public Employee(String employeeName, char status)
	{
		this.employeeName = employeeName;
		this.status = status;
		employeeID = key++;
	}

	public int getEmployeeID()
	{
		return employeeID;
	}

	public String getEmployeeName()
	{
		return employeeName;
	}

	public char getEmployeeStatus()
	{
		return status;
	}

	public void addDuty(Duty duty)
	{
		dutyList.add(duty);
	}

	public List<Duty> getDutyList()
	{
		return dutyList;
	}

	public abstract int getVacationDays();

	public abstract double getHourlyWage();
}

Comments

807589
Since you're looking at the key code of a KeyEvent, I'll assume you're in a key pressed event handle method. This method is called by the Event Dispatch Thread (EDT). Calls to paint are also done on the EDT, so repaint has no effect until after this method ends. So the calls to Thread.sleep() cause your whole application to freeze. Want you want to do is create a swing Timer that you can use to update your stick figure every 200 milliseconds. You then stop the timer when you've updated the figures position 11 times.
807589
Perhaps another way to do this is to have you're repaint and algo statment in a class that extends thread, and start that thread from your program
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
Runner run = new Runner(component_you_want_to_repaint);
run.start();
break;
}
Then you have your thread class, and you use the SwingUtilities.invoke later to repaint your screen and the sleep() method to sleep the thread
public class Runner extends Thread{
Component comp;
public Runner(Component c){
comp = c;
}
public void run(){

				for(int i=0; i<=10; i=i+1){
					y = y+1;  //the int y represents the images position of the y-axis
                                                        Runnable doLater = new Runnable(){
                                                         public void run(){
                                                         comp.repaint(); 
                                                         }
                                                       };
							SwingUtilities.invokeLater( doLater );
					sleep(200);
						
					if(i == 10){
						for(int j=0; j<=10; j=j+1){
							y = y-1;
                                                        Runnable doLater = new Runnable(){
                                                         public void run(){
                                                         comp.repaint(); 
                                                         }
                                                       };
							SwingUtilities.invokeLater( doLater );
							sleep(200);
						}

}
Hopefully something like this will solve your problem. If you use the code, double check the syntax, and it'll probably need other mods specific for your program
807589
I vote for gte42e's recommendation about using a Swing Timer. You could also do this in a background thread as sierratech recommends, but why do this if you're not doing heavy calculations or some other time-hogging computation? Also, if you do go this route, I strongly advise you not to extend Thread and instead either implement Runnable or use a SwingWorker object. Finally, you should look into using key bindings to capture your keyboard input rather than keylisteners.
807589
Thanks to all of you who responded!!!

What is a key binding by the way?
807589
I tried to use a timer like this:
	public void keyPressed(KeyEvent e){
		switch(e.getKeyCode()){
			case KeyEvent.VK_DOWN:
				y += 10;
				break;
			case KeyEvent.VK_UP:
				for(int i=0; i<=10; i=i+1){
				
					timer.start();					
					y = y+1;
						for(int j=0; j<=10; j=j+1){
							y = y-1;
						}
						timer.stop();
						break;
				}
				break;
but it didn't work.
What did I do wrong??
807589
clamm wrote:
but it didn't work.
What did I do wrong??
I don't see anywhere where you are setting the Timer's delay or ActionListener.
To be perfectly honest, it looks like you you're just throwing a timer object in there, calling start and stop, and haven't bothered yet looked at the Swing Timer API or tutorial which is bound to fail. I suggest you look at both. Same for key binding. Good luck!
807589
Sorry I forgot to put my code in about setting the delay and the actionListener.
Here is my new code, but still doesn't work:
	Timer timer;
	
	public myClass(){
		timer = new Timer(1000, new TimerAction());
	}

	public void keyPressed(KeyEvent e){
		switch(e.getKeyCode()){
			case KeyEvent.VK_DOWN:
				y += 10;
				break;
			case KeyEvent.VK_UP:
				timer.start();
				
				System.out.println("Initial:"+y);
				
				for(int i=1; i<=10; i=i+1){
					y=y-1;
					System.out.println(y);
				}
				for(int i=1; i<=10; i=i+1){
					y=y+1;
					System.out.println(y);
				}
				timer.stop();
				break;
                 }
class TimerAction implements ActionListener {
	
	public void actionPerformed(ActionEvent event){
		repaint();
	}
}
Whats wrong??
807589
You're still not using your Timer correctly. Please read the timer tutorial and go through the examples. It's all explained there.
807589
Right now you start a timer that will repaint your window, but then you do all your changes to y and then stop your timer. The timer never gets to run.

You're should be trying to replace your for loops with the timer. Each call to actionPerformed should run one iteration of one of your for loops. You'll need a counter to keep track of which for loop you're in, and when the timer should end. Call timer.stop when the last change to y is complete.
1 - 9
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jun 4 2006
Added on May 7 2006
2 comments
68 views