Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Beginning java help

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Beginning java help

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Beginning java help

    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)
    At line 40 the Scanner class's nextInt() method tried to read some data that could not be converted to an int value. The user must be careful to enter the correct data.
    Also the program should catch user errors by putting the call to the nextInt() method in a try{}catch() block soit can print an error message and ask the user to try again.

    it keeps combining purchaseDate and employeeID into one question.
    There is a problem when calls to the Scanner class's nextLine() and nextDouble() or nextInt() are mixed.
    The Scanner class saves the lineend character from the Enter key in its buffer after a call to nextDouble() or nextInt(). You need to call nextLine() to read that lineend character as an empty before trying to read a line of data from the user.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginning java help

    Ok, I understand the buffer but the calcAge method; it doesn't even let the user enter data. As soon as it gets to that input those errors automatically arise. It does not even ask the user to input anything. It just goes to that error.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Beginning java help

    Please post the current version of the code.
    Also copy and paste here the contents of the console from when you execute the code that shows the problem.
    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginning java help

     
     
     
    public class Asset 
    {
     
    	//*****************************************************
    	//	Variables
    	//*****************************************************
     
    	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(1);
    			       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;
    			}
     
     
    		}
     
    	//*****************************************************************************************
    	//	Print the data
    	//*****************************************************************************************
     
    		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();	
    		}
    }

     
    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 calcAge;
    		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();
     
    		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? ");
    		calcAge = 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, calcAge, testReplacement);
     
    		System.out.println();
    		System.out.println("Here is the data that you provided:");
    		System.out.println(asset.printData());
     
     
     
    		keyboard.close();
     
     
     
     
     
    	}
     
    }

    Error:

    Exception in thread "main" Here is the data that you provided:
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(Unknown Source)
    at Asset.calcAge(Asset.java:122)
    at Asset.printData(Asset.java:159)
    at AssetDriver.main(AssetDriver.java:52)

    The first code is the main class the second code is the driver class testing the main class. I wanted to post more than just the calcAge method because the constructors may have something to do with the errors.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Beginning java help

    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(Unknown Source)
    at Asset.calcAge(Asset.java:122)
    At line 122 the program used a value of -1 in a call to the substring() method.
    Check the code to see why it used an invalid value with substring().
    The code should test the value before using it.

    Also check that the length of the String is long enough.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginning java help

    Well the user will enter dd/mm/yyyy. So it should be a value of -6 since I only want the yyyy. I have tried that as well though and it doesn't work. I have tried: (""), (0), (1), and (6). None of which work. I get that same error everything and do not understand why. Possibly because my variables are messed up in the main class. I am not sure though.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Beginning java help

    Print out the String's contents and length so you can see what the computer sees when it executes the code.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Beginning Java
    By minorit_report in forum Java Theory & Questions
    Replies: 2
    Last Post: September 30th, 2012, 03:00 PM
  2. Very Simple Beginning Java Help Needed
    By lgore93 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 11th, 2011, 07:29 PM
  3. Hangman game for JAVA Beginning class
    By shahravi88 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 12th, 2011, 03:15 PM
  4. Restarting at the beginning of an array
    By bonbon242 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 31st, 2010, 10:58 AM
  5. Beginning java programmer!!!!!!!!!!!!!!!!!! need help
    By raidcomputer in forum Java Theory & Questions
    Replies: 3
    Last Post: September 15th, 2009, 08:52 PM