Results 1 to 8 of 8
  1. #1
    Jubic is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default Need some help with assigning & Exceptions

    Java Code:
    /*Importing the utilities*/
    import java.util.Random;
    import java.util.Scanner;
    /*Used this because I don't like seeing wriggly lines*/
    @SuppressWarnings("unused")
    public class FindYourWords {
    	public static void main(String[] args) {
    		/*==================Reading words.txt==================*/
    		String dict[] = FileUtil.readDictFromFile("words.txt");
    		/*==================Scanner & Generator==================*/
    		Random lettersGenerator = new Random();
    		Scanner userInput = new Scanner(System.in);
    		/*==================Inputting all the 26 alphabets into String arrays, consonants & vowels==================*/
    		String[] printConso = new String[8];
    		String[] printVowel = new String[2];
    		String[] consonants = {"b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z"};
    		String[] vowels= {"a","e","i","o","u"};
    		String[] arrayscore0 = {" "," "};
    		String[] arrayscore1 = {"e","a","i","o","n","r","t","l","s","u"};
    		String[] arrayscore2 = {"d","g"};
    		String[] arrayscore3 = {"b","c","m","p"};
    		String[] arrayscore4 = {"f","h","v","w","y"};
    		String[] arrayscore5 = {"k"};
    		String[] arrayscore8 = {"j","x"};
    		String[] arrayscore10 = {"q","z"};
    		int[] scores = {0,1,2,3,4,5,8,10};
    		/*==================Inputting two players & their info==================*/
    		PlayersInfo playerOne = new PlayersInfo();
    		playerOne.wordScore = 0;
    		playerOne.choice = " ";
    		playerOne.turn = true;
    		playerOne.wordScore += playerOne.letterScore;
    		playerOne.totalScore += playerOne.wordScore;
    		
    		PlayersInfo playerTwo = new PlayersInfo();
    		playerTwo.wordScore = 0;
    		playerTwo.choice = " ";
    		playerTwo.turn = false;
    		playerTwo.wordScore += playerTwo.letterScore;
    		playerTwo.totalScore += playerTwo.wordScore;
    		
    		System.out.println("FindYourWords");
    		/*==================Assigning values to letters (Undone)==================*/
    		
    		/*==================Keep the game continuous until a player's input is ! (Defined below)==================*/
    		boolean cont = true;
    		/*==================Game Process==================*/
    		while (cont)
    		{
    			if (playerOne.turn)
    				{
    					System.out.print("Letters of player 1: ");
    					/* Code for printing the 8 consonants & 2 vowels */
    					for (int a = 0;a < printConso.length;a++)
    						{
    							int consoindex = lettersGenerator.nextInt(21);
    							String selectedConso = consonants[consoindex];
    							printConso[a] = selectedConso;
    							System.out.print(printConso[a] + " ");
    						}
    					for (int a = 0;a < printVowel.length;a++)
    						{
    							int vowelindex = lettersGenerator.nextInt(5);
    							String selectedVowel = vowels[vowelindex];
    							printVowel[a] = selectedVowel;
    							System.out.print(printVowel[a] + " ");
    						}
    						System.out.println("");
    						System.out.println("Enter your word (or '@' to pass or '!' to quit): ");
    						playerOne.choice = userInput.nextLine();
    					if (playerOne.choice.equals("@"))
    						{
    							playerOne.totalScore += 0;
    							playerTwo.turn = true;
    						}
    					else if (playerOne.choice.equals("!"))
    					{
    						playerOne.totalScore += 0;
    						playerTwo.totalScore += 0;
    						cont = false;
    					}
    					System.out.println("Total score for word: " + playerOne.wordScore);
    					System.out.println("Total score for game: " + playerOne.totalScore);
    					System.out.println("");
    					playerOne.turn = false;
    				}
    			else if (!playerOne.turn)
    				{
    					System.out.print("Letters of player 2: ");
    					/* Code for printing the 10 letters */
    					for (int a = 0;a < printConso.length;a++)
    						{
    							int consoindex = lettersGenerator.nextInt(21);
    							String selectedConso = consonants[consoindex];
    							printConso[a] = selectedConso;
    							System.out.print(" "+printConso[a]);
    						}
    					for (int a = 0;a < printVowel.length;a++)
    						{
    							int vowelindex = lettersGenerator.nextInt(5);
    							String selectedVowel = vowels[vowelindex];
    							printVowel[a] = selectedVowel;
    							System.out.print(" "+printVowel[a]) ;
    						}
    						System.out.println("");
    						System.out.println("Enter your word (or '@' to pass or '!' to quit): ");
    						playerTwo.choice = userInput.nextLine();
    					if (playerTwo.choice.equals("@"))
    						{
    							playerTwo.totalScore += 0; 
    							playerOne.turn = true;
    						}
    					else if (playerTwo.choice.equals("!"))
    						{
    							playerOne.totalScore += 0;
    							playerTwo.totalScore += 0;
    							cont = false;
    						}
    						System.out.println("Total score for word: " + playerTwo.wordScore);
    						System.out.println("Total score for game: " + playerTwo.totalScore);
    						System.out.println("");
    						playerOne.turn = true;
    			}
    		}
    		/*==================Check whether player's input is in the words.txt or the generated 10 letters-array (Undone)==================*/
    		
    		/*==================Compare both players' scores and decide the winner==================*/
    		if (playerOne.totalScore > playerTwo.totalScore)
    		{
    			System.out.println("Total score for player 1: " + playerOne.totalScore);
    			System.out.println("Total score for player 2: " + playerTwo.totalScore);
    			System.out.println("Player 1 won!");
    		}
    		else if (playerOne.totalScore < playerTwo.totalScore)
    		{
    			System.out.println("Total score for player 1: " + playerOne.totalScore);
    			System.out.println("Total score for player 2: " + playerTwo.totalScore);
    			System.out.println("Player 2 won!");
    		}
    		else if (playerOne.totalScore == playerTwo.totalScore)
    		{ /*==================If draw==================*/
    			System.out.println("Total score for player 1: " + playerOne.totalScore);
    			System.out.println("Total score for player 2: " + playerTwo.totalScore);
    			System.out.println("It's a draw!");
    		}
    	}
    }
    Hello all, currently I'm quite new to Java programming & Obj-Oriented.
    As you can see, I have some problems assigning specific scores to the specific arrays of letters. E.G. If a Player chooses a letter in arrayscore1, the program would give the player 1 point. And also, checking whether player's input is in the words.txt or the 10 letters-array: I know I need to use 2 catch Exceptions for this, but I'm clueless of which Exceptions to use. I hope someone would be a kind soul and give me some guidance. Thanks+rep to those who tries to help :o

    The program should look something like this:

    FindYourWords
    Letters of player 1: a c f h i n p r s w
    Enter your word (or ‘@’ to pass or ‘!’ to quit) : saw
    Total score for word: 6
    Total score for game: 6

    Letters of player 2: b c e g n n o t x z
    Enter your word (or ‘@’ to pass or ‘!’ to quit): bench
    Error: A valid word is formed but one or more letter(s) used are not yours
    Enter your word (or ‘@’ to pass or ‘!’ to quit): benc
    Error: An invalid word is formed
    Enter your word (or ‘@’ to pass or ‘!’ to quit): bet
    Total score for word: 5
    Total score for game: 5

    Letters of player 1: c e f g h i n p r t
    Enter your word (or ‘@’ to pass or ‘!’ to quit): @
    Total score for word : 0
    Total score for game : 6

    Letters of player 2 : a c i g n n o v x z
    Enter your word (or ‘@’ to pass or ‘!’ to quit): gain
    Total score for word : 5
    Total score for game : 10

    Letters of player 1 : c e f g h i n p r t
    Enter your word (or ‘@’ to pass or ‘!’ to quit): !

    Total score for player 1: 6
    Total score for player 2: 10
    Player 2 won!
    Last edited by Jubic; 06-08-2010 at 05:46 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Jubic View Post
    I know I need to use 2 catch Exceptions for this, but I'm clueless of which Exceptions to use. I hope someone would be a kind soul and give me some guidance. Thanks+rep to those who tries to help :o
    You can create your own Exception class for this situation, and the Java Sun tutorials can show you how. They begin here: Lesson: Exceptions (The Java™ Tutorials > Essential Classes)

    Best of luck

  3. #3
    Jubic is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    Wasn't thought how to do so in school, but no harm trying

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Who on earth thought you how to write comments???? They should have a long hard look at themselves... and you should read some programming books on the matter....

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by Jubic View Post
    Wasn't thought how to do so in school, but no harm trying
    School/College will show you the bare minimum, the super 'A' in college is actually worthless, you pick one subject in college and you become a master in it, because unless you are a genius you cannot be a master of everything. If you have decided programming is your call then well done, programming is both creative and logical a weird combination that few can grasp at the start... lol..

    Any way colleges will teach you the very basic of any learning, you must realise this, before you can progress, they will for example show you basic math, if a science degree is your thing, its not up to them for you to expand upon what they have taught and it is the same for programming, you get the fundamentals pal, after that you have to have the desire to learn by yourself, aint no college going to show you every single cavity of any given language!!!

  6. #6
    Jubic is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    Who on earth thought you how to write comments???? They should have a long hard look at themselves... and you should read some programming books on the matter....
    I guess everyone has his own opinions. Doesn't have to follow the book all the time.

  7. #7
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    alright fair enough valid point, I can show you how to do this problem, we need to break it down into small parts and then join all parts together.

    we need a Class for the rules of scoring first, lets start here, create a Class called ScoringRules, and implement the scoring system into methods...

  8. #8
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    btw from reading books I learned how to implement my own exceptions, have a look....
    Java Code:
    package org.marshy.monopoly;
    
    /**
     * A Class to represent the exceptions that can occur while coding the Monopoly Game
     * 
     * @author alan
     * @version 1.0 2010.6.02
     */
    public class MonopolyException extends Exception{
    	
    	/**
    	 * The exception with a default message: 'An Exception Has Occured!'
    	 */
    	public MonopolyException()
    	{
    		super("An Exception Has Occured!");
    	}
    	
    	/**
    	 * The exception with a message provided by the programmer
    	 * 
    	 * @param msg The message set by the programmer
    	 */
    	public MonopolyException(final String msg)
    	{
    		super(msg);
    	}
    
    }
    and quick code of implementing this class
    Java Code:
    public void setRent(int propertyType,int amount) throws MonopolyException
    	{
    
    		switch(propertyType)
    		{
    		case WITHOUT_HOUSE:
    			rentWithoutHouse=amount;
    			break;
    
    		case ONE_HOUSE:
    			rentWithOneHouse=amount;
    			break;
    
    		case TWO_HOUSES:
    			rentWithTwoHouses=amount;
    			break;
    
    		case THREE_HOUSES:
    			rentWithThreeHouses=amount;
    			break;
    
    		case FOUR_HOUSES:
    			rentWithFourHouses=amount;
    			break;
    
    		case HOTEL:
    			rentWithHotel=amount;
    			break;
    
    		default:
    			throw new MonopolyException("Invalid Property Type Entered!!");
    		}
    	}

Similar Threads

  1. Char comparison/assigning through keyTyped
    By AdamMurg in forum Java Applets
    Replies: 0
    Last Post: 10-21-2009, 05:42 PM
  2. Assigning values to an object
    By camper2 in forum New To Java
    Replies: 4
    Last Post: 04-05-2009, 03:13 AM
  3. Assigning Words to an Array
    By ScaryJello in forum New To Java
    Replies: 10
    Last Post: 02-17-2009, 04:40 AM
  4. Assigning a string value to a char
    By coffeebean in forum New To Java
    Replies: 4
    Last Post: 06-15-2008, 06:30 AM
  5. Assigning Threads on Multiprocessor?
    By DrRufus in forum Advanced Java
    Replies: 1
    Last Post: 08-08-2007, 10:55 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
  •