(六).Scanner类

Scanner类用来文件及键盘获得输入,这里先介绍键盘获得输入。

1).为了使用键盘获得输入,需要在代码的开头声明:

import java.util.Scanner;

2). 建立对象

Scanner Scanner_Object_Name =new Scanner(System.in);

3). 使用对象的方法读取和返回通过键盘输入的各种数据。(要读取的值应当用空白符隔开(默认空白符),读取值时忽略这些空白符)

1.依次读取返回各种数据。

Scanner_Object_Name.nextInt()

Scanner_Object_Name.nextDouble()

Scanner_Object_Name.nextLong()

Scanner_Object_Name.nextByte()

Scanner_Object_Name.nextShort()

Scanner_Object_Name.nextFloat()

Scanner_Object_Name.nextBoolean()
通过键盘输入一个Boolean值。true和false允许大小写的任意组合。

2.每个上述方法都有一个public boolean hasType_Name(),如果下一个标记存在相对应的Type_Name数据,则返回true,否则返回false。
该方法用于文本文件输入时判断输入是否合法。

3.Scanner_Object_Name.next()

返回由下一个键盘字符一直到第一个分隔符(但不包括该字母)间的文本组成String。

4.Scanner_Object_Name.nextLine()

读取当前的键盘输入行上全部内容,并返回作为String类型的值读取的字符。注意:行终止符’\n’被读取,但是被丢弃;返回的字符串中不包含该字符。

***上述读取数据的方法如果Scanner流被关闭了,则抛出IllegalSateException异常。
如果读取的数据与所要求的不一致,则抛出NoSuchElementException异常。

5.修改键盘输入的分隔符 useDelimiter()方法

Scanner_Object_Name.useDelimiter("##”);

在调用完这个方法之后,##将成为该对象唯一的输入分隔符。空格和换行符不再是该对象的分隔符。

例子一:



import  java.util.Scanner;

 

/**

 *  
Scanner类的应用

 *

 */

public class ScannerDemo

{

    public static void main(String[] args)

    {

        int n1,n2;

        

        Scanner scannerObject =new Scanner(System.in);
                                

        System.out.println("Enter two number");
       
        System.out.println("seperate by one or more space");
       
        n1= scannerObject.nextInt();
        
        n2= scannerObject.nextInt();

       
        System.out.println("You entered "+n1+"and "+n2);
                       
        
        System.out.println("Next enter two words");
        

        String word1=scannerObject.next();
        

        String word2=scannerObject.next();
        

        System.out.println("You entered "+word1+"and"+word2);
                       

        String junk=scannerObject.nextLine();                            // 除去输入流中色换行符

        
        System.out.println("Next enter a line text");
       

        String line =scannerObject.nextLine();
        
        System.out.println("You entered "+line);
       

        scannerObject.close();                                       //关闭输入流

    }

    
}

============================
运行结果:

Enter two number

separate by one or more space

12 13

You entered 12and 13

Next enter two words

hello hi

You entered helloandhi

Next enter a line text

hello world

You entered hello world

例子二:





import java.util.Scanner;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

/**

 * @author 爱哭的周小姐

 *     用hasNextInt查看文本文件的结束

 *

 */

public class HasNextIntDemo 

{

    public static void main(String[] args)

    {

        Scanner inputStream =null;
        
        try

        {

            inputStream =new Scanner(new FileInputStream("data.txt"));
            
        }

        catch(FileNotFoundException e)

        {

            System.exit(0);
            
        }

        
        int next,sum=0;
        
        while(inputStream.hasNextInt())

        {

            next=inputStream.nextInt();
            
            sum+=next;            

        }
        

        inputStream.close();
        

        System.out.println("The sum of the number is"+sum);

        
    }

 
}


5). 处理Scanner类的异常

1).我们一直使用Scanner类的nextInt方法从键盘读取int值,若用户输入错误,则程序抛出一个InputMismatchException类型的异常,如果程序没有捕捉异常,程序结束运行并发出一则错误消息。在实际操作中我们可以捕获该异常,并让用户重复输入。

2).InputMismatchException类是RuntimeException类的派生类,因此不需要在catch语句中捕获该异常,也不必用throws子句声明。

3).InputMismatchException类包含在Java.util中。因此在使用时应手动导入:

import java.util.InputMismatchException;



import java.util.InputMismatchException;

import java.util.Scanner;

 
 

/**

 * 异常控制循环输入

 *

 */

 

public class InputMismatchExceptionDemo

{

    public static void main(String[] args)

    {

        Scanner keyboard =new Scanner(System.in);
        
        int number=0;

        boolean a=false;
        

        while(!a)

        {

            try

            {

                System.out.println("输入一个正整数:");
                
                number=keyboard.nextInt();
               
                a=true;
               
            }

          
            catch(InputMismatchException e)

            {

                keyboard.nextLine();

              
                System.out.println("输出错误!请重新输入:");
                             
            }
                       

        }

        
        System.out.println("你输入的正整数为:"+number);
        
        keyboard.close();

    }
 
}


运行结果:

=====================

输入一个正整数:

j

输出错误!请重新输入:

输入一个正整数:

8

你输入的正整数为:8

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值