java 不支持的操作异常

java Unsupported Operation Exception

提问人:user3047713 提问时间:4/1/2014 最后编辑:The Guy with The Hatuser3047713 更新时间:4/2/2014 访问量:5955

问:

我正在尝试为我的代码创建三个不同的类:、 和 。我正在按照说明进行操作,但是,当我运行代码时,我不断收到以下错误,并且不知道如何修复它。FutureValueAppValidatorFinancialCalculations

==============================================================================
Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
    at FutureValueApp.getDoubleWithinRange(FutureValueApp.java:64)
    at FutureValueApp.main(FutureValueApp.java:17)

Java Result: 1

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

这是我到目前为止的代码:

import java.util.*;
import java.text.*;

import java.util.Scanner;
public class FutureValueApp
{
    public static void main(String[] args)
    {
        System.out.println("Welcome to the Future Value Calculator\n");

        Scanner sc = new Scanner(System.in);
        String choice = "y";
        while (choice.equalsIgnoreCase("y"))
        {
        // get the input from the user
            System.out.println("DATA ENTRY");
            double monthlyInvestment = getDoubleWithinRange(sc,
                "Enter monthly investment: ", 0, 1000);
            double interestRate = getDoubleWithinRange(sc,
                "Enter yearly interest rate: ", 0, 30);
            int years = getIntWithinRange(sc,
                "Enter number of years: ", 0, 100);

        // calculate the future value
            double monthlyInterestRate = interestRate/12/100;
            int months = years * 12;
            double futureValue = calculateFutureValue(
                monthlyInvestment, monthlyInterestRate, months);

        // get the currency and percent formatters
            NumberFormat currency = NumberFormat.getCurrencyInstance();
            NumberFormat percent = NumberFormat.getPercentInstance();
            percent.setMinimumFractionDigits(1);

        // format the result as a single string
            String results =
            "Monthly investment:\t"
            + currency.format(monthlyInvestment) + "\n"
            + "Yearly interest rate:\t"
            + percent.format(interestRate/100) + "\n"
            + "Number of years:\t"
            +  years + "\n"
            + "Future value:\t\t"
            + currency.format(futureValue) + "\n";

        // print the results
            System.out.println();
            System.out.println("FORMATTED RESULTS");
            System.out.println(results);

        // see if the user wants to continue
            System.out.print("Continue? (y/n): ");
            choice = sc.next();
            sc.nextLine();  // discard any other data entered on the line
            System.out.println();
        }
    }

    private static int getIntWithinRange(Scanner sc, String enter_number_of_years_, int i, int i0) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private static double getDoubleWithinRange(Scanner sc, String enter_monthly_investment_, int i, int i0) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private static double calculateFutureValue(double monthlyInvestment, double monthlyInterestRate, int months) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

Validator 类

import java.util.Scanner;

public class Validator 
{
    public static double getDouble(Scanner sc, String prompt)
    {
        double d = 0.0;
        boolean isValid = false;
        while (isValid == false)
        {
            System.out.print(prompt);
            if (sc.hasNextDouble())
            {
                d = sc.nextDouble();
                isValid = true;
            }
            else
            {
                System.out.println("Error! Invalid decimal value. Try again.");
            }
            sc.nextLine();  // discard any other data entered on the line
        }
        return d;
    } 

    public static double getDoubleWithinRange(Scanner sc, String prompt,
        double min, double max)
    {
        double d = 0.0;
        boolean isValid = false;
        while (isValid == false)
        {
            d = getDouble(sc, prompt);
            if (d <= min)
                System.out.println(
                    "Error! Number must be greater than " + min + ".");
            else if (d >= max)
                System.out.println(
                    "Error! Number must be less than " + max + ".");
            else
                isValid = true;
        }
        return d;
    }

    public static int getInt(Scanner sc, String prompt)
    {
        int i = 0;
        boolean isValid = false;
        while (isValid == false)
        {
            System.out.print(prompt);
            if (sc.hasNextInt())
            {
                i = sc.nextInt();
                isValid = true;
            }
            else
            {
                System.out.println("Error! Invalid integer value. Try again.");
            }
            sc.nextLine();  // discard any other data entered on the line
        }
        return i;
    }

