method help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: method help

  1. #1
    Join Date
    Feb 2005
    Posts
    24

    method help

    Hi guys ive designing a method for entering a player and and human player. but the only problem is im not sure how to catch a thrown exception for example the 1st method is only enter 2,3,4,5 if eneter anything else show bad number. but it shows the error. but it cant catch it. Can anyone show me were i can add a catch (Exception e) { to my method?



    public static void playerselecta() throws InterruptedException {
    numberofplayers = getOutputAsInt("How many players?");
    if (numberofplayers != 2
    && numberofplayers != 3
    && numberofplayers != 4
    && numberofplayers != 5) {
    throw new InterruptedException("Bad number of players");
    }
    }

    public static void humanplayers() throws InterruptedException {
    humanpl = getOutputAsInt("How many human players?");
    if (numberofplayers - humanpl < 0) {
    throw new InterruptedException("Bad number of humans");
    }
    }

    public static int getOutputAsInt(String input) {
    int output = 0;

    try {
    System.out.println(input);
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    output = Integer.parseInt(in.readLine());
    } catch (Exception e) {
    System.out.println("Bad input/output.");
    }

    return output;
    }

  2. #2
    Join Date
    Sep 2006
    Posts
    68
    My advice: do not use an InterruptedException. It reserved for threads processing. You can define your own exceprion.

    Code:
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    
    public class Example1 {
        private int numberofplayers;
        private int humanpl;
    
        public static void main(String[] args) {
            new Example1().example();
        }
    
    
        public void example() {
            try {
                playerselecta();
                humanplayers();
            } catch (Exception e) {
                System.out.println("Something wrong");
            }
    
        }
    
        public void playerselecta() throws Exception {
            numberofplayers = getOutputAsInt("How many players?");
    
            if (numberofplayers < 2 && numberofplayers > 5) {
                throw new Exception("Bad number of players");
            }
        }
    
        public void humanplayers() throws Exception {
            humanpl = getOutputAsInt("How many human players?");
    
            if ((numberofplayers - humanpl) < 0) {
                throw new Exception("Bad number of humans");
            }
        }
    
        public static int getOutputAsInt(String input) throws Exception {
            int output = 0;
    
            System.out.println(input);
            try{
                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                output = Integer.parseInt(in.readLine());
            } catch (Exception e) {
                System.out.println("Bad input/output.");
                throw new Exception("Bad input/output");
            }
    
            return output;
        }
    }

  3. #3
    Join Date
    Feb 2005
    Posts
    24
    I have tried your method. It is a gud method but only one thing im having trouble with. It still doesnt catch the exception say for the playerselecta. if you type anything else apart from 2,3,4,5 its a bad number and you would have to enter the number again. how would your method doo that. Could you please show me an example. Thanks alots

  4. #4
    Join Date
    Sep 2006
    Posts
    68
    Ahh... I unterstood the question.

    Code:
    public void playerselecta() throws Exception {
            int players = 0;
    
            while (players == 0) {
                try {
                    players = getOutputAsInt("How many players?");
    
                    if (players < 2 && players > 5) {
                        throw new Exception("Bad number of players");
                    }
                } catch(Exception e) {
                    System.out.println(e.getMessage());
                    players = 0;
                }
            }
    
            numberofplayers = players;
        }

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2006, 10:40 PM
  2. Replies: 1
    Last Post: 04-13-2006, 04:57 AM
  3. jdi popframes method
    By caid in forum Java
    Replies: 0
    Last Post: 09-01-2005, 01:12 AM
  4. depricated method needs changing
    By Alan Shiers in forum Java
    Replies: 0
    Last Post: 03-15-2002, 12:51 PM
  5. Replies: 3
    Last Post: 04-13-2001, 09:13 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center