zoukankan      html  css  js  c++  java
  • Java基础 Scanner 使用nextLine接收字符串

    •     JDK :OpenJDK-11
    •      OS :CentOS 7.6.1810
    •      IDE :Eclipse 2019‑03
    • typesetting :Markdown

    code

    package per.jizuiku.base;
    
    import java.util.Scanner;
    
    /**
     * @author 给最苦
     * @date 2019/06/29
     * @blog www.cnblogs.com/jizuiku
     */
    class Demo {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
    
            Scanner sc = new Scanner(System.in);
    
            String myStr = sc.nextLine();
    
            System.out.println("resut:" + myStr);
    
            sc.close();
        }
    }
    

    result

    hello world
    resut:hello world
    
    

    sourceCode

    /**
        * 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.
        *
        * <p>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.
        *
        * @return the line that was skipped
        * @throws NoSuchElementException if no line was found
        * @throws IllegalStateException if this scanner is closed
        */
    public String nextLine() {
        modCount++;
        if (hasNextPattern == linePattern())
            return getCachedResult();
        clearCaches();
    
        String result = findWithinHorizon(linePattern, 0);
        if (result == null)
            throw new NoSuchElementException("No line found");
        MatchResult mr = this.match();
        String lineSep = mr.group(1);
        if (lineSep != null)
            result = result.substring(0, result.length() - lineSep.length());
        if (result == null)
            throw new NoSuchElementException();
        else
            return result;
    }
    

    resource

    • [ JDK ] openjdk.java.net
    • [ doc - 参考 ] docs.oracle.com/en/java/javase/11
    • [ 规范 - 推荐 ] yq.aliyun.com/articles/69327
    • [ 规范 - 推荐 ] google.github.io/styleguide
    • [ 源码 ] hg.openjdk.java.net
    • [ OS ] www.centos.org
    • [ IDE ] www.eclipse.org/downloads/packages
    • [ 平台 ] www.cnblogs.com


    感谢帮助过 给最苦 的人们。
    Java、Groovy和Scala等基于JVM的语言,优秀,值得学习。
    规范的命名和代码格式等,有助于沟通和理解。
    JVM的配置、监控与优化,比较实用,值得学习。

  • 相关阅读:
    css实现强制不换行/自动换行/强制换行
    提供一个跨浏览器的XML DOM对象解决方案,来自于《javascript高级程序设计》
    Javascript的IE和Firefox兼容性汇编收藏.txt
    弹出一个层来让用户确认操作(转)
    弹窗代码
    C# 绘制统计图(柱状图, 折线图, 扇形图) (转)
    把Sql类型转换为C#类型的函数
    类型对象和类实例对象
    javascript 父窗体获取子窗体操作结果
    类型转换(二):是使用 is 还是 as
  • 原文地址:https://www.cnblogs.com/jizuiku/p/11107752.html
Copyright © 2011-2022 走看看