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 2 of 2

Thread: Throw exception in repository, catch in UI

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

    Question Throw exception in repository, catch in UI

    For my project I need to make players, input is in my GUI (not a lot of graphics yet Everything goes through my domaincontroller to my repository. My repository makes object "player".
    I need to throw an exception in "maakSpeler" when my names are smaller than 3 characters and larger than 15. I need to catch the exception in my UI. Can you help me?





    package gui;
    import javax.swing.JOptionPane;

    import domein.domeincontroller;;

    public class GUI
    {
    public static void main(String args[])
    {
    int aantalSpelers, aantal;
    String naam;
    domeincontroller dom = new domeincontroller();


    aantalSpelers = Integer.parseInt(JOptionPane.showInputDialog("Geef aantal spelers in (2-6):"));
    dom.stelAantalSpelersIn(aantalSpelers);

    String[] namen = new String[dom.geefAantalSpelers()];
    char[] Symbool = new char[dom.geefAantalSpelers()];

    for(aantal = 0; aantal < dom.geefAantalSpelers(); aantal++)
    {
    naam = JOptionPane.showInputDialog("Geef naam van speler (3-15 char)", aantal+1);
    dom.maakSpeler(naam);
    }
    namen = dom.Namen();
    Symbool = dom.Symbolen();
    for(aantal = 0; aantal < dom.geefAantalSpelers(); aantal++)
    {
    System.out.printf("%s \n%c \n", namen[aantal], Symbool[aantal]);
    }

    }
    }






    package domein;

    public class domeincontroller
    {
    spelerRepository spelerR = new spelerRepository();
    public domeincontroller(){

    }
    public int geefAantalSpelers()
    {
    return spelerR.getAantalSpelers();
    }

    public void maakSpeler(String naam)
    {
    spelerR.maakSpeler(naam);
    }

    public void stelAantalSpelersIn(int aantalSpelers) {
    spelerR.setAantalSpelers(aantalSpelers);
    }
    public String[] Namen(){
    int aantalSpelers = spelerR.getAantalSpelers();
    String[] namen = new String[aantalSpelers];
    namen = spelerR.getNaam();
    return namen;
    }
    public char[] Symbolen(){
    int aantalSpelers = spelerR.getAantalSpelers();
    char[] Symbolen = new char[aantalSpelers];
    Symbolen = spelerR.getSymbool();
    return Symbolen;
    }

    }






    package domein;
    import java.util.EmptyStackException;

    import javax.swing.JOptionPane;

    public class spelerRepository
    {
    private int aantalSpelers =2, reedsGemaakt = 0;
    private String symbolen = "&@#$%";
    private Speler[] spelers = new Speler[aantalSpelers];
    char[] symbool = symbolen.toCharArray() ;

    public spelerRepository(){}

    public int getAantalSpelers()
    {
    return aantalSpelers;
    }

    public void setAantalSpelers(int aantalSpelers)
    {
    if(aantalSpelers < 2 || aantalSpelers > 6){aantalSpelers=(Integer) null;}
    try{
    this.aantalSpelers = aantalSpelers;
    Speler[] spelers = new Speler[aantalSpelers];
    }
    catch(NullPointerException ex){
    int nieuwAantal = Integer.parseInt(JOptionPane.showInputDialog("Geef een correct aantal spelers in(2-6)"));
    setAantalSpelers(nieuwAantal);
    }



    }

    public void maakSpeler(String naam)
    {
    if(naam.length() > 15 || naam.length() < 3){
    throw new Error("De opgegeven naam heeft niet de correcte grootte");
    }
    else{
    spelers[reedsGemaakt] = new Speler(naam);
    setSymbool();
    }

    }
    public void setSymbool()
    {
    spelers[reedsGemaakt].setSymbool(symbool[reedsGemaakt]);
    reedsGemaakt++;
    }
    public String[] getNaam()
    {
    String[] Namen = new String[aantalSpelers];
    for(int teller=0; teller<aantalSpelers; teller++)
    {
    Namen[teller] = spelers[teller].getNaam();
    }
    return Namen;
    }
    public char[] getSymbool()
    {
    char[] Symbolen = new char[aantalSpelers];
    for(int teller=0; teller<aantalSpelers; teller++)
    {
    Symbolen[teller] = spelers[teller].getSymbool();
    }
    return Symbolen;
    }

    }









    package domein;

    import javax.swing.JOptionPane;

    public class Speler
    {
    private String naam;
    private char symbool;

    public Speler(String naam){
    setNaam(naam);
    }

    public String getNaam()
    {
    return naam;
    }

    public void setNaam(String naam)
    {
    if(naam.length() >= 3 && naam.length() <= 15)
    {
    this.naam = naam;
    }
    else
    {
    this.naam = null;
    JOptionPane.showMessageDialog(null, "De spelernaam moet tussen de 3 en de 15 karakters lang zijn.","Naam te lang",JOptionPane.ERROR_MESSAGE);

    }
    }

    public char getSymbool()
    {
    return symbool;
    }
    public void setSymbool(char symbool)
    {
    this.symbool = symbool;
    }


    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,328
    Thanks
    49
    Thanked 2,194 Times in 2,167 Posts

    Default Re: Throw exception in repository, catch in UI

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: February 6th, 2013, 10:21 AM
  2. [SOLVED] how could I throw an exception in this case
    By mia_tech in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 2nd, 2012, 09:14 PM
  3. [SOLVED] Catch an Exception right
    By beruska in forum Exceptions
    Replies: 2
    Last Post: October 25th, 2011, 12:48 PM
  4. Applet throw ClassNotFound exception when one of the file is recompiled
    By philwei in forum What's Wrong With My Code?
    Replies: 21
    Last Post: July 12th, 2011, 01:21 PM
  5. [SOLVED] Problems with Try/Catch Catching Wrong Exception
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: June 10th, 2011, 08:08 PM

Tags for this Thread