Hi all. I am trying to run a method called "calcAge" that takes a mm/dd/yyyy string, converts it to an int, and then ONLY takes the yyyy part. Once the yyyy part is isolated I must then subtract it from the current year to provide the difference. This is what I have:
It is under the block comment called "method for "calcAge"
public class Asset { private String assetID; private double cost; private int lifeExpectancy; private String employeeID; private String purchaseDate; private String description; private int calcAge; private boolean outDated; //****************************************************** // Special constructor that sets assetID and employeeID //****************************************************** public Asset(String assetID, String employeeID) { this.assetID = assetID; this.employeeID = employeeID; } //****************************************************** // Special constructor that takes and sets all data //****************************************************** public Asset(double cost, int lifeExpectancy, String purchaseDate, String description, String assetID, String employeeID, int calcAge, boolean outDated ) { this.cost = cost; this.lifeExpectancy = lifeExpectancy; this.purchaseDate = purchaseDate; this.description = description; this.assetID = assetID; this.employeeID = employeeID; this.calcAge = calcAge; this.outDated = outDated; } //************************************************ // Typical getters for all data //************************************************ public String getAssetID() { return assetID; } public double getCost() { return cost; } public int getLifeExpectancy() { return lifeExpectancy; } public String getEmployeeID() { return employeeID; } public String getPurchaseDate() { return purchaseDate; } public String getDescription() { return description; } //************************************************ // Special setters for assetID and employeeID that // guarantees the two data are always stored in // upper case. //*********************************************** public void setAssetID(String assetID) { assetID = assetID.toUpperCase(); } public void setEmployeeID(String employeeID) { employeeID = employeeID.toUpperCase(); } //***************************************************** // Typical setters for all data //***************************************************** public void setCost(double cost) { this.cost = cost; } public void setLifeExpectancy(int lifeExpectancy) { this.lifeExpectancy = lifeExpectancy; } public void setPurchaseDate(String purchaseDate) { this.purchaseDate = purchaseDate; } public void setDescription(String description) { this.description = description; } //***************************************************************** // Method called calcAge that accepts the current date in a String // formatted as mm/dd/yyyy, and returns the age. //***************************************************************** public int calcAge(String purchaseDate) { String stringYear = purchaseDate.substring(7); int intDate = Integer.parseInt(stringYear); return (2013) - intDate; } //****************************************************************** // Predicate method to decide if the component needs to be replaced //****************************************************************** public boolean outdated() { if (calcAge(purchaseDate) > lifeExpectancy) { return true; } else { return false; } } public String printData() { return "Asset Number: " + assetID + "\n" + "Description: " + description + "\n" + "Cost: " + cost + "\n" + "Purchase on: " + purchaseDate + "\n" + "Assigned to Employee ID: " + "Life Expectancy: " + lifeExpectancy + "\n" + "Age: " + calcAge(purchaseDate) + "Needs Replacement? " + outdated(); } }
Then this is the error I get:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at AssetDriver.main(AssetDriver.java:40)
I can't figure it out.
Then also on my driver class that is testing the above class I get this:
import java.util.*; public class AssetDriver { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String testAssetID; String testDescription; double testCost; String testPurchaseDate; String testEmployeeID; int testLifeExpectancy; int testAge; boolean testReplacement; System.out.println("What is your asset number? "); testAssetID = keyboard.nextLine(); System.out.println("Give a description of the item: "); testDescription = keyboard.nextLine(); System.out.println("What is the cost of the item? "); testCost = keyboard.nextDouble(); System.out.println("What was the purchase date? "); testPurchaseDate = keyboard.nextLine(); System.out.println("What is the employee ID? "); testEmployeeID = keyboard.nextLine(); System.out.println("What is the item's life expectancy? "); testLifeExpectancy = keyboard.nextInt(); System.out.println("What is the age of the item? "); testAge = keyboard.nextInt(); System.out.println("Does the item need to be replaced? "); testReplacement = keyboard.nextBoolean(); Asset asset = new Asset(testCost, testLifeExpectancy, testPurchaseDate, testDescription, testAssetID, testEmployeeID, testAge, testReplacement); System.out.println(); System.out.println("Here is the data that you provided:"); System.out.println(asset.printData()); keyboard.close(); } }
For some reason it keeps combining purchaseDate and employeeID into one question.