Results 1 to 8 of 8
  1. #1
    maggie_2 is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

    Default Need Help with exception

    I am working on code to construct an application that helps a user selt an ice cream cone. I managed to get the program to work, howeverwhen I tried to insert an if statement that does not allow a user to select more that 3 scoops I get the following error:


    cannot find symbol:

    (new IceCreamConeException())

    Amy suggestion on how I can fix this would be greatly appreciated, here is my code:


    Java Code:
    public class IceCreamConeException extends Exception
    {
    
      public IceCreamConeException(String flavor)
      {
    
        super(flavor);
    
      }
    
    
    }
    
    
    
    
    import javax.swing.*;
    
     public class IceCreamCone
     {
    
       protected String[] flavor = {"Chocoloate", "Vanilla", "Strawberry"};
       private String options = ""; 
       private int choice;
       private int scoops;
       protected char initial[] = new char[flavor.length];
    
     public String displayOptions() throws IceCreamConeException
     {
    
       for(int x = 0; x < flavor.length; ++x)
       {
    
          options = options + "\n" +(x + 1) + " for " + flavor[x];
          initial[x] = flavor[x].charAt(0);
    
       }
    
       
       String input = JOptionPane.showInputDialog(null, "which flavor of ice cream would you like?" + options);
       for(int y = 0; y < flavor.length; ++y)
          if(input.charAt(0) == initial[y])
            throw (new IceCreamConeException(flavor[y]));  
       choice = Integer.parseInt(input);
       return(flavor[choice - 1]);
    
      String scps = JOptionPane.showInputDialog(null, " how many scoops would you like? " );
       
           if(scoops > 3)
          throw (new IceCreamConeException());
        
    
       }
    
    
    }
    
    
    
    import javax.swing.*;
    
     public class ThrowIceCreamCone
     {
    
       private IceCreamCone conePick;
       private String userChoice = new String();
    
     public ThrowIceCreamCone(IceCreamCone icecreamOptions) throws IceCreamConeException
     {
    
       conePick = icecreamOptions;
       setFlavor();
    
     }
    
    
     public void setFlavor() throws IceCreamConeException
     {
    
       String inputString = new String();
       JOptionPane.showMessageDialog(null, "Choose one the the following ice cream flavors: ");
       userChoice = conePick.displayOptions();
    
     }
    
    
     public String getuserChoice() 
     {
    
       return(userChoice);
    
     
     }
    
    
    } 
        
    
    
    import javax.swing.*;
    
     public class ThrowIceCreamConeII 
     {
    
       public static void main(String[] args)
       {
    
         IceCreamCone conePick = new IceCreamCone();
         ThrowIceCreamCone coneFlavor = null;
         String userChoice = new String();
    
      try
      {
    
        ThrowIceCreamCone selection = new ThrowIceCreamCone(conePick);
        coneFlavor = selection;
        userChoice = coneFlavor.getuserChoice();
    
      }
    
    
      catch(IceCreamConeException error)
      {
    
        userChoice = error.getMessage();
    
      }
    
      catch(Exception error)
      {
    
       userChoice = " You are only allowed a maximum of three scoops ";
    
      }
    
    
      JOptionPane.showMessageDialog(null, " You selected " + userChoice + " ice cream ");
      System.exit(0);
    
      }
    
    
    }

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    That means that you didn't define such constructor in the IceCreamConeEception,add those lines in it:
    Java Code:
    public IceCreamConeException()
      {
    
    
      }

  3. #3
    tamik0 is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default thanks for suggestion, one more question..

    In one of my files, I now get an error message informing me that the > symbol cannot be applied to java.lang.String, int in the "if(scoops > 3) " statement. :confused:

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    maggie_2 is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

    Default Error message

    This is the error message I get:

    Java Code:
    .\IceCreamCone.java:33: operator > cannot be applied to java.lang.String,int
           if(scoops > 3)
                     ^

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default use curly brackets..

    It's always a good idea/habit to use curly brackets "{}" after "if" and "for" statements (when they're one liners). This will save you a lot a headaches in the future if you decides to add more lines to those kind of statements. Not sure if this will help you with your current problem.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default scoops = ???

    Also, I don't see where scoops in assigned a value. Shouldn't scps be turned into an int named scoops?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    String class has methods to do the work, also I use {} as CJSL does.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Re:SQL Exception
    By jyotigupta6 in forum New To Java
    Replies: 1
    Last Post: 10-28-2008, 09:37 AM
  2. Help with Exception
    By bozovilla in forum New To Java
    Replies: 2
    Last Post: 10-19-2008, 05:19 AM
  3. get exception name
    By jithan in forum New To Java
    Replies: 6
    Last Post: 06-05-2008, 02:29 PM
  4. Replies: 1
    Last Post: 03-08-2008, 06:41 PM
  5. Exception
    By Camden in forum New To Java
    Replies: 2
    Last Post: 11-26-2007, 11:50 PM

Posting Permissions

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