Jump to content


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.


catch syntax in java

  • Please log in to reply
1 reply to this topic

#1 sausage


  • Members
  • 390 posts
  • Gender:Male
  • Location:Colorado
  • Local time:05:07 AM

Posted 05 November 2008 - 10:53 PM

So I need help with using the catch method

here's my code

int cclass;
System.out.println("Please choose your character class");
System.out.println("1) Fighter");
System.out.println("2) Theif");
System.out.println("3) Mage");
	 cclass = keyboard.nextInt();	
catch ( InputMismatchException inputMismatchException )
	 System.err.printf( "\nException: %s\n",
		 inputMismatchException );
		"You must an integer. Please try again.\n" );
f (cclass <=1)//the < is to solve the need for boolean in that statement
	 cclassname = Fighter; 
else if (cclass <=2)
	 cclassname = Theif; 
	  cclassname = Mage;
}//the new variable cclassname is used in dialogue inside my game and is already initialized using the String method
//Also, all of it is inside a public static void main(String[] args) method with import.java.util.*; at the top

What I get when I compile it is variable cclass might not have been initialized... help?

BC AdBot (Login to Remove)


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer

  • Malware Response Team
  • 12,304 posts
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:04:07 AM

Posted 05 November 2008 - 11:05 PM

You didn't initialize cclass until you entered the try block:
cclass = keyboard.nextInt();

This means that cclass could still be pointing to completely random RAM when you try to access it later in your catch blocks.

You can't simply assume that INT is 0 just because you haven't put something there.

int cclass;

int cclass = 0;

Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users