Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By Junky

Thread: Comparing array values with int's

  1. #1
    kcon90 is offline Member
    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0

    Default Comparing array values with int's

    I have this program from an Assignment at school and i'm kinda struggling with it... so, i have this code:

    Java Code:
    import java.util.Random;
    
    public class Skydiving {
    
    	public static final Random myRandom = new Random();
    	public static final int FORMATION = 38;
    	public static void main(String[] args) {
    		
    		int formation;
    		int points;
    		boolean random;
    		
    		for(int round = 1; round <= 10; round++){			
    			System.out.printf("\nROUND %d\n\t|", round);			
    			points = 0;
    			random = false;
    			
    			while(points < 5) {
    				if (points == 4 && random) {
    					do{
    						//formation = giveRandomNumber();
    						formation = 1 + myRandom.nextInt(FORMATION);								
    					} while(formation < 23 || formation == 31);
    				}
    				else {
    					do{
    						formation = 1 + myRandom.nextInt(FORMATION);
    						//formation = giveRandomNumber();	
    					} while (formation == 31);
    				}			
    				
    				if (formation < 23)
    					points+= 2;
    				else{
    					random = true;
    					points++;
    				}
    				
    				System.out.printf("%s %s |", formation > 22 ? randomLetter(formation) : formation, formationName(formation));
    			}
    			System.out.printf("\t%d Round Points", points);
    		}
    		System.out.println();
    	}
    	
    	public static String randomLetter(int formation){
    		if(formation == 23)
    			return "A";
    		if(formation == 24)
    			return "B";
    		if(formation == 25)
    			return "C";
    		if(formation == 26)
    			return "D";
    		if(formation == 27)
    			return "E";
    		if(formation == 28)
    			return "F";
    		if(formation == 29)
    			return "G";
    		if(formation == 30)
    			return "H";
    		if(formation == 31)
    			return "I";
    		if(formation == 32)
    			return "J";
    		if(formation == 33)
    			return "K";
    		if(formation == 34)
    			return "L";
    		if(formation == 35)
    			return "M";
    		if(formation == 36)
    			return "N";
    		if(formation == 37)
    			return "O";
    		if(formation == 38)
    			return "P";	
    		else
    			return "Q";
    	}
    	
    	public static String formationName(int formation){
    		if(formation == 1)
    			return "SNOWFLAKE-SNOWFLAKE";
    		if(formation == 2)
    			return "SIDEBODY DONUT-SIDE FLAKE DONUT";
    		if(formation == 3)	
    			return "SIDE FLAKE OPAL-TURF";
    		if(formation == 4)
    			return "MONOPOD-MONOPOD";
    		if(formation == 5)
    			return "OPAL-OPAL";
    		if(formation == 6)
    			return "STARDIAN-STARDIAN";
    		if(formation == 7)
    			return "SIDEBUDDIES-SIDEBUDDIES";
    		if(formation == 8)
    			return "CANADIAN TEE-CANADIAN TEE";
    		if(formation == 9)
    			return "CAT ACORRDIAN-CAT ACCORDIAN";
    		if(formation == 10)
    			return "DIAMOND-BUNYIP";
    		if(formation == 11)
    			return "PHOTON-PHOTON";
    		if(formation == 12)
    			return "BUNDY-BUNDY";
    		if(formation == 13)
    			return "OFFSET-SPINNER";
    		if(formation == 14)
    			return "BIPOLE-BIPOLE";
    		if(formation == 15)
    			return "CATERPILLAR-CATERPILLAR";
    		if(formation == 16)
    			return "COMPRESSED-BOX";
    		if(formation == 17)
    			return "DANISH TEE-MURPHY";
    		if(formation == 18)
    			return "ZIRCON-ZIRCON";
    		if(formation == 19)
    			return "RITZ-ICEPICK";
    		if(formation == 20)
    			return "PIVER-VIPER";
    		if(formation == 21)
    			return "ZIG ZAG-MARQUIS";
    		if(formation == 22)
    			return "TEE-CHINESE TEE";
    		if(formation == 23)
    			return "UNIPOD";
    		if(formation == 24)
    			return "STAIRTEP DIAMOND";
    		if(formation == 25)
    			return "MURPHY FLAKE";
    		if(formation == 26)
    			return "YUAN";
    		if(formation == 27)
    			return "MEEKER";
    		if(formation == 28)
    			return "OPEN ACCORDIAN";
    		if(formation == 29)
    			return "CATACORD";
    		if(formation == 30)
    			return "BOW";
    		if(formation == 31)
    			return "DONUT";
    		if(formation == 32)
    			return "DONUT";
    		if(formation == 33)
    			return "HOOK";
    		if(formation == 34)
    			return "ADDER";
    		if(formation == 35)
    			return "STAR";
    		if(formation == 36)
    			return "CRANK";
    		if(formation == 37)
    			return "SATELLITE";
    		if(formation == 38)
    			return "SIDEBODY";
    		else
    			return "PHALANX";
    	}
    
    	public static int giveRandomNumber(){
    		
    		int[] myArray = new int[FORMATION];
    		boolean isUsed = false;
    		int formation = 0;
    		
    		try{
    			
    			for(int i = 0; i < myArray.length; i++){
    				
    				do{
    					formation =  1 + myRandom.nextInt(FORMATION);
    					isUsed = equalValue(formation, myArray);
    					myArray[i] = formation;
    					formation = myArray[formation];
    				} while (!isUsed);
    
    			}
    		}
    		catch(ArrayIndexOutOfBoundsException e){
    			
    		}
    			return formation;
    	}
    	
    	public static boolean equalValue(int random, int[] myArray){
    		
    		boolean equalValue = false;
    		
    		for (int index = 0; index < myArray.length; index++){
    			if(random == myArray[index]){
    				equalValue = true;
    				continue;
    			}
    		}		
    		return equalValue;
    	}	
    }
    what i need to do is generate skydiving formations using ararys to store the formations numbers (which i generate using the class RANDOM)... so, i generate a number and store it in the array and then when another number is generated is compared with the values inside the array so there are not repetitions during the 10 rounds i have to generate... I think my problem is in the giveRandomNumber method because i'm getting repetitions... my method equalValue() is working properly from tests I've been doing in other program, so, id like to get some help with this :( i don't know exactly what to do right now cuz I'm not a programmer and i'm desperate :(

    NOTE: others parts and method of the program are working pretty fine...

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Comparing array values with int's

    Firstly you can make the equalValue more efficient by exiting the loop (or returning) as soon as it finds a duplicate value. As it is if you find the first number is a duplicate you still go and test all other nine values which is pointless.

    Next your do loop in the giveRandomNumber method is a little hinky. Your condition is !isUsed. Imagine if the new formation is not in the array, equalValue returns false, ! false is true therefore your loop goes around again when it shouldn't and I imagine it doesn't go around again when it should.
    Java Code:
    formation = myArray[formation];
    What is that line for?

  3. #3
    kcon90 is offline Member
    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0

    Default Re: Comparing array values with int's

    To give the value of the random rumber in the array

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Comparing array values with int's

    Huh?

  5. #5
    kcon90 is offline Member
    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0

    Default Re: Comparing array values with int's

    ok I generate the random numbers and then I store it in the Array

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Comparing array values with int's

    Java Code:
    formation =  1 + myRandom.nextInt(FORMATION); // generates random number
    myArray[i] = formation; // stores random number in array
    formation = myArray[formation]; // totally pointless
    Imagine your array (at some point) contains [4,1,3,0,0,0,0,0,0,0]
    You then generate the number 7 and store it in formation.
    The number 7 is then stored in the array: [4,1,3,7,0,0,0,0,0,0]
    That last line of code then says go to position 7 in the array and store the value there in the variable called formation. So the number 0 gets stored in formation. For what purpose?

  7. #7
    kcon90 is offline Member
    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0

    Default Re: Comparing array values with int's

    I used

    Java Code:
    formation = myArray[formation];
    to return an Int to my main method

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Comparing array values with int's

    You have commented out the calls to the giveRandomNumber method and are generating the random numbers in the main method. Looks like you need to step away from the keyboard, think about things, sketch out your ideas on paper and then go back and tidy up your code.

  9. #9
    kcon90 is offline Member
    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0

    Default Re: Comparing array values with int's

    I have 3 days on it :(

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Comparing array values with int's

    So? How is that our problem?

  11. #11
    kcon90 is offline Member
    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0

    Default Re: Comparing array values with int's

    i cannot make it generate formations without repetitions in this program

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Comparing array values with int's

    Once again so? I have offered you help and advice. What I will not do is write the code for you. If you are just going to sit there and whine then you will not get far.
    DarrylBurke likes this.

Similar Threads

  1. Comparing 4 values
    By faust83 in forum New To Java
    Replies: 11
    Last Post: 05-04-2012, 02:23 PM
  2. Help with comparing values
    By sabarooni in forum New To Java
    Replies: 1
    Last Post: 12-06-2011, 03:41 PM
  3. Comparing values within an Object
    By Bake1tEazy in forum New To Java
    Replies: 10
    Last Post: 02-09-2011, 02:05 PM
  4. Comparing values in the table with the value the user keyed in
    By pancakeleh in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-06-2011, 01:31 AM
  5. Comparing values problems
    By mari.beka in forum New To Java
    Replies: 12
    Last Post: 11-16-2010, 09:15 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
  •