公共静态方法返回问题

时间:2016-02-23 02:35:40

标签: java if-statement static-methods do-while

我创建了一个用户添加值的text.file。这是用户数据被读取和验证的部分。我的斗争是,当用户在提示时添加值时,它不会返回任何数据。它显示为零。我不能在某处返回值。

以下是本节的说明。

“询问用户是否还有更多的金额要添加到文本文件中.如果: 使用静态方法读取和验证金额类型和金额 使用实例方法将金额添加到总计之一“

这是我到目前为止所拥有的。感谢您的任何指导和帮助。

public static char readValidateAmountType()
{ 
    System.out.println("Enter the amount type(T, D, E) you would like to enter: ");
    Scanner keyboard = new Scanner(System.in);
        char amountType = keyboard.next().charAt(0);
        amountType = Character.toUpperCase(amountType);
            do
            {
                if(amountType !='T' && amountType !='D' && amountType !='E') 
                {

                    System.out.println("Invalid amount type!"); 
                    System.out.println("Enter the amount type(T, D, E) you would like to enter: ");
                    amountType = keyboard.next().charAt(0);
                    amountType = Character.toUpperCase(amountType);
                }
                else
                {
                    return amountType;
                }    
            }
            while(amountType !='T' && amountType !='D' && amountType !='E');
            return amountType;    
}

public static double readValidateAmount()
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please enter a dollar amount: ");
    amount = keyboard.nextDouble();
    if(amount <=0)
    {
        do
        {       
            System.out.println("Invalid amount!");
            System.out.println("Please enter a dollar amount: ");
            amount = keyboard.nextDouble();
        }
        while(amount <=0);
        return amount;
    }
    else
    {
        return amount;
    }

主要:

  public static void main (String[] args) throws IOException
{
    HenwoodEventClass event = new HenwoodEventClass();
    System.out.println("This program will tell you the financial details of"
            + " your event. \nAnd allow you to change or add input.\n\n");

    String userFile = "Event.txt";
    try 
    {
        File inputFile = new File (userFile);

        Scanner CheckFile = new Scanner (inputFile);

        if(CheckFile.hasNextLine())
        {
            while (CheckFile.hasNextLine())
            {
                try
                {
                    System.out.println(CheckFile.nextLine());    
                }
                catch (IllegalArgumentException exception)
                {   
                    System.out.println("Does not exist!");
                }   
            }
            CheckFile.close();    
            event.gettotalTicketSales(); 
            event.gettotalMoneyDonated(); 
            event.gettotalExpenses();
        }
            System.out.println("Are ther anymore amounts to add that were not in the textfile?: ");
            Scanner keyboard = new Scanner(System.in);
            char choice = keyboard.next().charAt(0);
            choice = Character.toUpperCase(choice);

                do
                {
                    if(choice == 'Y')
                    {
                        readValidateAmountType();
                        readValidateAmount();

                        System.out.println("Are ther anymore amounts to add that were not in the textfile?: ");
                        choice = keyboard.next().charAt(0);
                        choice = Character.toUpperCase(choice);
                    }
                    event.displayFinalResults();
                }
                while(choice =='Y');        
    }
    catch (FileNotFoundException exception) 
    {   
        System.out.println("File "+ userFile +" does not exist!\n\n");  
    }
}

}

1 个答案:

答案 0 :(得分:1)

尝试使用Double.parseDouble(keyboard.nextLine());代替nextDouble(),并将next()替换为nextLine()

请参阅:By default, a scanner uses white space to separate tokens.