为了账号安全,请及时绑定邮箱和手机立即绑定

Scanner NextChar in Java Example

标签:
Java

Java 中的 Scanner 类具有一些方法:next(),nextInt(),nextLong(),nextLine()等,但没有 nextChar() 。这是一个从java.util.Scanner的输入中获取下一个字符的示例。

Java 示例中的 Scanner 和 nextChar()

import java.util.Scanner; 

public class ScannerExample { 
    public static void main(String[] args) { 
        Scanner scanner = new Scanner(System.in); 

        // next() will return a string
        // charAt(0) will return the first character 
        char c = scanner.next().charAt(0); 

        System.out.println("Output = " + c); 
    } 
}

输入:

C

输出:

Output= C

Scanner.next

如果匹配指定的模式,则返回字符串

//  consume strictly one character you could use
char c = scanner.next("[a-zA-Z]").charAt(0);

Scanner.findInLine

尝试查找下一个出现的指定模式而忽略定界符。

// consume exactly one character you could use
char c = reader.next("[a-zA-Z]").charAt(0);

Scanner.next() 将返回一个单字符字符串。 String.charAt() 将返回字符串的char。

本文由博客一文多发平台 OpenWrite 发布!

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消