-
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;
}
-
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;
}
}
-
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
-
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
-
Replies: 2
Last Post: 08-05-2006, 10:40 PM
-
Replies: 1
Last Post: 04-13-2006, 04:57 AM
-
Replies: 0
Last Post: 09-01-2005, 01:12 AM
-
By Alan Shiers in forum Java
Replies: 0
Last Post: 03-15-2002, 12:51 PM
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks