-3

I'm going to start this off with an explanation of what I'm trying to do. I am trying to detect if there is a space in a string, and give an error if there is one. I have tried indexOf, try/catches, and a few other possible solutions I can't remember the name of. is there any fairly simple way to do this? Sorry, I'm just learning so there might be other errors or things that can be done better. below is an example of what I have.

System.out.println("Please enter your guess of numbers. Please do not include spaces in your submission.");
        String guess = input.next();
        int attempt = guess.indexOf(" ");
        System.out.println(attempt);
        if (guess.matches(".*\\s+.*")){
            System.out.println(attempt);
        JOptionPane.showMessageDialog(null, "Try again, and Please do not enter spaces in between the characters.");
        System.exit(0);}

1 Answers1

-1
String text = "Mark Stevenson is a footbal player.";
if(text.contains(" ")) {
  System.out.println("Text contains at least one space.");
}
CaptainPyscho
  • 338
  • 1
  • 7