why eclipse sometimes auto adds "*/"? - java

I'm a new user of eclipse 3.7 and dont know why eclipse always auto adds "*/" when I didnt notice. So I'm wondering why this happened? I haven't catch when it happens since when I write the same pieces of the code the second time the problem disapears.Can someone tell me how to solve it?
for example,this following really annoys me
public void addBall()
{
try
{
Ball ball = new Ball();
comp.add(ball);
for (int i = 1; i <= STEPS; i++)
{
ball.move(comp.getBounds());
comp.paint(comp.getGraphics());
Thread.sleep(DELAY);
}
}
*/
catch (InterruptedException e)
{
}
}
The problem occurred when I press "{" following the word try. It seems only happens when the corresponding "}" is out of current view, however I'm not really sure.Plus,the compiler didnt give me any warnings.

I'd look at the Java code templates - possibly you triggered something there. The defined templates can be found in Preferences, the Java/Editor/Templates page.
At first look at the try template whether it contains the */; or look for similar patterns as well.

By default, Eclipse automatically adds "*/" when you input "/*" and hit Enter, the same way as it automatically adds closing parenthesis.

Related

How to compile and run some String entered in a TextArea?

My intentions are to ask the user to write down some code in a TextArea, then see if that code compiles, and if it does, print out the results to another TextArea, which acts like a console.
Edit:Solving this via online compilers is the priority.
To accomplish this, I've tried using online compilers (i.e. compilerjava.net) and used the library HtmlUnit, but the library came in with a lot of errors, especially when reading the JavaScript code and returned me pages of 'warnings' that increase the compile & run time for about 20 seconds. I will leave the code below with explanations if anyone has intentions about trying to fix it.
I've also tried using the JavaCompiler interface, which did succeed in compiling, but under the conditions that I have provided the exact location of the .java file, which is something I have to create using the information I get from the TextArea. So again, a dead end.
I decided to come back to online compilers, since if I can manage to just return the data from the compiled program, I am set. The only issue is I haven't yet found an online compiler that allows a user to access its fields via Java code ( since its something too specific). I would appreciate any help on this if anyone can provide a way to send and retrieve data from an online compiler.
Here is the code using the HtmlUnit library, on the site 'compilerjava.net'. It is so close to working that the only 2 issues I have is that,
1) Run-time is too long.
2) I cannot access the console output of the code. Reasoning is that, when you hit 'compile', the output text-area's text turns into "executing". After a few seconds, it turns into the output of the code. When I try to access it, the data I retrieve is always "executing" and not the desired output. Here is the code;
public class Test {
public static void main(String[] args) {
try {
// Prevents the program to print thousands of warning codes.
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF);
java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF);
// Initializes the web client and yet again stops some warning codes.
WebClient webClient = new WebClient( BrowserVersion.CHROME);
webClient.getOptions().setThrowExceptionOnFailingStatusCode( false);
webClient.getOptions().setThrowExceptionOnScriptError( false);
webClient.getOptions().setJavaScriptEnabled( true);
webClient.getOptions().setCssEnabled( true);
// Gets the html page, which is the online compiler I'm using.
HtmlPage page = webClient.getPage("https://www.compilejava.net/");
// Succesfully finds the form which has the required buttons etc.
List<HtmlForm> forms = page.getForms();
HtmlForm form = forms.get( 0);
// Finds the textarea which will hold the code.
HtmlTextArea textArea = form.getTextAreaByName( "code");
// Finds the textarea which will hold the result of the compilation.
HtmlTextArea resultArea = page.getHtmlElementById( "execsout");
// Finds the compile button.
HtmlButtonInput button = form.getInputByName( "compile");
System.out.println( button.getValueAttribute());
// Simple code to run.
textArea.setText( "public class HelloWorld\n" +
"{\n" +
" // arguments are passed using the text field below this editor\n" +
" public static void main(String[] args)\n" +
" {\n" +
" System.out.print( \"Hello\");\n" +
" }\n" +
"}");
// Compiles.
button.click();
// Result of the compilation.
System.out.println( resultArea.getText());
} catch ( Exception e) {
e.printStackTrace();
}
}
}
As I said, this final code System.out.println( resultArea.getText()); prints out "executing", which implies that I have succeeded in pressing the compile button on the webpage via code.
So after this long wall of text, I'm either looking for a way to fix the code I presented, which is so darn close to my answer but not quite, or just an entirely different solution to the problem I presented at the beginning.
P.S. Maven is the last hope.

IntelliJ Idea debug/run console; System.out.flush not flushing

