This is a school assignment. I got this text file that I need to read values from as follows
T = ticket sales
, D = donations
, E = expenses
and the text file list it as;
T 2000.00
E 111.11
D 500.00
E 22.22
I want to get the data from the text file, add the like values, ask the user if they wish to add additional data, then display the computed output.
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();
}
}
The second class
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();
}
}
I think one of my issues is in instanceMethod return where it is supposed to add the values here.
Current output;
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)