When is IWizard.canFinish executed - java

I have overridden the canFinish() method of IWizard. The API can be found here
Here is my code:
#Override
public boolean canFinish(){
if(templatePage.isPageComplete()
&& jenkinsPage.isPageComplete()
&& containerPage.isPageComplete()
&& dataSourcePage.isPageComplete()
&& queuePage.isPageComplete()
&& deploymentPage.isPageComplete()){
return true;
}
else if(model.isDeployOnly()){
return true;
}
return false;
}
There are other methods like addPages() that are also executed.
Really, I want to know when canFinish() is executed? And is there a list of the order in which they are executed?

canFinish is called by the WizardDialog whenever it needs to update the buttons on the button bar (the Back, Next and Finish button). There will be calls when the wizard is first shown and when you move between pages. Individual wizards can also call IWizardContainer.updateButtons whenever they want the button status updated.
Note: the default code for canFinish is:
public boolean canFinish() {
// Default implementation is to check if all pages are complete.
for (int i = 0; i < pages.size(); i++) {
if (!pages.get(i).isPageComplete()) {
return false;
}
}
return true;
}
so if you just want to call isPageComplete on all your pages just call super.canFinish

Related

How to stop actor from continuously picking up object?

I'm taking an intro to java class at school and were doing very early on stuff. We are given a few methods and the knowledge of how if else statements, while loops, etc work. We use a program called greenfoot. The objective of the project is to have the character go around and pick up all the leaves, then place them at the end of the row and repeat. The order is changed so you need control statements. I am encountering an issue where at the end of each row, I will place a leaf, then pick it up and loop that forever. I have tried to solve it with variables to no avail. My code is here:
public void rowSweep() {
boolean turnCheck = false;
boolean leafCheck = true;
if (foundLeaf() && (leafCheck)) {
pickLeaf();
leafCheck = true;
}
else if (canMove() && (!facingNorth())) {
walk();
leafCheck = true;
}
else if (hasLeaf()) {
while (hasLeaf()) {
placeLeaf();
leafCheck = false;
}
}
else {
turnLeft();
while (canMove() && (!facingNorth())) {
walk();
turnCheck = true;
}
if (!canMove() && (turnCheck)) {
turnLeft();
walk();
turnLeft();
}
}
}
There are no syntax errors, just it doesn't do it properly. How can I make it so that I can move on after I place it instead of infinitely repeating?

Using booleans with set and get methods

