i am trying to make a encryption program.
How do i get rid of «Syntax error, insert «Finally» to complete BlockStatements» on line 100
<imports>
public class Afp {
...
/**
* Initialize the contents of the frame.
*/
private void initialize() {
....
JButton btnEncrypt = new JButton("Encrypt");
btnEncrypt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try{
String text;
StringBuffer passWord = new StringBuffer(""+ text);
for(int i = 0; i < passWord.length(); i++){
int temp = 0;
temp = (int)passWord.charAt(i);
temp = temp*9834 / 8942 /33 *90023243 * 9 +124324534 - 2335 *24324;
passWord.setCharAt(i, (char)temp);
}
}
}
});
...
}
}
Stephen C
688k94 gold badges790 silver badges1200 bronze badges
asked Sep 27, 2015 at 0:38
2
You are getting Syntax Error because you have written try block without catch or finally block. You can either delete try block or Add catch or finally
answered Sep 27, 2015 at 0:49
AkashAkash
743 bronze badges
The possible syntaxes for a try
statement go something like this:
// 1 try-catch
try {
....
} catch (SomeException ex) {
...
}
// 2 try-catch-finally
try {
....
} catch (SomeException ex) {
...
} finally {
...
}
// 3 try-finally
try {
....
} finally {
...
}
// 4 try with resources
try (...) {
...
}
...
(In forms 1, 2 and 3, you have to have at least one catch
or a finally
… or both. In the 4th form, you can leave out both catch
and finally
blocks because there is an implicit final block.)
Your code doesn’t match any of these. However, the correct fix for your code depends on what you are trying to do with that try
statement. If you don’t know, then perhaps just delete try {
and the matching }
.
answered Sep 27, 2015 at 0:52
Stephen CStephen C
688k94 gold badges790 silver badges1200 bronze badges
- Thread Status:
-
Not open for further replies.
-
I’ve had 1 error in my plugin and that is the only reason it hasn’t been working. here is the code.
- package me.KrisVos130.Tutorial;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.plugin.java.JavaPlugin;
- public class Tutorial extends JavaPlugin {
- getLogger().info("UHC Timer Enabled!");
- public void onDisable() {
- getLogger().info("UHC Timer Disabled!");
- public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
- if(cmd.getName().equalsIgnoreCase("Timer-Start")) {
- do Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
- Bukkit.getServer().broadcastMessage(ChatColor.DARK_RED + "20 Minutes in!"); {
The problem is at line 29 where ; is underlined in red with this error: Syntax error, insert «while ( Expression ) ;» to complete BlockStatements
I’m really bad with java so please help. -
You have a random do statement in your code…
The Syntax of a do statement isYou don’t have the while statement to go along with the do, and that’s why it’s throwing an error. The easiest way to fix is to just remove the do in front of your
- do Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
That or add a while loop.
-
Thanks alot! it works now and the timer works fully automatic now. I’m still learning java so sorry for the stupidity from me to ask this dumb question.
- Thread Status:
-
Not open for further replies.
Share This Page
Introduction
Let’s see the differences between final, finally, and finalize in Java programming language.
final
final is a keyword that can be used with a variable or a method or a class. If a final keyword is used with a variable then the variable is considered constant. We cannot change the value and it can be initialized only once.
public static final int NO_OF_MONTHS = 12;
If we try to assign a new value to the above constant we will get an error. The error message is displayed below:
The final field JavaExample.NO_OF_MONTHS cannot be assigned
If the final keyword is used with the method then that method cannot be overridden.
public final void finalMethod() { //final method }
If we try to override this method we will get an error message is displayed below:
Cannot override the final method from JavaExample
– overrides
com.testingdocs.sample.programs.JavaExample.finalMethod
When the final keyword is used with a class then we cannot extend or subclass that class.
public final class JavaExample {}
If we try to subclass this class, we will get an error message displayed below:
The type JavaSubExample cannot subclass the final class JavaExample
finally
finally is used along with try/ catch block in java for exception handling. Also, for exception handling in java, we need to use try and catch block or finally block. We cannot have a simple try block in Java. For example
try{ }
We will get Syntax error, insert “Finally” to complete BlockStatements
The finally block is guaranteed to be executed even if you get exception or not. The finally block will execute for sure at the end. That’s the reason, this block is used for cleanup code like closing streams, network connections, database connections, etc.
Below program exception will be thrown :
package com.testingdocs.sample.programs; public final class JavaExample { static int a; public static void main(String[] args) { finallyDemo(); } public static void finallyDemo() { try{ a= 1/0 ; } catch(Exception e) { System.out.println(e.getMessage()); } finally { System.out.println("Anywayz,This will be executed "); }; } }
Output:
/ by zero
Anywayz,This will be executed
Below code no exception will be thrown, but the finally will be executed as shown below:
package com.testingdocs.sample.programs; public final class JavaExample { static int a; public static void main(String[] args) { finallyDemo(); } public static void finallyDemo() { try{ a= 1/1 ; } catch(Exception e) { System.out.println(e.getMessage()); } finally { System.out.println("Anyway,this will be executed "); }; } }
Output:
Anyway,this will be executed
finalize
It is a method of the Object class that can be overridden by a subclass.
In Java Object
is the root of the class hierarchy. Every class has Object
as a superclass. All objects, including arrays, implement the methods of this class.
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. The general contract of finalize is that it is invoked if and when the JVM has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized.
The Java language doesn’t guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and the finalization of that object terminates.
After the finalize method has been invoked for an object, no further action is taken until the JVM has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.
The finalize method is never invoked more than once by a JVM for any given object. Any exception thrown by the finalize method causes the finalization of this object to be halted but is otherwise ignored.
—
Java Tutorial
Java Tutorial on this website:
For more information on Java, visit the official website :
https://www.oracle.com/java/