Anyway so im trying to make something like a chat program and someone told me to use this code to check for new messages while allowing the user to submit a message:
timer.schedule(new TimerTask() {
@Override
public void run() {
read.readChat(line);
}
}, 0, 1000);
//Wait for user input
while(true) {
String bar = scan.next();
}
Where the read.readChat(line); is the method which displays the messages from another file. Java tells me that read and line both have to be declared as final… I dont understand why especially for the “line” because that’s a variable and I need it to change.
Additionally after I declare them as final I get this error:
unreported exception java.lang.Exception; must be caught or declared to be thrown
read.readChat(salt);
What am I doing wrong?
Second error tells you that the method
read.readChat(line);throws acheckedexception so you have to catch.You cannot use local variables declared at outer class in the Anonymous
innerclasses. Make themfinalor declare them asfields(instance variables).EDIT: