Java IO

这里记录JAVA输入相关的知识,后续会不定期更新。


20180306
介绍scanner 和 console

1.Scanner
scanner输入很简单,这里直接贴代码

import java.util.*;
import java.io.*;

public class InputTest{

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

        System.out.println("What is your name? ");
        String name = in.nextLine();

        System.out.println("What is your age?");
        int age = in.nextInt();

        System.out.println("Your name : " + name);
        System.out.println("You will be " + (age + 1) + " next year!");

    }

}

}

小结:
关于Scanner

  1. Scanner(InputStream in)
    用给定的输入流创建一个Scanner对象

  2. String nextLine()
    读取输入的下一行内容

  3. String next()
    读取输入的下一个单词(yi以空格作为分隔符)

  4. int nextInt()

  5. double nextDouble()
    读取并转换下一个表示整数或浮点数的字符序列

  6. boolean hasNextInt()

  7. boolouble()
    检测是否还有表示整数或浮是否还有表示整数或浮点数的下一个字符序列

2.关于Console
首先贴代码

import java.util.*;
import java.io.*;

public class InputTest{

    public static void main(String[] args)
    {
        Console cons = System.console();
        System.out.print("Please input your username:");
        String username = cons.readLine();
        System.out.println("Please input your password:");
        char[] psw = cons.readPassword();
        System.out.println("Username:" + username);
        System.out.println("Password:" + psw);

        System.out.println("Anothe way input password.");
        char[] psw2 = cons.readPassword("Password:");
        System.out.println("Username:" + username);
        System.out.println("Password:" + Arrays.toString(psw2));

        System.out.println("Third way.");
        String psw3 = new String(cons.readPassword("Password:"));
        System.out.println("Username:" + username);
        System.out.println("Password:" + psw3);

    }
}

小结:

  1. console输入只能是控制台,不能是IDE
    2.密码保存在数组之中,因此打印时要进行转换,可采用Arrays.toString(arr)或Arrays.deepToString(arr)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值