This has been bugging me for the last couple days, because it used to work. I upgraded my intellij and now it doesn't work. I don't want to go back, but I need an answer.
So, I'm writing a console application, and while it runs, I want to have a shell'd out display of progress. It works fine when it's running, but when I'm debugging in IntelliJ Idea, the System.out.flush() won't flush to the console unless the buffer contains a newline. I wrote the following unit test to find out if it was me or my application.
#Test
public void flushTest() {
int loops = 100;
for (int i = 0 ; i < 100 ; ++i) {
System.out.print("This is a print, does it flush : " + i + "\r");
if (i %10 == 0) {
System.out.print("\n");
}
System.out.flush();
}
}
Spoiler, it is the application. I don't know what's changed, but it makes it very difficult to debug display issues without doing a full build and running on the command line. If someone can help me figure out why this changed and/or help me fix it so It'll flush properly, I'd be very grateful.
Tested on version 2016.3.5, the problem is still there.
The bug is caused by all characters after "\r" will be cleared in the console.
For example, using a string "Test\r" will be got "Test" written to console, after cursor is moved to the left by the "\r", all the characters "Test" is cleared. So you won't see anything.
The workaround is to move "\r" to the beginning of the string. For example, "\rTest" will see the output in console.
The following codes will illustrate the bug. You will see "0000" and "111" will appear for 1 second and then all are cleared by "22".
System.out.print("0000\r");
Thread.sleep(1000);
System.out.print("111\r");
Thread.sleep(1000);
System.out.print("22\r");
Until this issue is not resolved, current workaround is to use
-Deditable.java.test.console=true (specify it in idea.properties).

Eclipse debugger "jumps" skipping important code

I have a weird problem debugging an android application.
To be accurate, I copy here the exact code I'm running on:
// Get the puzzles from cache
List<PuzzleDetails> newPuzzles = m_cachedPuzzles.getPuzzles(count);
if(newPuzzles.size() > 0){
// Remove from cache
m_cachedPuzzles.removePuzzles(newPuzzles); // LINE (A)
// Add the new puzzles from cache immediately
m_ownedPuzzles.addPuzzles(newPuzzles);
Log.d("requests", "" + newPuzzles.size() + " moved from cache to user");
}
int left = count - newPuzzles.size();
String deviceId = ResourcesPublisher.getInstance().getDeviceId();
// Don't let anyone else use these points for now
ChallengePointsManagerImpl.getInstance().usePoints(left);
Log.d("requests", "aquirePuzzles(" + left + ")");
// Get a list of requests for 'left' number of puzzles
RequestList reqList = getRequestList(left);
// TODO this is a bug, now
if(reqList.size() > 1){
reqList = getRequestList(left); // LINE (B)
}
When I run on this code, after stepping over the line (A)
m_cachedPuzzles.removePuzzles(newPuzzles);
The debugger "jumps" to the last line (B)
reqList = getRequestList(left);
A simple check shows it really skipped all code between these code lines.
For example the Log.d(...) was never called nor written.
Can anyone give me a clue why does it happen???
Thanks!
Try to do a right click > refresh on the project as it appears on the Project Explorer after you compile the code and before you start debugging.
Perhaps an exception was thrown from line A, and the next step corresponds to it closing off this stack frame?
mIsReaded = (mIsReaded)?false:true;
//mIsReaded = !mIsReaded;
saveReadFlag();
refreshUI();
Toast.makeText(getSherlockActivity(),...
In my case commented codeline cause the similar problem (two lines are skipped). For to solve it I just changed this line by codeline posted above (I mean mIsReaded = (mIsReaded)?false:true;) So different cases haves different solutions. It is result of code optimization by compiler, so please refactor something in (inside)
m_cachedPuzzles.removePuzzles(newPuzzles);
I had the same problem. The thing is, you are probably debugging the code that is in your IDE and not the on on the server. You have to deploy the code from the IDE (Eclypse, Netbeans etc.) on the server. It worked for me! Good luck!
Not directly related to Eclipse but I experienced a similar problem using the Xamarin Extension for Visual Studio and my realization may be of some help. I was developing an App with a Class library. when i made changes to the library then began emulating my App, the DLL wouldn't always rebuild so the debugger would step through the PDB as it was before my latest changes. After rebuilding the DLL, it would step through fine.
In short, rebuild dependencies if there are any changes made.
Hope you solve your issue. Have a good one
comment TODO using multi-line comment
/*// TODO this is a bug, now*/
and try again.

syntax error: insert } to complete ClassBody

