Results 1 to 3 of 3
  1. #1
    Sparkx is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default First Game In Java: Having trouble with my try catch errors.

    My game program ( I am a beginner in Java so the code may not be the most efficient way to do it). This is in Ready To Program Java:


    /*
    The "QuadGameAlpha" class
    Author: Andi Zhou
    Date: January 20, 2011
    Description: //remember to add
    /*
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;



    public class QuadGameAlpha extends Applet implements ActionListener
    {

    int appletWidth, appletHeight;
    private DrawPanel display;
    private Panel input, initiate;
    JButton btnStart, btnControls, btnHelp, btnExit, btnMove1, btnMove2, btnAtk1, btnAtk2, btnAtk3, btnAtk4, btnAtkArea1, btnAtkArea2, btnDone;
    public int quadrant = 0, atk = 0, atkArea = 0;
    public boolean move = false, attack = false, where = false;
    public int aiMove = 0, aiAtk = 0, aiWhere = 0;
    public int userHP = 0, userStamina = 0, enemyHP = 0;
    public int ranDamage, ranDamageM;
    public int counter = 0;
    public int score = 0;
    public String playerName;



    public void init ()
    {

    setSize (900, 650);
    appletWidth = 900;
    appletHeight = 650;

    display = new DrawPanel ();
    input = new Panel ();
    initiate = new Panel ();

    setLayout (new BorderLayout ());

    btnStart = new JButton ("Start");
    btnControls = new JButton ("Controls");
    btnHelp = new JButton ("Help and Objectives");
    btnExit = new JButton ("Exit");
    btnMove1 = new JButton ("Up");
    btnMove2 = new JButton ("Down");
    btnAtk1 = new JButton ("Mega Buster");
    btnAtk2 = new JButton ("Cyber Sword");
    btnAtk3 = new JButton ("Charged Shot");
    btnAtk4 = new JButton ("Power Up");
    btnAtkArea1 = new JButton ("Attack Top");
    btnAtkArea2 = new JButton ("Attack Bottom");
    btnDone = new JButton ("Done");

    initiate.add (btnStart);
    initiate.add (btnControls);
    initiate.add (btnHelp);
    initiate.add (btnExit);

    input.add (btnMove1);
    input.add (btnMove2);
    input.add (btnAtk1);
    input.add (btnAtk2);
    input.add (btnAtk3);
    input.add (btnAtk4);
    input.add (btnAtkArea1);
    input.add (btnAtkArea2);
    input.add (btnDone);

    add (input, BorderLayout.NORTH);
    add (initiate, BorderLayout.SOUTH);
    add (display, BorderLayout.CENTER);

    btnStart.addActionListener (this);
    btnControls.addActionListener (this);
    btnHelp.addActionListener (this);
    btnExit.addActionListener (this);
    btnMove1.addActionListener (this);
    btnMove2.addActionListener (this);
    btnAtk1.addActionListener (this);
    btnAtk2.addActionListener (this);
    btnAtk3.addActionListener (this);
    btnAtk4.addActionListener (this);
    btnAtkArea1.addActionListener (this);
    btnAtkArea2.addActionListener (this);
    btnDone.addActionListener (this);

    btnAtk3.setEnabled (false);


    }


    public void paint (Graphics g)
    {
    }


    public void actionPerformed (ActionEvent e)
    {

    if (e.getSource () == btnMove1)
    {
    quadrant = 1;
    move = true;

    }

    if (e.getSource () == btnMove2)
    {
    quadrant = 2;
    move = true;
    }

    if (e.getSource () == btnAtk1)
    {
    atk = 1;
    attack = true;
    }

    if (e.getSource () == btnAtk2)
    {
    atk = 2;
    attack = true;
    }

    if (e.getSource () == btnAtk3)
    {
    atk = 3;
    attack = true;
    }

    if (e.getSource () == btnAtk4)
    {
    atk = 4;
    attack = true;
    }

    if (e.getSource () == btnAtkArea1)
    {
    atkArea = 1;
    where = true;
    }

    if (e.getSource () == btnAtkArea2)
    {
    atkArea = 2;
    where = true;
    }

    if (e.getSource () == btnDone)
    {
    if (move == true && attack == true && where == true)
    {
    fight (quadrant, atk, atkArea);
    aiMove = (int) (Math.random () * 2 + 1);
    aiAtk = (int) (Math.random () * 3 + 1);
    aiWhere = (int) (Math.random () * 2 + 1);
    enemy (aiMove, aiAtk, aiWhere);
    ranDamage = (int) (Math.random () * 90 + 1);
    ranDamageM = (int) (Math.random () * 100 + 30);

    if (userStamina == 100 || userStamina > 100)
    {
    btnAtk3.setEnabled (true);
    }

    else if (userStamina < 100)
    {
    btnAtk3.setEnabled (false);
    atk = 4;
    }

    if (userStamina < 20)
    {
    btnAtk1.setEnabled (false);
    atk = 4;
    }

    else if (userStamina == 20 || userStamina > 20)
    {
    btnAtk1.setEnabled (true);
    }

    if (userStamina < 40)
    {
    btnAtk2.setEnabled (false);
    atk = 4;
    }

    else if (userStamina == 40 || userStamina > 40)
    {
    btnAtk2.setEnabled (true);
    }

    }

    else
    {
    JOptionPane.showMessageDialog (null, "Please click start and enter all 3 inputs.", "WARNING", JOptionPane.WARNING_MESSAGE);
    }
    }

    if (e.getSource () == btnStart)
    {
    boolean start = true;
    display.animations (start);
    userHP = 300;
    userStamina = 50;
    enemyHP = 500;

    btnStart.setEnabled (false);
    }

    if (e.getSource () == btnControls)
    {
    JOptionPane.showMessageDialog (null, "Movement: " + "\n" + "To move Megaman you choose either to move up or down buttons. Your character (Megaman) will start on the bottom left of the screen. \n\nAttacking: \nAfter selecting where you want Megaman to go, select a type of attack. Each attack has different effects and amounts of damage therefore should be used strategically. \nWhen you are done selecting an attack, select the area (up or down) you want the attack to go. \n Mega Buster is your standard attack, doesn't take much stamina while still doing decent and consistent damage. \nCyber Sword hits hard if you're, but has a random damage rate and takes a lot more stamina. \nPower Up being your most essential attack, it recovers stamina as well as health, but you can't deal ANY damage so choose carefully when you want to use it. \nCharged Shot is your special attack, which you can use when you have 100 stamina. \n Charged Shot never misses, and takes away 1/3 of your opponents health though it does take all 100 points of stamina. ");
    }

    if (e.getSource () == btnHelp)
    {
    JOptionPane.showMessageDialog (null, "Your main objective is to deal enough damage to reduce your opponent's (Bass') health to zero. \nYou can do this in a variety of ways such as using the powerful yet random Cyber Sword, or \nuse your consistent Mega Buster OR power up til you have 100 stamina to unleash Charged Shot! \nYour score is determined on how much damage you deal to the enemy and how many turns it takes for you to delete your opponent. \nThe shorter it takes for you to beat the enemy or the more damage you deal will give you a better score.");
    }

    if (e.getSource () == btnExit)
    {
    System.exit (0);
    }
    }


    public void fight (int quadrant, int atk, int atkArea)
    {
    if (atk == 4)
    {
    userHP = userHP + 10;
    userStamina = userStamina + 25;
    System.out.println ("Megaman used Power Up!");
    System.out.println ("Your HP is now: " + userHP);
    System.out.println ("Your stamina is now: " + userStamina + "\n");

    }

    else if (atk == 1 && atkArea == aiMove)
    {
    enemyHP = enemyHP - 50;
    userStamina = userStamina - 20;
    System.out.println ("Megaman used Mega Buster!");
    System.out.println ("Bass' HP is now: " + enemyHP);
    System.out.println ("Megaman's stamina is now: " + userStamina + "\n");
    score = score + 50;
    }

    else if (atk == 2 && atkArea == aiMove)
    {
    enemyHP = enemyHP - ranDamageM;
    userStamina = userStamina - 40;
    System.out.println ("Megaman used Cyber Sword!");
    System.out.println ("Bass' HP is now: " + enemyHP);
    System.out.println ("Megaman's stamina is now: " + userStamina + "\n");
    score = score + ranDamageM;
    }


    else if (atk == 3 && userStamina == 100 || userStamina > 100)
    {
    enemyHP = enemyHP - 175;
    userStamina = userStamina - 100;
    System.out.println ("Megaman used Charged Shot!");
    System.out.println ("Bass' HP is now; " + enemyHP);
    System.out.println ("Megaman's stamina is now: " + userStamina + "\n");
    score = score + 175;
    }

    else if (atkArea != aiMove)
    {
    System.out.println ("Megaman's attack missed! \n");
    }


    }

    public void enemy (int aiMove, int aiAtk, int aiWhere)
    {
    if (aiAtk == 3)
    {
    enemyHP = enemyHP + 30;
    System.out.println ("Bass has just used Recover 20!");
    System.out.println ("Bass' HP is now: " + enemyHP + "\n");
    }

    else if (aiAtk == 1 && aiWhere == quadrant)
    {
    userHP = userHP - 60;
    System.out.println ("Bass just used Tank Cannon!");
    System.out.println ("Your HP is now: " + userHP + "\n");
    gameOver (userHP);
    }

    else if (aiAtk == 2 && aiWhere == quadrant)
    {
    userHP = userHP - ranDamage;
    System.out.println ("Bass just used Hell Wheels!");
    System.out.println ("Your HP is now: " + userHP + "\n");
    gameOver (userHP);
    }

    else if (aiWhere != quadrant)
    {
    System.out.println ("Bass missed his attack! \n");
    }


    }


    public void gameOver (int userHP)
    {
    if (userHP == 0 || userHP < 0)
    {

    JOptionPane.showMessageDialog (null, "MEGAMAN DELETED.", "WARNING", JOptionPane.WARNING_MESSAGE);
    JOptionPane.showMessageDialog (null, "Your score is: " + score);
    playerName = JOptionPane.showInputDialog ("Please enter your game name.");
    snSort (playerName, score);
    JOptionPane.showMessageDialog (null, "Your score has been saved.");

    System.exit (0);

    }

    if (enemyHP == 0 || enemyHP < 0)
    {
    JOptionPane.showMessageDialog (null, "The Net is now safe from Bass' reign of destruction!", "BASS DELETED", JOptionPane.WARNING_MESSAGE);
    JOptionPane.showMessageDialog (null, "Your score is: " + score);
    playerName = JOptionPane.showInputDialog ("Please enter your game name.");

    snSort (playerName, score);
    JOptionPane.showMessageDialog (null, "Your score has been saved.");

    System.exit (0);
    }
    }


    public static void snSort (String addName, int addHighScore)
    {
    try
    {
    FileReader file = new FileReader ("tophighscores.txt");
    BufferedReader input = new BufferedReader (file);

    FileWriter outFile = new FileWriter ("tophighscores.txt");
    PrintWriter output = new PrintWriter (outFile);

    String topNames[] = new String [11];
    int playerScores[] = new int [11];

    for (int i = 0 ; i < 10 ; i++)
    {
    topNames [i] = input.readLine ();
    playerScores[i] = Integer.parseInt (input.readLine ());
    System.out.println (topNames [i] + "" + playerScores [i]);
    }

    topNames[10] = addName;
    playerScores [10] = addHighScore;

    for (int i = 0 ; i < 11 ; i++)
    {

    for (int z = 0 ; z < 10 ; z++)
    {

    if (playerScores [z] > playerScores [z + 1])
    {
    int tempScores = playerScores [z];
    playerScores [z] = playerScores [z + 1];
    playerScores [z + 1] = tempScores;

    String tempPlayers = topNames [z];
    topNames [z] = topNames [z + 1];
    topNames [z + 1] = tempPlayers;
    }
    }
    }


    for (int i = 0 ; i < 11 ; i++)
    {
    output.println (topNames [i]);
    output.println (playerScores [i]);
    JOptionPane.showMessageDialog (null, topNames [i] + "" + playerScores [i]);
    }



    String find = JOptionPane.showInputDialog ("Enter the name you wish to search.");
    snSearch (find, topNames, playerScores);

    //input.close ();
    output.close ();

    }

    catch (FileNotFoundException e)
    {
    JOptionPane.showMessageDialog (null, "File not found." , "ERROR 404", JOptionPane.ERROR_MESSAGE);
    System.exit (0);
    }

    catch (NullPointerException e)
    {
    JOptionPane.showMessageDialog (null, "CANCELLED!", "WARNING", JOptionPane.WARNING_MESSAGE);
    System.exit (0);
    }

    catch (Exception e)
    {
    JOptionPane.showMessageDialog (null, "UNKOWN ERROR!", "ERROR", JOptionPane.ERROR_MESSAGE);
    System.exit(0);
    }

    }


    public static void snSearch (String findIt, String players[], int allScores[])
    {
    for (int l = 0 ; l < players.length ; l++)
    {
    if (findIt.equalsIgnoreCase (players [l]) == true)
    {
    JOptionPane.showMessageDialog (null, players [l] + allScores [l]);
    }
    }

    }



    }

    That is all of my code for my game. Now the error... So whenever the player wins or loses, the dialogues with "score saved" and "enter your name here" appear. But whenever I try to type in a name in that dialogue box "Please enter your name here" I always get my try..catch error (exception) so the name and score is never saved into the .txt file I want it saved in. When I take out the:
    catch (Exception e)
    {
    JOptionPane.showMessageDialog (null, "UNKOWN ERROR!", "ERROR", JOptionPane.ERROR_MESSAGE);
    System.exit(0);
    }
    it always gives me the error
    "The constructor "FileWriter" can throw the checked exception "java.io.IOException, so the class creation must be enclosed in a try statement that catches the exception, or else this method must be declared to throw the exception."

    If anyone could shed some light on this, it'd be much appreciated.
    Last edited by Sparkx; 01-24-2011 at 01:00 AM.

  2. #2
    Zman3359's Avatar
    Zman3359 is offline Member
    Join Date
    Jan 2011
    Posts
    42
    Rep Power
    0

    Default

    Java Code:
    catch (FileNotFoundException e)
    {
    JOptionPane.showMessageDialog (null, "File not found." , "ERROR 404", JOptionPane.ERROR_MESSAGE); 
    System.exit (0);
    }
    
    catch (NullPointerException e)
    {
    JOptionPane.showMessageDialog (null, "CANCELLED!", "WARNING", JOptionPane.WARNING_MESSAGE);
    System.exit (0);
    } 
    
    catch (Exception e)
    {
    JOptionPane.showMessageDialog (null, "UNKOWN ERROR!", "ERROR", JOptionPane.ERROR_MESSAGE);
    System.exit(0);
    }
    Try-Catch blocks don't work like that ;) Here is Oracle's explanation of exceptions and trys/catchs, and here is an example:

    Java Code:
    try() {
      // Stuff that throws an exception
    }
    catch(Exception e) {
      // Stuff to do if an exception is thrown
    {

  3. #3
    Join Date
    Jan 2011
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Zman3359 View Post
    Try-Catch blocks don't work like that ;) Here is Oracle's explanation of exceptions and trys/catchs, and here is an example:

    Java Code:
    try() {
      // Stuff that throws an exception
    }
    catch(Exception e) {
      // Stuff to do if an exception is thrown
    {
    thanks this even helped me out!

Similar Threads

  1. Replies: 4
    Last Post: 01-04-2011, 01:36 AM
  2. Trouble with Try Catch blocks and file reading.
    By theBurgh22 in forum New To Java
    Replies: 2
    Last Post: 11-30-2010, 02:11 AM
  3. Replies: 12
    Last Post: 05-06-2010, 11:54 AM
  4. Catch raiserror via java
    By mashinata in forum JDBC
    Replies: 2
    Last Post: 02-19-2010, 04:08 AM
  5. Trying to catch thread errors
    By yelllow4u in forum New To Java
    Replies: 2
    Last Post: 08-07-2007, 03:52 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
  •