编译器说我试图将字符转换为字符串,但我不是

时间:2018-04-28 01:36:45

标签: java

首先,让我先说一下,在Java编码方面,我是一个新手。我正在学习编程入门课程,到目前为止它已经完全踢了我的屁股。

我有一个奇怪的问题。我正在使用一个名为zyBook.com的网站上的在线教科书。当我提交我的程序时,我得到70/100,并且两个自动测试失败。我收到此错误消息:

zyLabsUnitTest.java:6:错误:不兼容的类型:char无法转换为String        event.addAmount('T',10);                        ^ zyLabsUnitTest.java:7:错误:不兼容的类型:char无法转换为String        event.addAmount('D',20);                        ^ zyLabsUnitTest.java:8:错误:不兼容的类型:char无法转换为String        event.addAmount('E',30);

注意:有些消息已经简化;使用-Xdiags重新编译:详细以获得完整输出

3个错误

我收到此错误消息:

zyLabsUnitTest.java:6:错误:不兼容的类型:char无法转换为String        event.addAmount('T',100);                        ^ zyLabsUnitTest.java:7:错误:不兼容的类型:char无法转换为String        event.addAmount('D',200);                        ^ zyLabsUnitTest.java:8:错误:不兼容的类型:char无法转换为String        event.addAmount('E',160);                        ^ 注意:某些消息已经简化;使用-Xdiags重新编译:详细以获得完整输出

3个错误

好吧,也许它只是我正在使用的在线教科书,但我无法看到我在代码中的任何地方将char转换为字符串......

这是我的代码中的第一个类:

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

public class EventManager {

public static void main (String[] args) {

   Scanner keyboard = new Scanner(System.in);
   String fileName;
   File inFile = null;
   Scanner eventFile = null;

   String amountType;
   double amountValue;


   Event event = null;

   System.out.println("Enter filename:");
   fileName = keyboard.next();

   //keyboard.next();

   try {

       inFile = new File(fileName);

       eventFile = new Scanner(inFile);

       event = new Event();

       while(eventFile.hasNext()) {
           amountType = eventFile.next();
           amountValue = eventFile.nextDouble();

           System.out.printf("Line read: %s %4.2f\n", amountType, amountValue);

           event.addAmount(amountType, amountValue);
       }

       System.out.println("Done reading file " + inFile);
       System.out.println();


       event.displayTotals();
       double netsales = event.calcEventProfit();

       if(netsales > 0) {
           System.out.println("Event generated a profit of $ "+ netsales);
       }
       else {
           System.out.print("Event generated a loss of $ ");
           System.out.printf("%.2f\n", netsales);
       }

   }

这是我的代码中的第二个类:

public class Event {
private double ticketSales;
private double donations;
private double expenses;

public Event() {
    ticketSales = 0;
    donations = 0;
    expenses = 0;
}


public void addAmount(String type, double amount){
    if(type.equals("T")) {
        ticketSales += amount;
    }
    else if(type.equals("D"))
        donations += amount;
    else if(type.equals("E"))
        expenses += amount;      
}


public void displayTotals() {      
    System.out.printf("%-20s%10.2f\n","Total ticket sales: ",ticketSales);
    System.out.printf("%-20s%10.2f\n","Total donations: ",donations);
    System.out.printf("%-20s%10.2f\n","Total expenses: ",expenses);
    System.out.println();
}



public double calcEventProfit() {
    return (ticketSales + donations - expenses);
}  

public double getTicketSales(){
    return ticketSales;
}

public double getDonations(){
    return donations;
}

public double getExpenses(){
    return expenses;
}

}

我真的不想在任何地方将char转换为字符串。它在Eclipse和NetBeans上运行良好。

我只是一个不应该尝试编码的白痴,还是教科书有问题?

0 个答案:

没有答案