Why won't this scanner accept input on the println? [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 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.

Related

Split function giving incorrect result 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 1 year ago.
Improve this question
import java.util.*;
class Solution {
public static void main(String d[]) {
Scanner sc=new Scanner(System.in);
String s0=sc.next();
String p[]=s0.split(" ");
System.out.println(p.length);
}
}
I am using jdk version 8 and when i am giving input as "hello world java", it is printing length as 1 ,whereas it should print the length as 3,please help me in resolving this
Scanner.next() will get the input from the user till a space is encountered. For the input "hello world java" it only assign "hello"
If you want to include spaces also use Scanner.nextLine()

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+", " ")

Having trouble copying an ArrayList [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 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.

Random.nextInt seems wrong from a book [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 4 years ago.
Improve this question
It's a block of code for random verification code in a Servlet book.
the underlined part must be a number of 30+?
but the intention is one of those chars above
Pic
NextInt method only has one parameter which is the max number (exclusive)?
So is it a mistake in the book?
nextInt(int) returns a [pseudo]random number between 0 and the parameter passed to it, exclusive. Thus, if you pass a length of an array, you'd get a random valid index from the array, which you can then use to pick a random element from it, as this code does.

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