【发布时间】:2016-06-03 08:18:54
【问题描述】:
这是一个学校作业。我得到了这个文本文件,我需要从以下文件中读取值T = ticket sales、D = donations、E = expenses,文本文件将其列为;
T 2000.00
E 111.11
D 500.00
E 22.22
我想从文本文件中获取数据,添加类似的值,询问用户是否希望添加其他数据,然后显示计算输出。
import java.io.*;
import java.util.Scanner;
public class MyEventManager {
public static String amountType;
public static int amount;
public static String validationMethodType () throws IOException
{
Scanner keyboard = new Scanner ( System.in );
System.out.printf("\nPlease enter an amount type ('T' - Tickets), ('D' - Donations), ('E' - Expenses): ");
amountType = keyboard.next().toUpperCase();
char choice = amountType.charAt(0);
//choice = Character.toUpperCase(choice);
if (choice != 'T' && choice != 'D' && choice != 'E')
{
do
{
System.out.printf("\nInvlaid amount entered...");
System.out.printf("\nPlease enter an amount type ('T' - Tickets), ('D' - Donations), ('E' - Expenses): ");
amountType = keyboard.next().toUpperCase();
choice = amountType.charAt(0);
//choice = Character.toUpperCase(choice);
}
while(choice != 'T' && choice != 'D' && choice != 'E');
return amountType;
}
else
{
return amountType;
}
}
public static int validationMethodAmount()
{
Scanner keyboard = new Scanner ( System.in );
System.out.printf("\nPlease enter an amount (amount must be positive and non-zero): ");
amount = keyboard.nextInt();
if (amount <= 0)
{
do
{
System.out.printf("\nInvlaid amount entered...");
System.out.printf("\nPlease enter an amount (amount must be positive and non-zero): ");
amount = keyboard.nextInt();
}
while (amount <= 0);
return amount;
}
else
{
return amount;
}
}
public static void main(String [] args) throws IOException
{
Scanner keyboard = new Scanner ( System.in );
System.out.printf("This program will read a text file and add data to it, then compute the results.\n\n"); // display purpose
MyEventClass myEvent = new MyEventClass(); //create object
//
String readFile = "Event.txt"; //file location constant
try
{
File inputFile = new File (readFile); //open the file
InputStream is;
Scanner scanFile = new Scanner (inputFile); //scan the file
{
is = new BufferedInputStream(new FileInputStream(inputFile));
//
try
{
while(scanFile.hasNext())
{
if ( scanFile.hasNextLine())
{
myEvent.instanceMethod(amountType, amount);
}
}
}
catch (IllegalArgumentException o)
{
System.out.println("Error code 3: No data found!" );
}
}
byte[] c = new byte[1024];
int count = 1;
int readChars;
while ((readChars = is.read(c)) != -1)
{
for (int i = 0; i < readChars; ++i)
{
if (c[i] == '\n')
{
++count;
}
}
}
System.out.println("Total number of valid lines read was " + count);
}
catch (FileNotFoundException e)
{
System.out.println("Error code 4: The file " + readFile + " was not found!" );
}
System.out.println("Are there any more amounts to add that where not in the text file? ");
String questionOne = keyboard.next();
if ("y".equalsIgnoreCase(questionOne))
{
validationMethodType();
validationMethodAmount();
myEvent.instanceMethod(amountType, amount);
}
myEvent.displayResults();
}
}
二等
public class MyEventClass {
private double ticketSales;
private double moneyDonated;
private double moneySpent;
public MyEventClass ()
{
this.ticketSales = 0.0;
this.moneyDonated = 0.0;
this.moneySpent = 0.0;
}
public double getTicketSales ()
{
return ticketSales;
}
public double getMoneyDonated ()
{
return moneyDonated;
}
public double getMoneySpent ()
{
return moneySpent;
}
public double instanceMethod (String amountType, double amount)
{
char choice = amountType.charAt(0);
if(amount <= 0)
{
throw new IllegalArgumentException("Error code 1: Amount should be larger then 0");
}
if(choice != 'T' && choice != 'D' && choice != 'E')
{
//increment the current total for the amount type specified by the first parameter by the amount in the second paramter?
return amount++;
}
else
{
throw new IllegalArgumentException("Error code 2: Invalid input, data will be ignored");
}
}
public void displayResults()
{
double income = this.ticketSales + this.moneyDonated;
double profits = income - this.moneySpent;
System.out.printf("\nTotal Ticket Sales: " + "%8.2f", this.ticketSales);
System.out.printf("\nTotal Donations: " + "%11.2f" + " +", this.moneyDonated);
System.out.printf("\n --------");
System.out.printf("\nTotal Income: " + "%14.2f", income);
System.out.printf("\nTotal Expenses: " + "%12.2f" + " -", this.moneySpent);
System.out.printf("\n --------");
System.out.printf("\nEvent Profits: " + "%13.2f", profits);
System.out.println();
}
}
我认为我的问题之一是 instanceMethod 返回应该在此处添加值的位置。
电流输出;
run:
This program will read a text file and add data to it, then compute the results.
Exception in thread "main" java.lang.NullPointerException
at MyEventClass.instanceMethod(MyEventClass.java:34)
at MyEventManager.main(MyEventManager.java:90)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
【问题讨论】:
-
nextInt()不擅长读取带小数点的数字... -
nextDouble()也许? -
您能否添加更多信息,包括任何错误或您现在在代码中遇到的行为?
-
是的,这样会更好。 (实际上,在处理金额时,您应该使用
BigDecimal,因为double的值并不是真正的精确值。但由于您是新手,nextDouble()可以很好地用于此目的。您可以了解@ 987654334@ 稍后。) -
你可以看看@这个链接Reading double values from a file
标签: java file oop exception-handling return