To summarize I am making a program for a metro ticket system. and I am using set and get methods for it, when it comes to boolean values (since I need to validate that the person enters enough money for the ticket) how am i supposed to put in the main class( it is defined in brain) using the set method and an if statement.Here is a little fraction of the entire code and the rest is on github(https://github.com/alexxei4/subwayticket). The main is basically the class that will be used for interaction with the user and the brain is where alot of the actions are defined.All help is appreciated, please and thank you.
if (Choice1a == 10){
if(subway1.ticketcounter1(true);){
System.out.println("PRINT SUCCESSFUL, COLLECT YOUR TICKET!");
}
if(subway1.ticketcounter1(false);){
System.out.println("INSEFFICIENT FUNDS, PLEASE ADD MORE");
}
This is not how you evaluate boolean values, you just place the value in an if statement and it will proceed if true and refuse if false, also there is no need to duplicate the statement when you can just place an else block to handle situations that are not true:
if(subway1.ticketcounter1) {
System.out.println("PRINT SUCCESSFUL, COLLECT YOUR TICKET!");
}
else {
System.out.println("INSEFFICIENT FUNDS, PLEASE ADD MORE");
}
Also do not include semicolons in if statements, that's incorrect syntax. Read more about how to use use boolean values here: https://codingbat.com/doc/java-if-boolean-logic.html
EDIT:
After reading through your Github code I see that ticketcounter1 indeed is a method, but what it's doing is trying to change the value of ticketcounter1 like it's a referenced object, but boolean are primitive data types and can't be referenced, and even if they could it still wouldn't work because Java is a pass-by-value language. Read here for more information on that.
public void ticketcounter1(boolean ticketcounter1){
if (credit1 > total1){
ticketcounter1 = true;
}
else {
ticketcounter1 = false;
}
}
public void ticketcounter2(boolean ticketcounter2){
if (credit2 > total2){
ticketcounter2 = true;
}
else {
ticketcounter2= false;
}
Like the other answer said you should be returning the value as boolean instead of trying to change it:
public boolean ticketcounter1(){
if (credit1 > total1){
return true;
}
else {
return false;
}
}
public boolean ticketcounter2(){
if (credit2 > total2){
return true;
}
else {
return false;
}
}
But all in all your code demonstrated fundamental flaws in understanding how the language works, I would suggest picking up a good Java for beginners kind of book or do some introductory online tutorials. Here is a good place to start your learning journey: https://docs.oracle.com/javase/tutorial/java/index.html
You code is like this
public void ticketcounter1(boolean ticketcounter1){
if (credit1 > total1){
ticketcounter1 = true;
}
else {
ticketcounter1 = false;
}
}
public void ticketcounter2(boolean ticketcounter2) {
if (credit2 > total2){
ticketcounter2 = true;
}
else {
ticketcounter2= false;
}
}
It should be like this. Instead of using the variable and passing it though parameter. Use getter. Besides that your code won't run since subway1.ticketcounter1(true) is giving nothing. It is only changing variables stored in Brain.java. No information is being sent to main.
public boolean ticketcounter1(){
if (credit1 > total1){
return true;
}
else {
return false;
}
}
public boolean ticketcounter2(){
if (credit2 > total2){
return true;
}
else {
return false;
}
}
You can create functions without parameters. I don't know what were you trying to do?
if (Choice1a == 10){
if(subway1.ticketcounter1()){
System.out.println("PRINT SUCCESSFUL, COLLECT YOUR TICKET!");
}
if(subway1.ticketcounter1()){
System.out.println("INSEFFICIENT FUNDS, PLEASE ADD MORE");
}
}
subway1.ticketcounter1() will give either true and false. Do not use ; in if statement condition. ; ends the statement. Check this guide to learn about use of semi-colon If you do want to use ; The code should look like this
if (Choice1a == 10){
boolean ticketCounter1 = subway1.ticketcounter1();
if(ticketCounter1){
System.out.println("PRINT SUCCESSFUL, COLLECT YOUR TICKET!");
} else {
System.out.println("INSEFFICIENT FUNDS, PLEASE ADD MORE");
}
}
P.S You don't need two ifs if-else would be better in this case
if(condition) {
// Conditions is true
} else {
// Condition is false
}

Java while loop for false

I created a while loop which i want to stop when it becomes true
so i put:
while(!vending1.cansEmpty()) {
vending1.clickCan();
count++;
}
now it has been set so that the vending machine has 10 cans inside
and every time you call the method clickCan() it minus's 1 can,
but the while loop doesn't end when it the machine is empty (method becomes true), it just goes on infinitly
I am not sure why though
public class VendingMachine(){
private Integer cans;
public VendingMachine(int numCans) {
this.cans = numCans
}
public void clickCan() {
cans--;
}
public boolean cansEmpty() {
if (cans <= 0) {
return true;
} else {
return false;
}
}
}
I think you just need to make sure that your cans empty function returns a boolean.

Android simple methods not working?

I have a spinner where the user can select something. When they do, it calls switchCat() which then calls chooseView() and based on the int passed will do something different. In this case it loads a different set of array strings.
However, whenever I run it, I will ONLY see the strings from R.array.array0, so the first if in chooseView(). csArray is set nowhere else. I am calling chooseView() onCreate.
I am a novice. Thank you!
Spinner item selected
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
if (synthetic) {
synthetic = false;
return true;
}
switchCat(itemPosition);
return true;
}
switchCat();
public void switchCat(int i) {
if(i == 0) {
chooseView(0);
}
else if(i == 1) {
chooseView(1);
}
}
chooseView()
public void chooseView(int i) {
if(i == 0) {
CharSequence[] csArray = getResources().getTextArray(R.array.array0);
for (CharSequence s : csArray) {
mArrayList.add(s);
}
}
if(i == 1) {
CharSequence[] csArray = getResources().getTextArray(R.array.array1);
for (CharSequence s : csArray) {
mArrayList.add(s);
}
}
}
It looks like you are adding items to your ArrayList. Are you sure you don't mean to get rid of old ones as well prior to adding? If you don't, they will remain in the list when chooseView() is called. In chooseView(), simply call mArrayList.clear() before your if statements. This, of course, assumes that your ListView is dependent on mArrayList.
Whenever you run first time it will give correct output,another time you arraylist will add the values with previous one,so every time you want's to clear the arraylist for get a correct output.
public void switchCat(int i) {
myArrayList.clear();
if(i == 0) {
chooseView(0);
}
else if(i == 1) {
chooseView(1);
}
}

Java: If condition only being checked once

I'm trying to create a simple Android app in Java with the following code:
public class MainActivity extends Activity {
//Declare variables
boolean first = true;
boolean secondorbefore = true;
Button ClickMe = (Button) findViewById(R.id.clicker);
ClickMe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Check to see if this is the first click
if (first = true) {
first = false;
// DO STUFF FOR FIRST CLICK
} else if ((secondorbefore = true) {
//so this is the second click?
secondorbefore = false;
// DO STUFF FOR SECOND CLICK
} else {
//OK it's the third click or later
// DO STUFF FOR THIRD OR LATER CLICKS
}
}
});
}
However, it only seems to run through the if condition once. It carries out the code in the first instance, including setting the secondorbefore variable to false, but subsequent clicks seem to do nothing. The code OnClickListener is being executed in subsequent clicks but it's just not running through the conditional statement.
New to Java, so I'm probably making a very obvious mistake.
Many thanks in advance.
Hint:
= is an assignment operator.
== is an equality operator.
What happens when you use assignment operator in an if statement ??
What happens when you use equalityoperator in an if statement ??
in ifconditions you always have to use the == operator for comparison and not the single =. So it would be:
if (first == true) { // this is comparison
first = false; // this is assignment
// DO STUFF FOR FIRST CLICK
} else if ((secondorbefore == true) {
//so this is the second click?
secondorbefore = false;
// DO STUFF FOR SECOND CLICK
} else {
//OK it's the third click or later
// DO STUFF FOR THIRD OR LATER CLICKS
}
if you use = instead of == you are assigning a value instead of comparing. And the return of the assingment is the value being assigned. So in that case it will be true So:
if(bool = true){...}
and
if(true){...}
are equivalent in the comparison. The difference is that bool will carry the new value from this statement on.
change this if (first = true) to { if (first == true) {
Here = is an assignment operator .But == is equality operator.
When checking if a variable is equal to something else always use ==
ie
public void onClick(View v) {
//Check to see if this is the first click
if (first == true) {
first = false;
// DO STUFF FOR FIRST CLICK
} else if ((secondorbefore == true) {
//so this is the second click?
secondorbefore = false;
// DO STUFF FOR SECOND CLICK
} else {
//OK it's the third click or later
// DO STUFF FOR THIRD OR LATER CLICKS
}
}

Categories

Resources