从Scanner输入Java接收浮动 [英] Receiving float from Scanner input Java

查看:206
本文介绍了从Scanner输入Java接收浮动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个方法来检查用户的输入是否是一个浮点数,如果是字符串或整型,它应该抛出一个异常。



我在这个方法之外声明了扫描器:

pre code> Scanner sc =新的扫描仪(System.in);

而方法定义是:

<$ p $($)$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'
}
else {
float input = sc.nextFloat();
if(input%1 == 0){
throw new MyException(Wrong type);
}
else返回true;




$ b $ p
$ b

问题是无论如何抛出异常用户输入,所以我的问题是:我究竟做错了什么?



我知道在Java中,像1.2这样的输入被解释为double,但是如何获取从控制台的浮动呢?或者我误解了方法hasNextFloat()或整个扫描仪的工作?



到目前为止我还没有找到有用的东西

nextFloat(),所以您必须确保输入一个浮点数,否则清除扫描程序与 next()

  public static void main(String [] args)抛出Exception {
while(true){
System.out.print(Enter a float:);
尝试{
float myFloat = input.nextFloat();
if(myFloat%1 == 0){
throw new Exception(Wrong type);
}
System.out.println(myFloat);
catch(InputMismatchException ime){
System.out.println(ime.toString());
input.next(); //从所有数据



code


$ b

结果:



UPDATE



您仍然必须处理InputMismatchException,只是在catch块中抛出自己的异常。 ($ {

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

// while(true)仅用于测试
while(true){
try {
System.out.print(Enter a float:);
System.out.println(CheckFloat(input));
} catch(MyException me){
System.out.println(me.toString());



$ b private static float CheckFloat(Scanner sc)throws MyException {
try {
float input = sc.nextFloat ();
if(input%1 == 0){
throw new MyException(Wrong type);
} else {
return input;

catch(InputMismatchException ime){
sc.next(); //刷新扫描器

//重新创建自己的异常
抛出new MyException(Wrong type);



私有静态类MyException扩展异常{
//您异常详细信息
public MyException(String message){
super (信息);




$结果:


I need a method that should check whether user's input is a float, and if it is string or int it should throw an exception.

I declare the Scanner outside of the method:

    Scanner sc = new Scanner(System.in);

And the method definition is:

private boolean CheckFloat(Scanner sc) throws MyException {
    if(!sc.hasNextFloat()){
        throw new MyException("Wrong type");
    }
    else {
        float input = sc.nextFloat();
        if(input%1==0) {
            throw new MyException("Wrong type");
        }
        else return true;
    }
}

The problem is that the exception is thrown no matter what the user types in, so my question is: what exactly do I do wrong?

I know that in Java an input like 1.2 is interpreted as double, but how to take a float from the console then? Or do I misunderstand working of the method hasNextFloat() or the whole Scanner?

I haven't found anything helpful so far

解决方案

Since you're using nextFloat() you must be sure that you enter a floating number, otherwise clear the scanner with next()

public static void main(String[] args) throws Exception {
    while (true) {
        System.out.print("Enter a float: ");
        try {
            float myFloat = input.nextFloat();
            if (myFloat % 1 == 0) {
                throw new Exception("Wrong type");
            }
            System.out.println(myFloat);
        } catch (InputMismatchException ime) {
            System.out.println(ime.toString());
            input.next(); // Flush the buffer from all data
        }
    }
}

Results:

UPDATE

You still have to handle the InputMismatchException, just throw your own exception in the catch block.

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

    // while (true) just for testing
    while (true) {
        try {
            System.out.print("Enter a float: ");
            System.out.println(CheckFloat(input));
        } catch (MyException me) {
            System.out.println(me.toString());
        }
    }
}

private static float CheckFloat(Scanner sc) throws MyException {
    try {
        float input = sc.nextFloat();
        if (input % 1 == 0) {
            throw new MyException("Wrong type");
        } else {
            return input;
        }
    } catch (InputMismatchException ime) {
        sc.next(); // Flush the scanner

        // Rethrow your own exception
        throw new MyException("Wrong type");
    }
}

private static class MyException extends Exception {
    // You exception details
    public MyException(String message) {
        super(message);
    }
}

Results:

这篇关于从Scanner输入Java接收浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