What is this element and how does one use it? [duplicate] - java

This question already has answers here:
"loop:" in Java code. What is this, and why does it compile?
(12 answers)
Closed 7 years ago.
In this code:
search :
for (int i = 1; i <= 30; i++) {
System.out.println(i);
if (i == 20) {
break search;
}
}
What is search and how does one use it?
And wouldn't this code do the same without it?

search: is a label. You use labels with loops (break and continue) to indicate which loop is to be exited or continued.
Tutorial: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
In this case, there's only one loop. So yes I think the program would work the same without the label. However, occasionally I've seen labels used as a kind of comment to indicate what exactly is being done, and I guess that's what the coder here has done

Related

Java for loop is not removing all empty arraylist lines [duplicate]

This question already has answers here:
Delete data from ArrayList with a For-loop
(14 answers)
Closed 1 year ago.
I created new ArrayList of packages and with loop want to check, which packages have plans and which is don't have (empty). In my case I have 3 packages and all of them don't have plans (plans is all empty). If package don't have plan, I want to remove it. I tried to to in this way, but code removing only 2 of 3 packages. Don't know why, because all of them don't have plans.
List<Package> package = new ArrayList<Package>();
package.addAll(package3);
for (int i = 0; i < package.size(); i++) {
if(package.get(i).getPlan().isEmpty()) {
package.remove(i);
}
}
You could to it like this
list.removeIf(s -> s.getPlan().isEmpty())

Initializing a variable within an if statement? (For a game) [duplicate]

This question already has answers here:
variable access outside of if statement
(5 answers)
What is 'scope' in Java?
(2 answers)
Closed 2 years ago.
I'm new to programming and I thought it would be fun to build a game, but I've run into an issue. I want to make it to where the user can collect an item once but once they collect it they can't collect it anymore. The way I'm seeing if they collect it is by changing the value of that variable from 0 to 1. This is what I've got for the collection, but it doesn't seem to be working. It's saying it can't find the variable yesCookie. I also want to point out that this is just the section that allows you to take it or not, the variable stuff is somewhere else.
if(moldyCookie == 0)
{
String yesCookie = "In a cavity within the wall you see a mold-covered cookie. Bleh. \nYou obtained the Rotten Cookie!\n";
}
else
{
String yesCookie = "There's nothing but an empty cavity in the wall. ";
}
System.out.println("You walk forward towards the painting, moving it aside."+yesCookie+"You return to the door.");
System.out.println();

Java how to see if String has an integer in it [duplicate]

This question already has answers here:
Java - char, int conversions
(4 answers)
Closed 3 years ago.
I'm trying to determine if my ticket number has a "9" then change the price to 5. This is the code I have. The code itself compiles, but doesn't work as intended. I imagine it's because I'm using a wrong operator. If anyone could let me know how I could change it for it to work that'd be greatly appreciated.
for(int x=0; x<Integer.toString(e1.getNumber()).length(); x++)
{
if(Integer.toString(e1.getNumber()).charAt(x) == 9)
{
System.out.println("The price is $5");
}
}
Thank you! I just had to change 9 to '9'! Its working now!!!
it depends on specific requirements. what if 9 is more then one times in string? what you do in such case? if you are sure that 9 appears once in your ticket price. just you next code.
if (yourString.contains("9")){
yourString = yourString.replace("9", "5");
}
Just had to put '9' so it looks for a char!

Eclipse: Iteration's count while Debugging a for each loop [duplicate]

This question already has answers here:
Is there a way to access an iteration-counter in Java's for-each loop?
(16 answers)
Closed 5 years ago.
Can we know the count of the iteration (that is whether it's the first, second, third ... iteration ) while we are stepping over the code in a for each loop, in the eclipse debugger? Eclipse must be holding a count but how to get it show it to us. Apart from the workarounds of introducing an index in the code can we utilize something in built in eclipse.
You can initiate a counter and increment it in the for loop, it should be a workaround.
You can track the variable under Eclipse console below:

Unfamiliar piece of enhanced for loop syntax [duplicate]

This question already has answers here:
"loop:" in Java code. What is this, and why does it compile?
(12 answers)
Closed 9 years ago.
I have the job of going through another's code and I'm trying to figure it all out when I came across this for loop.
//I don't understand the purpose of assetLoop
assetLoop: for (AssetObject asset : assets) {
//Some code
}
I've never seen this syntax and I can't find any reference to it anywhere through my google searches. Can anyone tell me what assetLoop: is doing? Or simply give me the name of this concept so I can do some non-mindless googling and read about it? :)
This is called a label.
It allows you to write break assetLoop from a nested loop to break out of the outer loop.
It's essentially a limited form of goto, and is rarely used.
It's a label. You can put them on any statement. break assetLoop; will break out of that loop, even if the break statement is within another for, while, do-while or switch statement. Similarly continure assetLoop; will jump to the next iteration of the loop.

Categories