David Barnes
David Barnes

Reputation: 43

Take in Strings with spaces

I'm trying to get into Java and I decided to jump straight into user input. Here is my code:

import java.util.Scanner;

public class HelloWorld{

 public static void main(String []args){
    System.out.println("Enter a phrase to mutate: \n"); 

    Scanner userinput = new Scanner(System.in);
    String phrase = userinput.next();
    System.out.println(phrase);

    String phrasemutation1 = phrase.toUpperCase();
    System.out.println(phrasemutation1);

    String phrasemutation2 = phrasemutation1.replace('I','X');
    System.out.println(phrasemutation2);

    //String phrasemutation3 = phrasemutation2.substring(17, 23);
    //System.out.println(phrasemutation3);
 }
}

However, when entering in anything with a space, only the first word is taken in. Searching the site just gives me results on how to remove them, but I'd like to keep the spaces intact. Is there a way to do this, or at the very least replace them with underscores?

Also, if there's some beginner formatting errors I'm making I'd love to hear them.

Upvotes: 2

Views: 79

Answers (4)

user1803551
user1803551

Reputation: 13407

You would want to familiarize yourself with the Javadoc. Scanner's explicitly states:

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.

So each next gives you a word which is separated by whitespaces:

The next() and hasNext() methods and their primitive-type companion methods (such as nextInt() and hasNextInt()) first skip any input that matches the delimiter pattern, and then attempt to return the next token.

You can change the delimiter with useDelimiter or in the constructor to match new lines.

As suggested by everyone else here, you can just use nextLine instead.


As for other improvements of your code, I would acquaint myself with System.lineSeparator() instead of all sorts of \ns and \rs.

Upvotes: 2

Haifeng Zhang
Haifeng Zhang

Reputation: 31895

String phrase = userinput.next();

Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true. Change to:

  String phrase = userinput.nextLine();

Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line. Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.

Upvotes: 3

lightning_missile
lightning_missile

Reputation: 2992

Have you tried

String phrase = userinput.nextLine();

The nextLine() method reads everything, including spaces until it encounters a newline.

Upvotes: 1

bpgeck
bpgeck

Reputation: 1624

Use userinput.nextLine() to get the whole like of text rather than individual strings.

userinput.next() will only get the next string (string delimited by a space).

Upvotes: 1

Related Questions