Hey guys, i have this while loop for variable 'i' and i have created a board[][] which is 2x2 (so sizeX for the board is 2 in this example). The problem is i can't work out why it isn't leaving the while loop if it goes through one of the 'if' statements starting with 'i=1' and then it then sets 'i=i+1' (=2) but doesn't leave the while statement and tries to see what board[2][x] is equal to when obviously that is out of the bounds of the board. Sorry for confusing explanation i hope you understand...
I am assuming that you are new to programming because you are using BlueJ.
When your second loop is running, I'm guessing that your line x=x is keeping x within range and hence the loop never exits.
One good thing to help is to use a debugger or print statements to show what the values of x and i are at each stage. That will quickly let you know what is going wrong.
Related
I've been scratching my head at this for hours now, but I can't seem to figure this one out.
I'm writing a program that has a constant loop going in a Thread, which occasionally sends back data to another part of the program using an Arraylist.
I only want to send this data back after I have gathered 11 items in my arraylist. So I used this code:
//The rest of the loop in which i gather the values for key and velocity.
notenbuffer.add(new Noot(key,velocity));
if (notenbuffer.size() > 10){
System.out.println("Notenbuffer > 10, verstuur data");
if (notenbuffer.isEmpty()){
System.out.println("buffer is empty!");
}
else {
usbRead.writeNotes(notenbuffer);
System.out.println("emptied the buffer!");
notenbuffer.clear();
}
}
Now for some weird reason the program never empties the Arraylist, and just keeps on adding items to it. I've checked and it does in fact reach the usbRead.writeNotes(notenbuffer) part because this function gets called correctly. When I use the debugger it simply skips to the beginning of the loop after this function was called.
Is there a way in which I can empty an Arraylist once it reaches a certain size?
Edit: I made a logic error by writing if (notenbuffer.isEmpty()) this will always be false because I am already in an if statement which requires this to be false.
Did you put your second if inside your first by accident? (The missing indentation seems to suggest so). Having if
(notenbuffer.isEmpty()) inside the if (notenbuffer.size() > 10) block
makes no sense at all logically. After all if your List size is > 10
then the list is obviously not empty. So if (notenbuffer.isEmpty())
can never be true at all. – OH GOD SPIDERS 15 mins ago
This was indeed the problem. Removing this exposed an NoClassDefFoundError that needed resolving.
Firstly, I'm sorry for my bad english. I wrote my problem in the forums that speak my language. But I did not get an answer. I hope you can help me.
When I write the for loop, the System.out.println() or JTextArea.setText() command starts to work when the loop is over. Part of my codes:
for(int pixelCount = 0;pixelCount<pixelLength;pixelCount++){
System.out.println("Progress..:"+pixelCount+"/"+pixelLength);
int x = pixelCount%Image.getWidth();
int y = pixelCount/Image.getWidth();
if(isChange==1){
if(new Color(Image.getRGB(x, y)).getRed()%2==0){
Image.setRGB(x, y,new Color(new Color(Image.getRGB(x, y)).getRed()+1,
new Color(Image.getRGB(x, y)).getGreen(),
new Color(Image.getRGB(x, y)).getBlue(),
new Color(Image.getRGB(x, y)).getAlpha()).getRGB());}
}
The loop is sometimes very long. So I print it on the screen to see the progress of the loop. The loop starts when I press the button. But when I press the button, the System.out.println() command looks afterwards.
EDIT: How should I write the progress in the loop?
EDIT: I solved the problem, friends. The System.out.println () command works fine. There is a replace command before the for loop starts. The program spends time there before the for loop starts. Thanks everyone who took the time and helped me
Either call System.out.flush() after you print, or use System.err.println.
System.out is a buffered stream that prints when the buffer is full, when you flush it, or st other times that your system finds useful. System.err is unbuffered and is also the preferred stream for debug and diagnostic information, such as progress information.
Using of System.our.flush() was already mentionned by Erwin Bolwidt. The way output works strongly depends on type of OS (and terminal) you use. For example for *nix systems you could consider some kind of library similar to Curses (it is originally a C library, but I'm sure there are some Java implementations).
I'm doing some java programming in Netbeans 7.3 and I have written a program that contains a complex while loop. It reads a file in, does some calculations, then prints an output file with some changes.
Something goes slightly wrong at the point where the while loop goes through the 158th line of the text (158th cycle of the while loop). Nothing appears to be wrong with the file itself at this point.
Am I being stupid when putting a breakpoint on the while loop and continuously clicking "Step Out" 158 times to see what's going on? Is there an easier way to do this with java? Like running the loop until a variable reaches a certain value?
adding debugcode like
if (iterationCount == 158) {
int dummy = 13; // <= put breakpoint here
}
is a practical alternative to "conditional breakpoints".
Sometimes when I examine a code I didn’t write, I launch eclipse in debug mode and use figures to understand a program. For example, if they are n items retrieved from the DB, it can be interesting to know that there’re n processed items in a service, etc.
When loops are used, things get more complicated: if we’re in a “while” loop, the number of execution is not defined and if there are alternatives, the flow of execution can greatly change.
For this purpose, I sometimes set a breakpoint in a portion of code and count how many times we reach it.
Of course, it’s not very convenient and I wonder if there is a way to count the number of breakpoint hits. I know that Eclipse can suspend execution after a fixed number of hits but I just want Eclipse to count them in a normal flow of execution.
I’d be glad to hear your opinions about it.
Thanks!
You can add a condition into your breakpoint. The simplest one could look something like this:
System.err.println("Passing checkpoint");
return false;
You can also extend it by calling your own static class:
org.foo.StaticCounter.COUNTER++;
System.err.println("Passing checkpoint " + org.foo.StaticCounter.COUNTER);
return false;
As an alternative to counting breakpoints, you could use a profiling tool, such as the one here.
I'm working on a Java class in an Android project that summarizes array entries saved in previous classes, with each entry itself being an array with multiple elements.
I've have created methods to move forwards and backwards through the entries, but given there can be over 100 entries I would like to create another method that cycles through them instead of pressing the "Next" button over and over again.
Is there a way to do this?
I've found that loops will only show the last entry, but below is the best example I can think of, of what I need.
for (int i = Selection; i<=Qty; i++){
Num.setText(Integer.toString(i));
loadNext();
try{
Thread.sleep(1500);
}catch(InterruptedException e){}
if (Brk=true){
break;
}
}
The solution that would be closest to your original answer would be to create a background thread that does the loop, loading each item inside an Activity.runOnUiThread(). You can also do a similar thing with AsyncTask and progress updates. See this article for more information on both of these:
http://developer.android.com/resources/articles/painless-threading.html
However, a better solution is to not have a loop at all - just have a timer, and increment your loop variable each time the timer runs.
It may work. However, it will cause your UI to freeze during each time you call the sleep method. In general, when you are dealing with UI stuff, never use Thread class. Instead, use the Handler class. There are a lot of documentation but if, after you have search exhaustively, you can't find a good example just let me know.
Your break condition seems wrong, and causes the loop breaks at the first iteration:
if (Brk=true){
break;
}
Brk=true is an assigment exception, not a comparation exception. It will return always true. The expresion should be Brk==trueto check if the variable value is true. But again, it is a boolean variable, so you don't need to compare, but just reference it at the if statement:
if (Brk){
break;
}