首页 > 解决方案 > 编译器说我正在尝试将字符转换为字符串,但我没有

问题描述

首先,让我先说我是 Java 编码的新手。我正在上一门编程入门课程,到目前为止,它已经彻底打动了我。

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


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

注意:一些消息已被简化;使用 -Xdiags:verbose 重新编译以获得完整输出

3 个错误

我收到此错误消息:


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

3 个错误

好吧,也许这只是我正在使用的在线教科书,但我看不到我在代码中的任何地方将 char 转换为 String ......

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

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 上运行良好。

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

标签: java

解决方案


推荐阅读