Results 1 to 3 of 3
  1. #1
    Gantz is offline Member
    Join Date
    Mar 2015
    Posts
    1
    Rep Power
    0

    Default I can't compile my code

    I am new to java, I am trying to make a system that entering with snooker balls and show error if a ball that doesn't exist or is duplicate to review "try catch" however can't figure out why my code doesn't run.
    Java Code:
    import java.util.ArrayList;
    
    /**
     *
     * @author gantz
     */
    public class Sinuca {
        
        public class Bola{
            private int numero;
            Bola(int B){ numero = B ; }
            
            public void  setNumero (int numero){
                this.numero = numero;
            }
            public int getNumero(){
                return numero;
            }
            
        }
        public class MesaSinuca{
            ArrayList<String> Bolas = new ArrayList<String> ();
            
               public void addBolas(Bola B){
                   try{
                       verifyBallDuplicated(B);
                       verifyBallInvalid(B);
                       Bolas.addBolas(B);
                   }catch(BolaInvalidaException | BolaRepetidaException e){
                       
                   }
               }
               
               public void verifyBallDuplicated(Bola B)throws BolaRepetidaException{
                   
                   for (Bola teste : Bolas){
                       if(teste.getNumero() == B.getNumero()){
                           throw new BolaRepetidaException("Bola %s duplicada");
                       }
                           
                   }
               
               }
               
               public void verifyBallInvalid(Bola B)throws BolaInvalidaException{
                   if(B.getNumero() < 0 || B.getNumero() > 15){
                       throw new BolaInvalidaException("Bola %s invalida");
                   }
               }
        }
        
        public class BolaRepetidaException extends Exception{
            public BolaRepetidaException (String message){
                super(message);
            }
        }
        
        public class BolaInvalidaException extends Exception{
            public BolaInvalidaException (String message){
                super(message);
            }
           
        }
        
        
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            MesaSinuca ms = new MesaSinuca();
            ms.addBolas(new Bola (1));
            ms.addBolas(new Bola (1));
            ms.addBolas(new Bola (16));
            ms.addBolas(new Bola (2));
        }
        
    }
    Last edited by JosAH; 03-19-2015 at 07:31 AM. Reason: added [code] ... [/code] tags

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: I can't compile my code

    Please place your code between [code][/code] tags to format the code. One problem is that you can't create an instance of an non-static inner class without an instance of the enclosing class. It might be best to make the inner class static and work from there. In your addBolas method, you are trying to invoke Bola.addBolas(). That method doesn't exist in an ArrayList.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: I can't compile my code

    And you declare Bolas to have Strings: ArrayList<String> Bolas = new ArrayList<String> ();
    But later when you iterate over them you expect it to contain Bola: for (Bola teste : Bolas)

    Those two don't match.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Does the following code compile or not?
    By allaudin in forum New To Java
    Replies: 8
    Last Post: 06-23-2013, 09:50 AM
  2. I cant get my code to compile Need help please
    By MikeJ39 in forum New To Java
    Replies: 6
    Last Post: 10-15-2011, 06:28 AM
  3. Can not compile code , anyone help me fix it ?
    By batista11b5 in forum New To Java
    Replies: 3
    Last Post: 10-22-2010, 02:00 PM
  4. Code does not Compile
    By ustar in forum New To Java
    Replies: 13
    Last Post: 03-06-2010, 04:54 PM
  5. Code will not compile
    By ShotGunRockets in forum New To Java
    Replies: 17
    Last Post: 05-10-2009, 03:31 AM

Posting Permissions

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