    public static int getIntWithinRange(Scanner sc, String prompt,
        int min, int max)
    {
        int i = 0;
        boolean isValid = false;
        while (isValid == false)
        {
            i = getInt(sc, prompt);
            if (i <= min)
                System.out.println(
                    "Error! Number must be greater than " + min + ".");
            else if (i >= max)
                System.out.println(
                    "Error! Number must be less than " + max + ".");
            else
                isValid = true;
        }
        return i;
    }  
}

FinancialCalculations 类

public class FinancialCalculations 
{

    public static double calculateFutureValue(double monthlyInvestment,
        double monthlyInterestRate, int months)
    {
        double futureValue = 0;
        for (int i = 1; i <= months; i++)
        {
            futureValue =
            (futureValue + monthlyInvestment) *
            (1 + monthlyInterestRate);
        }
        return futureValue;
    }  
}

我知道为了使代码正常工作,不应包含“Private UnsupportedOperationException”,并且我需要将其公开,但这太令人困惑了。

Java 验证

评论


答:

2赞 WeMakeSoftware 4/1/2014 #1

缺少该方法的实现

private static double getDoubleWithinRange(Scanner sc, String enter_monthly_investment_, int i, int i0) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

只要实施它,你就会没事的

0赞 Arash Saidi 4/1/2014 #2

如前所述,您尚未实现三种方法,它们会自动引发异常。

private static int getIntWithinRange(Scanner sc, String enter_number_of_years_, int i, int i0) {
    return 0;
}

private static double getDoubleWithinRange(Scanner sc, String enter_monthly_investment_, int i, int i0) {
    return 0;
}

private static double calculateFutureValue(double monthlyInvestment, double monthlyInterestRate, int months) {
    return 0;
}

为了测试您的代码,您可以执行我上面所做的操作,只需设置一个返回值,您的控制台就会打印出来:

欢迎使用未来价值计算器

数据录入

格式化的结果 每月投资: NOK 0.00 年利息 税率:0.0% 年数:0 未来价值:0.00 挪威克朗

继续?(是/否):

评论

0赞 Lajos Arpad 4/2/2014
没有冒犯,但他已经实施了它们。两次。问题在于,正如 JonK 正确指出的那样,使用了方法的错误版本,它们会抛出异常。
0赞 Arash Saidi 4/2/2014
没有采取,但这些是他所说的方法,我只是指的是那个。他没有两次实现相同的方法,但是有两个类的方法具有相同的名称,这与相同的方法不同。:-)
1赞 JonK 4/2/2014 #3

您调用了两个方法(和也是如此)。你的问题是你叫错了人getDoubleWithinRangegetIntWithinRangecalculateFutureValue

这一行:

double monthlyInvestment = getDoubleWithinRange(sc,
                "Enter monthly investment: ", 0, 1000);

正在调用类的方法,该方法只能抛出一个.它之所以这样做,是因为您尚未使用所属类限定方法调用。staticgetDoubleWithinRangeFutureValueAppUnsupportedOperationException

你要调用的方法是类中的那个,你需要像这样调用它:Validator

double monthlyInvestment = Validator.getDoubleWithinRange(sc,
                "Enter monthly investment: ", 0, 1000);

同样,该行:

int years = getIntWithinRange(sc, "Enter number of years: ", 0, 100);

需要:

int years = Validator.getIntWithinRange(sc, "Enter number of years: ", 0, 100);

和:

double futureValue = calculateFutureValue(
                monthlyInvestment, monthlyInterestRate, months);

需要成为:

double futureValue = FinancialCalculations.calculateFutureValue(
                monthlyInvestment, monthlyInterestRate, months);

每当你处理方法时,最好总是用拥有类来限定调用 - 如果你这样做了,在这种情况下,你将能够立即发现问题。static

从 中删除多余的方法也是一个好主意。FutureValueApp