I created a method and keep getting an error that I need to include a } at the end of my method. I put the } in and the error is still there! If I then delete that } the same error will pop up on a prior method; and that error wasn't there before. in other words, if i type the } on my most recent method then the error stays there and only there. if i delete it, it duplicates that error on my prior method.
private void putThreeBeepers() {
for (int i = 0; i < 2; i++) {
putBeeper();
move();
}
putBeeper();
}
private void backUp() {
turnAround();
move();
turnAround();
}
You really want to go to the top of your file and do proper and consistent indention all the way to the bottom.
For example...
private void putThreeBeepers()
{
for (int i = 0; i < 2; i++) {
putBeeper();
move();
}
putBeeper();
}
private void backUp()
{
turnAround();
move();
turnAround();
}
Odds are, somewhere along the line, you are missing a }. Your description isn't super clear, but if the code you posted is how you actually have it formatted in your file then odds are you just missed something somewhere... and poor indentation makes it very hard to spot.
The fact that the message is changing is confusing, but it is the sort of thing you see in these cases.
the error might be misleading. In my case i had incorrect/incomplete comment statements such as below which is broken lead to this error:
/*
// */
*/
Fixing the comments fixed the error. Hope this helps. Thanks.
I think this can be caused by a number of different problems. :(
In my case it was that I have forgotten to specify the type of two parameters in one of my methods declareations.
To fix I had to change this:
onUpgrade(SQLiteDatabase pDb, pOldVersion, pNewVersion)
{
}
to this:
onUpgrade(SQLiteDatabase pDb, int pOldVersion, int pNewVersion)
It might be due to invisible Chars when copying code from PDF ebook.
Be careful of the little red dot '.'
Choose 'Select First Character' -> then delete it.
Also, the same error might occur if you accidentally write an if-statement outside of a method. I kept overlooking it since I was looking at the bracket-matching only.
I just simply add another"}",makes as "}}",then problem solved
I didnt have to put anther "}" for my other java code exercise.
Im a java beginner,I've come across same problem so I searched online found this thread.Hope this help
Had the same problem. Turned out to be a very fundamental oversight.
I was having the properties of a class declared like this:
private Texture foo;
private Sprite bar;
foo = new Texture();
bar = new Sprite();
The mistake was i had been instantiating the foo and bar variables outside the functions
of the class. When I put the
foo = new Texture();
bar = new Sprite();
into their proper functions (like below), the error went away.
private Texture foo;
private Sprite bar;
// function
public void instantiateVariables(){
foo = new Texture();
bar = new Sprite();
}
I got this error due to a missing <%.
Here are the steps.
Just copy paste your code in notepad
Remove copy from Java file
Again copy notepad and paste into Java file.
An error will be gone.
This question have already accepted answer but still there are some other problems where it occurs (like even though all statements are there correctly sometimes we will get this issue) and there are solutions too.
Recently I came across like this situation in eclipse workspace.
The solution is
Take the back up of that java file.
Remove the java file from that location and do the build/compile
Paste the file in the same location and do the build/compile
If the above step 3 does not work create new file and paste the content/code from backup file and do the build/compile
If the above step 3&4 both are not working then type the code of that file manually end-to-end and do the build/compile
If this error comes in jsp, look for the braces. if open or close braces are missing, we will get this error.

How do I setup Eclipse to stop on the line an exception occurred?

How can I setup Eclipse to stop at the point an exception occurred.
I have an Eclipse breakpoint setup to break on an exception. In the code example below, the problem I'm having is Eclipse tries to open the Integer source code. Is there any way to just have debugger break at the point shown in my code example? If I move down the stack trace, I will get to this line, it'd be nice if there's a way to do this without the "Source not found" window coming up.
This can be done in Visual Studio, so it's driving me crazy not being able to find a way to do this in Eclipse.
package com.test;
public class QuickTest
{
public static void main(String[] args)
{
try
{
test();
}
catch(NumberFormatException e)
{
System.out.println(e.getMessage());
}
}
private static void test()
{
String str = "notAnumber";
Integer.parseInt(str);//<----I want debugger to stop here
}
}
I'm not sure how else to get there, but I just do:
Window -> Show View -> Breakpoints
And in that tab there is a "J!" that lets you set breakpoints on exceptions.
Preferences -> Java -> Debug -> Step Filtering
Choose packages you want to filter out when debugging (java.*, sun.*, etc)
On debug make sure the little step filtering arrow is selected.
This should do the trick.
Can't you just set a breakpoint on the Integer.parseInt(str) line?
If you point your Eclipse project at using a JDK instead of a JRE it should pick up the source code for all of the platform classes (Integer etc).
The Debug view will show the entire stack of the current execution. You need to select the line you want to see. It will be same as you had a breakpoint there, you can see all the variables etc.

Categories