java 比较字符与单空格
我正在尝试测试一个程序,如果用户输入一个空格,该程序将打印“空格”。 但是当我点击空格然后输入时什么也没有显示。我的目标实际上是计算空格数,但我想我会从这个开始。帮助我,谢谢你的帮助 这是我的代码 导入java.util.Scanner;
public class The
{
public static void main(String args[])throws Exception
{
Scanner scanner = new Scanner(System.in);
String input;
System.out.println("Enter string input: ");
input = scanner.next();
char[] charArray;
charArray = input.toCharArray();
for(char c : charArray)
{
if(c == ' ')
{
System.out.println("space");
}
else
{
System.out.println(" not space");
}
}
}
}
i'm trying to test a program that will print "space" if the user enters a single space.
but nothings displayed when i hit space then enter. my aim was really to count the number of spaces but i guess i'll just start with this. help me guys, thanks for any help
here's my code
import java.util.Scanner;
public class The
{
public static void main(String args[])throws Exception
{
Scanner scanner = new Scanner(System.in);
String input;
System.out.println("Enter string input: ");
input = scanner.next();
char[] charArray;
charArray = input.toCharArray();
for(char c : charArray)
{
if(c == ' ')
{
System.out.println("space");
}
else
{
System.out.println(" not space");
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Scanner
默认情况下会忽略空格。使用 BufferedReader 读取输入。Scanner
ignores spaces by default. UseBufferedReader
to read input.默认情况下,扫描程序将忽略所有空白,其中包括换行符、空格和制表符。但是,您可以轻松更改它划分输入的方式:
这将使您的扫描仪仅在新行处划分字符串,因此它将“读取”所有空格字符,直到您按 Enter 键。查找分隔符的更多自定义选项 这里。
By default, Scanner will ignore all whitespace, which includes new lines, spaces, and tabs. However, you can easily change how it divides your input:
This will make your Scanner only divide Strings at new line, so it will "read" all the space characters up until you press enter. Find more customization options for the delimiters here.
公共类 CountSpace {
}
public class CountSpace {
}