Having trouble copying an ArrayList [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
This might be a bit of a stupid question, but I can't figure this out! I'm working on a minigame that involves one player being randomly selected as the terminator, and all the rest as weaklings. I have an ArrayList of players, and I can get it to choose the terminator, and then I need it to copy players into weaklings, but skip terminator. The code I'm using doesn't copy anything at all into weaklings. Here's the code I'm using, could someone a bit more experienced help me out please:
for (int i = 0; i < players.size(); i++ ) {
if (!players.get(i).contentEquals(terminator)) {
players.add(players.get(i));
}
}
System.out.print("The other players are: " + weaklings + ".");
}

You add to the players again: players.add, instead of weaklings.add.

Related

I want to replace ALL double spaces with single spaces, including those after a period [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have this code:
item.replaceAll("\\s+", " ")
and this is the string
"The census request file for completion has been attached.  In addition, the attached Client Checklist "
It doesn't work: double space remains after the period, so ".  If" is unchanged.
I don't understand WHY!?
Since String.replaceAll() method is not an in-place operation, you should write like this:
item = item.replaceAll("\\s+", " ")

Why won't this scanner accept input on the println? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
When my application compiles, it will not accept the input for roomNum on the same line in which it asks, "Please enter a room to search for:"
System.out.println();
if(roomNum < 0);
{
System.out.println("Please enter a room to search for: ");
roomNum = input.nextInt();
}
If I just use next instead of nextInt, it doesn't compile correctly.
The code above works, but will not accept the input on the same line which is the functionality I need.
Two things: Remove semi-colon after if condition and use System.out.print() instead if you want input on the same line.

How to initialize a String array with unknown length to null? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'mm working on this program in which connects Prolog and Java GUI. And i am encountering this problem where i don't know how many solutions the prolog program is gonna pass to java and therefore i can't declare the String array with fixed-length. This is my code:
String[] options; int i;
Query qMeat = new Query(new Compound("meat", new Term[] {new Variable("X")}));
i = 0;
while(qMeat.hasMoreSolution()){
options[i] = "" + qMeat.nextSolution().get("X");
i++;
}
I am getting this NullPointerException, which i guess because i didn't initialize the String array to null. And i don't know how to do so. I tried java.util.Arrays.fill(options,"") But not helping =(
Please help.
If you don't know the required size of the array in advance, you should use an ArrayList instead.
List<String> options = new ArrayList<>();
while(qMeat.hasMoreSolution()){
options.add("" + qMeat.nextSolution().get("X"));
}

Why am I getting an invalid character constant for the '[ in Java? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
There is an issue with for the '[ right in front of the data-testing-id and I am not sure why. Any help is appreciated.
for (int i = 1; i < 1001; i = i + 1) {
if(driver.findElement(By.cssSelector('[data-testing-id="data-id1"]'))!= null){
System.out.println("Element is Present");
}else{
System.out.println("Element is Absent");
}
}
Java String(s) can't be done in single quotes. This,
By.cssSelector('[data-testing-id="data-id1"]')
Should be (escaping the double quotes),
By.cssSelector("[data-testing-id=\"data-id1\"]")
or with single quotes inside the double quotes, like
By.cssSelector("[data-testing-id='data-id1']")
You should use
"[data-testing-id=\"data-id1\"]"
for(int i = 1; i < 1001; i++)
i++ is another way to add an increment of 1 to i for each loop... And a wee bit easier than typing (i = i + 1)
I am new to Java and not sure if that was worth your time reading, but I think its pretty cool and might make your code prettier...

Mod not working the way I expect [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I'm trying to count the number of items in a scanner object which are divisible by 2.
My code looks like this:
while (s.hasNext()) {
num = s.nextInt();
if ((num % 2) == 0); {
count++;
}
}
For every integer in the object though, count is increasing by 1, regardless if it is divisible by 2, or not. Can someone tell me what I'm doing wrong?
You have a semicolon (;) after your if clause. That means empty code is executed if the condition is true and the code in the code block is always executed.
The ; after the if should be omitted

Categories