Results 1 to 11 of 11
  1. #1
    jaavanewb is offline Member
    Join Date
    Mar 2017
    Posts
    13
    Rep Power
    0

    Default acceptableType() always returns false???

    I have a coin class, an exception class, a bankteller class, and a driver class. I need to test the name exception and have created code within the coin class to test it for validity, then a method within bankteller to throw the exception if it is not valid..if it is valid it creates a new coin object with that type. However, it always returns false. Why does this return false?

    Java Code:
    // here is the issue the code ALWAYS prints the exception //regardless of input
            if(theCoin.acceptableType(type) == false)
            {
                try{
            theTeller.acceptType(type);
        }catch(CoinNameException e){
            System.out.println("abcd");
            System.out.println(type);
        }
    
    
    
    
    // constants for the names
    private final String NAME_QUARTER = "QUARTER";
        private final String NAME_DIME = "DIME";
        private final String NAME_NICKEL = "NICKEL";
        private final String NAME_PENNY = "PENNY";
    
    // method to determine validity found in class coin
    public boolean acceptableType(String type)
        {
            type = type.toUpperCase();
            if(type == NAME_QUARTER || type == NAME_DIME || type == NAME_NICKEL || type == NAME_PENNY)
            {
                return true;
            }
            else 
            return false;
    
    
    
        }
    
    // piggybank class (driver) 
            Scanner keyboard = new Scanner(System.in);
            System.out.println(BankTeller.askForCoins());
            BankTeller theTeller = new BankTeller();// here is the issue the code ALWAYS prints the exception //regardless of input
            if(theCoin.acceptableType(type) == false)
            {
                try{
            theTeller.acceptType(type);
        }catch(CoinNameException e){
            System.out.println("abcd");
            System.out.println(type);
        }
    Last edited by Norm; 03-06-2017 at 02:56 AM. Reason: Added / to ending code tag

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: acceptableType() always returns false???

    You need to use the equals() methods for comparing Strings, not the == operator.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jaavanewb is offline Member
    Join Date
    Mar 2017
    Posts
    13
    Rep Power
    0

    Default Re: acceptableType() always returns false???

    Quote Originally Posted by Norm View Post
    You need to use the equals() methods for comparing Strings, not the == operator.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    how do I edit the post? I am not seeing an edit button anywhere?
    edit: never mind for some reason it kept logging me out

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: acceptableType() always returns false???

    I think there is a minimum posting requirement before editing is allowed.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jaavanewb is offline Member
    Join Date
    Mar 2017
    Posts
    13
    Rep Power
    0

    Default Re: acceptableType() always returns false???

    got it thanks for your help! I have just one more question...I am trying to test a coin object for validity, however, I can't get this to compile:
    Java Code:
    // found in PiggyBank...gives me a no argument error
    if(BankTeller.createCoin(type, 0).acceptableType() == false)
    
    //found in Bankteller
    public static Coin createCoin(String type, int value)
    	{
    		Coin aCoin = new Coin(type, value);
    		return aCoin;
    	}
    // found in Coin
    public boolean acceptableType(Coin other)
    	{
    		type = other.getType();
    		if(type.equals(NAME_QUARTER) || type.equals(NAME_DIME) || type.equals(NAME_NICKEL) || type.equals(NAME_PENNY))
    		{
    			return true;
    		}
    		else 
    		return false;
    		
    		
    		
    	}
    Last edited by Norm; 03-05-2017 at 11:40 PM. Reason: Change quote tags to code tags

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: acceptableType() always returns false???

    I can't get this to compile:
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Please use CODE tags not QUOTE tags. See post#5
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jaavanewb is offline Member
    Join Date
    Mar 2017
    Posts
    13
    Rep Power
    0

    Default Re: acceptableType() always returns false???

    error message when I run PiggyBank.java

    Java Code:
    Exception in thread "main" java.lang.NoSuchMethodError: Coin.acceptableType(LCoin;)Z
    	at BankTeller.acceptType(BankTeller.java:109)
    	at PiggyBank.main(PiggyBank.java:40)
    Java Code:
    PiggyBank.java
    import java.lang.Exception;
    import java.util.Scanner;
    public class PiggyBank {
    	
    	public static void main (String[] args) {
    		
    		
    		Scanner keyboard = new Scanner(System.in);
    		System.out.println(BankTeller.askForCoins());
    		BankTeller theTeller = new BankTeller();
    		//Coin[] myAllowance = Coin.createBank(howMany);
    		System.out.println(BankTeller.askForType());
    		String type = BankTeller.typeToStore();
    		Coin aCoin = BankTeller.createCoin(type,0);
    		if(BankTeller.createCoin(type, 0).acceptableType(type) == false);
    		{
    			try{
    		theTeller.acceptType(type);
    	}catch(CoinNameException e){
    		System.out.println("abcd");
    		System.out.println(type);
    	}
    		System.out.println(BankTeller.askForValue());
    		int value = BankTeller.valueToStore();
    BankTeller.java
    i
    Java Code:
    mport java.util.Scanner;
    public class BankTeller 
    {
    	private final String QUARTER = "25";
    	
    	private final String DIME = "10";
    	private final String NICKEL = "5";
    	private final String PENNY = "1";
    	private final String NAME_QUARTER = "QUARTER";
    	private final String NAME_DIME = "DIME";
    	private final String NAME_NICKEL = "NICKEL";
    	private final String NAME_PENNY = "PENNY";
    	static Scanner keyboard = new Scanner(System.in);
    	private String type = null;
    	private int top = 0;
    	private int numCoins = keyboard.nextInt();
    	private Coin[] allowance = createBank(numCoins);
    	public static String askForCoins()
    	{
    		return "How many coins?";
    		
    	}
    	public static String askForType()
    	{
    		return "What kind of coin to deposit?";
    	}
    	public static String askForValue()
    	{
    		return "How much is the coin worth?";
    	}	
    	public static int valueToStore()
    	{
    		int value = keyboard.nextInt();
    		return value;
    	}
    	public static int coinsToStore()
    	{
    		int coins = keyboard.nextInt();
    		return coins;
    	}
    	public static String typeToStore()
    	{
    		String type = keyboard.next();
    		return type;
    	}
    	public static Coin createCoin(String type, int value)
    	{
    		Coin aCoin = new Coin(type, value);
    		return aCoin;
    	}
    	public static Coin[] createBank(int numberOfCoins)
    	{
    		Coin[] allowance = new Coin[numberOfCoins];
    		return allowance;
    	}
    	public boolean acceptableValue(Coin other)
    	{
    		
    		allowance[0] = other;
    		String value = Integer.toString(allowance[0].getValue());
    		if(value.equals(QUARTER) || value.equals(DIME) || value.equals(NICKEL) || value.equals(PENNY))
    		{
    			return true;
    		}
    		else 
    		return false;
    		
    	}
    	/*public boolean acceptableType(Coin other)
    	{
    		type = other.getType();
    		if(type.equals(NAME_QUARTER) || type.equals(NAME_DIME) || type.equals(NAME_NICKEL) || type.equals(NAME_PENNY))
    		{
    			return true;
    		}
    		else 
    		return false;
    		
    		
    		
    	}*/
    	public Coin acceptType(String type) throws CoinNameException
    	{
    		type = type;
    		Coin ofType = new Coin(type, 0);
    		if(ofType.acceptableType(ofType) == false){
    			try{
    				throw new CoinNameException(type);
    			} catch(CoinNameException e){
    				System.out.println(e.getMessage());
    			}
    			}else if (ofType.acceptableType(ofType) == true)
    				++top;
    				allowance[top - 1] = new Coin(type, 0);
    				return allowance[top - 1];
    				
    		}
    		public Coin acceptValue(boolean accept) throws CoinValueException
    	{
    		
    		accept = acceptableValue(allowance[top]);
    		
    		if(accept == false){
    			try{
    				throw new CoinValueException(allowance[top].getValue());
    			} catch(CoinValueException e){
    				System.out.println(e.getMessage());
    			}
    		}
    			else if (accept == true)
    	
    				allowance[top] = new Coin(" ", allowance[top].getValue());
    				return allowance[top];
    				
    			
    		}
    		public Coin acceptCoin()
    		{
    			Coin[] allowance = new Coin[numCoins];
    			allowance[top-1] = new Coin(allowance[top-1].getType(), allowance[top].getValue());
    			return allowance[top-1];
    			
    			
    		}
    			
    	
    	
    
    }

    I can get the code to function correctly if I code it in main..but I'm trying to get my code to be more object oriented and I'm having major issues with that....

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: acceptableType() always returns false???

    Exception in thread "main" java.lang.NoSuchMethodError: Coin.acceptableType(LCoin;)Z
    at BankTeller.acceptType(BankTeller.java:109)
    What was the compiler error you got before you tried to execute the program? Executing code with compiler errors is never a good idea.
    Fixing the compiler errors should help the execution.

    Where is the definition of the Coin class?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: acceptableType() always returns false???

    The error message tells it all.

  10. #10
    jaavanewb is offline Member
    Join Date
    Mar 2017
    Posts
    13
    Rep Power
    0

    Default Re: acceptableType() always returns false???

    Java Code:
    import java.util.Scanner;
    public class Coin
    {
    	
    	private final String QUARTER = "25";
    	
    	private final String DIME = "10";
    	private final String NICKEL = "5";
    	private final String PENNY = "1";
    	private final String NAME_QUARTER = "QUARTER";
    	private final String NAME_DIME = "DIME";
    	private final String NAME_NICKEL = "NICKEL";
    	private final String NAME_PENNY = "PENNY";
    	
    	private String type;
    	private int value;
    	public Coin(String type, int value)
    	{
    		this.type = type;
    		this.value = value;
    	}
    	public String getType()
    	{
    		return type;
    	}
    	public int getValue()
    	{
    		return value;
    	}
    	
    	public boolean acceptableType(String type)
    	{
    		type = type;
    		if(type.equals(NAME_QUARTER) || type.equals(NAME_DIME) || type.equals(NAME_NICKEL) || type.equals(NAME_PENNY))
    		{
    			return true;
    		}
    		else 
    		return false;
    		
    		
    		
    	}
    
    	public String toString()
    	{
    		return "Type: " + type + " Value: " + value;
    		
    	}
    	public boolean equals(Object other)
    	{
    		if(other.getClass() != getClass() || other == null)
    		{
    			return false;
    		}
    		Coin aCoin = (Coin) other;
    		if(aCoin.getType() == getType() || aCoin.getValue() == getValue())
    		return true;
    		return false;
    	}
    	
    }

  11. #11
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: acceptableType() always returns false???

    Line 86 of the BankTeller class:
    ofType is a Coin (instance)
    acceptableType(String string) is a method in the Coin class.
    The acceptableType() method expects a String as parameter, but you pass in a Coin. Since the compiler can't find a method that takes a coin as parameter, it says "no such method".
    It should look something ... ofType.acceptableType(type) ...

Similar Threads

  1. Replies: 13
    Last Post: 07-10-2014, 03:28 PM
  2. True and False Help
    By NBAPwns in forum New To Java
    Replies: 2
    Last Post: 12-19-2013, 12:55 PM
  3. Set TexField to false
    By globo in forum New To Java
    Replies: 3
    Last Post: 11-18-2010, 08:46 PM
  4. swirling becomes false,why?
    By arefeh in forum New To Java
    Replies: 2
    Last Post: 01-18-2010, 06:12 PM
  5. Replies: 4
    Last Post: 08-11-2009, 11:31 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •