java:Scanner实现文本输入

1.包:

import Java.util.Scanner

2.使用方法:

Scanner reader=new Scanner(System.in);    

然后reader对象调用下列方法(函数),读取用户在命令行输入的各种数据类型:

nextByte(),

nextDouble(),

nextFloat(),

nextInt(),

nextLine(),

nextLong(),

nextShort()    

注:上面由next()方法转化而来,空格,TAB键结束

上述方法执行时都会造成堵塞,等待用户在命令行输入数据回车确认.

例如,用户在键盘输入  

12.34,

hasNextFloat()的值是true,而hasNextInt()的值是false. NextLine()等待用户输入一个文  

本行并且回车,该方法得到一个String类型的数据。相比nextLine()回车确认,按照行读为string

3.实例

  1. //逐行扫描文件,并逐行输出  
  2. public static void main(String[] args) throws FileNotFoundException {   
  3.     InputStream in = new FileInputStream(new File("C:\\AutoSubmit.java"));   
  4.     Scanner s = new Scanner(in);   
  5.     while(s.hasNextLine()){   
  6.             System.out.println(s.nextLine());   
  7.     }   
  8. }  

 

  1. //all out  
  2. import java.util.Scanner;  
  3.   
  4. public class testNextline {   
  5.         public static void main(String[] args) {   
  6.                 Scanner s = new Scanner(System.in);   
  7.                 System.out.println("请输入字符串:");   
  8.                 while (true) {   
  9.                         String line = s.nextLine();   
  10.                         if (line.equals("exit")) break;   
  11.                         System.out.println(">>>" + line);   
  12.                 }   
  13.         }   
  14. }  

 

  1. //next(), <span style="font-size:18px;">nextByte(),nextDouble(),nextFloat,nextInt(),nextLine(),nextLong(),nextShort()  //用法类似</span>  
  2. import java.util.Scanner;  
  3.   
  4. public class hasNextInt {  
  5.   
  6.     public static void main(String[] args) {  
  7.         Scanner in =  new Scanner(System.in);  
  8.         System.out.println("请输入一个整数");  
  9.         while(in.hasNextInt()){  
  10.             int num = in.nextInt();  
  11.             System.out.println("数字"+num);//输入123 12只能读到123  
  12.             System.out.println("请输入一个字符串");  
  13.             String str = in.next();//输入 adc cv只能读到adc  
  14.              
  15.             System.out.println("字符串"+str);  
  16.         }  
  17.     }  
  18. }  


4.其他相关方法

下面这几个相对实用:

delimiter() :返回此 Scanner 当前正在用于匹配分隔符的 Pattern。

  1.  public static void main(String[] args) throws FileNotFoundException {   
  2.                 Scanner s = new Scanner("123 asda bf 12 123 nh l,sf.fl ...adafafa    lda");   
  3. //                s.useDelimiter(" |,|\\.");   
  4.                 while (s.hasNext()) {   
  5.                         System.out.println(s.next());   
  6.                 }   
  7.         }  

 

  1. 123  
  2. asda  
  3. bf  
  4. 12  
  5. 123  
  6. nh  
  7. l,sf.fl  
  8. ...adafafa  
  9. lda  


hasNext()          判断扫描器中当前扫描位置后是否还存在下一段。(原APIDoc的注释很扯淡)
hasNextLine()   如果在此扫描器的输入中存在另一行,则返回 true。
next()               查找并返回来自此扫描器的下一个完整标记(String)。
nextLine()         此扫描器执行当前行,并返回跳过的输入信息。

5. 一个读写实例

  1. import java.util.Scanner;  
  2. public class test{  
  3.     public static int getCount(String str,char c){  
  4.         int count = 0;  
  5.         if(str != null && str.length() > 0){  
  6.             for(int i = 0;i < str.length();i++){  
  7.                 if(c == str.charAt(i)){  
  8.                     count++;  
  9.                 }  
  10.             }  
  11.         }else{  
  12.             count = 0;  
  13.         }  
  14.         return count;  
  15.     }  
  16.        
  17.     public static void main(String[] args){  
  18.            Scanner s = new Scanner(System.in);  
  19.             String str = s.next();  
  20.             char c = s.next().charAt(0);  
  21.             int i = getCount(str,c);  
  22.             System.out.println(i);  
  23.     }  
  24. }  

转载于:https://my.oschina.net/u/3624220/blog/1518183

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值