根据文件

时间:2016-02-22 19:30:47

标签: java

我无法让我的程序在我的IDE中显示没有错误消息。我遇到的两个问题是我试图从文件中读取信息,在实例方法中使用该信息来更新值(总和数字),然后使用getter返回总数。我有错误的"不兼容的类型,可能有损转换从double到char"在while循环中,我尝试调用我的实例方法updateValues。我得到"非静态方法getTicketSalesIncome()不能从静态上下文中引用"当我尝试使用吸气剂打印总数时。我有一种感觉,我需要做一些事情,比如以某种方式使用文件对象,但我不知道如何纠正它。我想我的代码可能还有其他问题,但这些是我需要帮助才能继续前进的两个主要问题。

第一个文件:

import java.util.Scanner;
import java.io.*;

public class KingEventManager {   
    public static char getAmountType() {
        Scanner keyboard = new Scanner(System.in);
        char amountType;    
    do {
        System.out.print("Enter the amount type (T, D, or E): ");
        amountType = keyboard.next().charAt(0);
        amountType = Character.toUpperCase(amountType);        
        } while (amountType != 'T'||amountType != 'D' || amountType != 'E' );
        return amountType;
   }

   public static double getAmount() { 
       Scanner keyboard = new Scanner(System.in);
       double amount;
       do { 
           System.out.print("Enter the amount value: ");
           amount = keyboard.nextDouble();
       } while (amount <= 0);
       return amount;
  }

  public static void main(String[] args)  {
       System.out.println("This program reads data about expenses, ticket sales"
                        + ", and donations from a data file.");

       String fileName = "Event.txt";
       File file = new File(fileName);
       char amountType;
       double amount;     
       try {
            Scanner scanFile = new Scanner(file);      
            while (scanFile.hasNext() ) {           
                KingEventClass.updateValues(amount, amountType);
                amountType = scanFile.next().charAt(0);
                amount = scanFile.nextDouble();           
            }
            System.out.println("Total ticket sales : " +                         KingEventClass.getTicketSalesIncome() );
            scanFile.close();       
       } catch (FileNotFoundException fnfe) {
            System.err.println("Failed to open file '" + fileName + ".");
       }
   }       
}

第二档:

public class KingEventClass {   
    // data fields
    private double ticketSalesIncome;
    private double totalDonationAmount;
    private double totalEventExpenses;

    // object constructor
    public KingEventClass() {
         ticketSalesIncome = 0;
         totalDonationAmount = 0;
         totalEventExpenses = 0;
    }

    //accessors
    public double getTicketSalesIncome() {
        return ticketSalesIncome; 
    }
    public double getTotalDonationAmount() {
        return totalDonationAmount; 
    }
    public double getTotalEventExpenses() {
        return totalEventExpenses; 
    }


/**instance method
 * 
 * @param amountType
 * @param amount
 * @return 
 */
  public double updateValues(char amountType, double amount) {
        if (amount < 0) {
            throw new IllegalArgumentException("*** The amount entered must be"
                                             + "greater than 0");
        }
        else if (amountType != 'T' || amountType != 'D' || amountType != 'E') {
            throw new IllegalArgumentException("**The amount type entered must"
                                             + "be 't', 'd', or 'e' ");
        }
        else { switch (amountType) {
                case 'T':
                    ticketSalesIncome = ticketSalesIncome + amount;
                    return ticketSalesIncome;
                case 'D':
                    totalDonationAmount = totalDonationAmount + amount;
                    return totalDonationAmount;
                default:
                    totalEventExpenses = totalEventExpenses + amount;
                    return totalEventExpenses;
            }        
        }
    }
    public void displayFinalResults() {
        double totalIncome = ticketSalesIncome + totalDonationAmount;
        double eventProfits = totalIncome - totalEventExpenses;
        System.out.println("Event Overall Outcome: ");
        System.out.printf("    Total Ticket Sales:   %.2f\n",
                          ticketSalesIncome);
        System.out.printf("    Total donations:      %.2f" + " +\n",
                          totalDonationAmount);
        System.out.println("                        --------");
        System.out.printf("    Total income:         %.2f\n", totalIncome);
        System.out.printf("    Total expenses:       %.2f\n", 
                          totalEventExpenses);
        System.out.println("                        --------");
        System.out.printf("Event profits:            %.2f\n", eventProfits);

    }        
}

1 个答案:

答案 0 :(得分:0)

我看到的第一个问题是从静态方法调用非静态方法。您的Java main被列为静态(主要方法在Java中必须是静态的,因此我们知道我们无法更改它。)KingEventClass中的方法updateValues()是非静态的。这意味着当Java main尝试调用updateValues()方法时,它有可能不存在!

更改

public double updateValues(char amountType, double amount)

public static double updateValues(char amountType, double amount)

或者在while循环中调用updateValues之前创建一个KingEventClass实例。

KingEventClass kingEventClass = new KingEventClass();
kingEventClass.updateValues(your, values);

这将确保Java main调用它时该方法存在。

至于你的问题是双重问题......你能否详细描述哪一行特定于错误发生?

:: EDIT :: 有损转换问题是因为您在方法调用中混淆了参数。从你的Java main,你传入updateValues(double,char),但在你的KingEventClass.updatevalues中,Java期待(char,double)。在这种情况下,顺序很重要,所以只需切换其中一个!