将trim()和IsEmpty()实现为代码

时间:2015-10-14 07:57:16

标签: java

我有这段代码:

package rr.fr.oo.lab.proc1;
import java.util.Scanner;

public class Rectangle {

    public static void main(String[] args) {

        if(args.length != 2 && args.length != 0){
            System.err.println("Invalid number of arguments was provided.");
            System.exit(1);


        double a = Double.parseDouble(args[0]);
        double b = Double.parseDouble(args[1]);

        double area = area(a,b);
        double perimeter = perimeter(a,b);

        System.out.println("You have specified a rectangle of width " + a + " and height "
                + b + ". Its area is " + area + " and its perimeter is " + perimeter);
        }



        double x,y;
        if(args.length == 0){
            Scanner sc = new Scanner(System.in);
            System.out.printf("Please provide width: ");
            x = scanner(sc);
            while(x < 0){
                System.out.printf("The width must not be negative.\n");
                System.out.printf("Please provide width: ");
                x = scanner(sc);
            }
            System.out.printf("Please provide height: ");
            y = scanner(sc);
            while(y < 0){
                System.out.printf("The width must not be negative.\n");
                System.out.printf("Please provide height: ");
                y = scanner(sc);
            }
            sc.close();
            double area = area(x,y);
            double perimeter = perimeter(x,y);
            System.out.println("You have specified a rectangle of width " + x + " and height "
                    + y + ". Its area is " + area + " and its perimeter is " + perimeter);
        }   
    }



    private static double area(double a, double b){
        return a*b;
    }


    private static double perimeter(double a, double b){
            return 2*(a+b);
    }


    private static double scanner(Scanner sc){
        double number = sc.nextDouble();
        return number;
    }

}

我想知道如何使用trim和isEmpty方法来改善从系统输入读取的结果。 读完一行之后,我想清除所有带修剪的空白。如果line为空,我想打印如下信息:“输入不能为空”。

1 个答案:

答案 0 :(得分:0)

你可以自己全力以赴,这样做:

private static double readDouble( BufferedReader kbd, String prompt ){
    String line = null;
    double res = 0;
    while( true ){
        System.out.print( prompt + " " );
        try {
            line = kbd.readLine();
        } catch( IOException ioe ){
            System.out.println( ioe.getMessage() );
            throw new IllegalStateException();
        }
        if( line == null ){
            System.out.println( "input terminated" );
            continue;
        }
        line = line.trim();
        if( line.length() == 0 ){
            System.out.println( "input line can't be blank" );
            continue;
        }
        try {
            res = Double.parseDouble( line );
        } catch( NumberFormatException nfe ){
            System.out.println( "error in number" );
            continue;
        }
        if( res < 0 ){
            System.out.println( "input must not be negative" );
            continue;
        }
        return res;
    }
}

但使用扫描仪要方便得多。捕捉一些输入事件,如空行或ctrl-D(EOF)通常不值得。