How to get Java to register string input with spaces?

Here is my code:

public static void main(String[] args) { Scanner in = new Scanner(System.in); String question; question = in.next(); if (question.equalsIgnoreCase("howdoyoulikeschool?") ) /* it seems strings do not allow for spaces */ System.out.println("CLOSED!!"); else System.out.println("Que?"); 

When I try to write "how do you like school?" the answer is always "Que?". but it works great as a "howdoyoulikeschool"?

Should I define input as something other than String?

+4
source share
4 answers

in.next() will return lines separated by spaces. Use in.nextLine() if you want to read the entire line. After reading the line, use question = question.replaceAll("\\s","") to remove spaces.

+7
source

Instead

 Scanner in = new Scanner(System.in); String question; question = in.next(); 

Enter

 Scanner in = new Scanner(System.in); String question; question = in.nextLine(); 

It should be able to accept spaces as input.

+1
source

This is an example of input implementation in java, I added some fault tolerance only in the salary field to show how this is done. If you notice, you should also close the input stream. Enjoy :-)

 /* AUTHOR: MIKEQ * DATE: 04/29/2016 * DESCRIPTION: Take input with Java using Scanner Class, Wow, stunningly fun. :-) * Added example of error check on salary input. * TESTED: Eclipse Java EE IDE for Web Developers. Version: Mars.2 Release (4.5.2) */ import java.util.Scanner; public class userInputVersion1 { public static void main(String[] args) { System.out.println("** Taking in User input **"); Scanner input = new Scanner(System.in); System.out.println("Please enter your name : "); String s = input.nextLine(); // getting a String value (full line) //String s = input.next(); // getting a String value (issues with spaces in line) System.out.println("Please enter your age : "); int i = input.nextInt(); // getting an integer // version with Fault Tolerance: System.out.println("Please enter your salary : "); while (!input.hasNextDouble()) { System.out.println("Invalid input\n Type the double-type number:"); input.next(); } double d = input.nextDouble(); // need to check the data type? System.out.printf("\nName %s" + "\nAge: %d" + "\nSalary: %f\n", s, i, d); // close the scanner System.out.println("Closing Scanner..."); input.close(); System.out.println("Scanner Closed."); } } 
+1
source

Since this is a long time, and people continue to suggest using Scanner#nextLine() , there is another chance that Scanner can accept spaces included in input.

Scanner class

The scanner splits its input into tokens using the delimiter pattern, which by default matches a space.

You can use Scanner#useDelimiter() to change the Scanner separator to another template, such as line feed or something else.

 Scanner in = new Scanner(System.in); in.useDelimiter("\n"); // use LF as the delimiter String question; System.out.println("Please input question:"); question = in.next(); // TODO do something with your input such as removing spaces... if (question.equalsIgnoreCase("howdoyoulikeschool?") ) /* it seems strings do not allow for spaces */ System.out.println("CLOSED!!"); else System.out.println("Que?"); 
0
source

Source: https://habr.com/ru/post/1435426/


All Articles