Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • How to "get" next array index in Java?


    Cardozz

    Recommended Posts

    Hello guys!

     

    I'm programming a relatively simple gamble program for my school's elective course. I am working with an Array with 3 indexes, so 0, 1 and 2.

     

    What i want is to let the computer guess a number the user has put into the textbox (i got all that sorted of and works blabla). But i want to have the user the option to tell the computer if the guessed number is higher or lower than the set number. Normally, this wouldn't be a problem, but since i'm dealing with 2 buttons (one for higher, one for lower) i can't seem to get a good proper way to access the next empty index of the array.

     

    I simply want to get the next empty index of the array, because either arrayIndex[1] and arrayIndex[2] can be called under the button LOWER or HIGHER.

     

    Sorry if i'm being a little vague. I'm not very well in explaining the problems i deal with lol. If you didn't understand a word i said: i simply want to get the next empty array index.

     

    Oh and yeah i've looked at ArrayList (which is basically the answer), but i want to see if it's also possible using just the Array index.

     

     

    Thanks in advance!

    Link to comment
    Share on other sites

    Hmm, I think I understand what you're trying to communicate.

     

    If you're looking for the index which is null then that may be hard to do since with integers JAva usually sets them equal to 0 by default. My solution to this would be to create a method like getEmtpyIndex() which uses a for loop to go therough the array/ArrayList and find the first null/empty value....

    Link to comment
    Share on other sites

            List<Integer> arrayList = new ArrayList();
            for(int i = 0; i < arrayList.size(); i++){
            	if(arrayList.get(i) != null){
            		//Do stuff
            	}
            }
    

    I know this is using an ArrayList, but it should work nontheless

    Link to comment
    Share on other sites

    If your "empty" values are null, this will do it. You'll have to edit it when you put it in your class to make it work a bit.

     

    public static void main (String[] args) throws java.lang.Exception {
    	String[] x = {"C", null, "B", "D", null};
    	System.out.println(getNextEmpty(x, 0));
    }
    
    private static int getNextEmpty(Object[] x, int start) {
    	if (x[++start] != null) {
    		return getNextEmpty(x, start);
    	} else {
    		return start;
    	}
    }
    
    Produces: 1
    
    Link to comment
    Share on other sites

    Hmm, I think I understand what you're trying to communicate.

     

    If you're looking for the index which is null then that may be hard to do since with integers JAva usually sets them equal to 0 by default. My solution to this would be to create a method like getEmtpyIndex() which uses a for loop to go therough the array/ArrayList and find the first null/empty value....

    Yeah, i tried to use the default zero check (if array[1] == 0) but that wouldn't do much. It basically ignored the boolean and printed out the code that followed so yeah thats why i came here :P. Thanks for the help though!

     

            List<Integer> arrayList = new ArrayList();
            for(int i = 0; i < arrayList.size(); i++){
            	if(arrayList.get(i) != null){
            		//Do stuff
            	}
            }
    

    I know this is using an ArrayList, but it should work nontheless

     

    Yeah, something i don't really want to use. Well, i will use this eventually because it's the best one to go with but i was just curious if there was a way to use just an Array to check for the next empty element. Thanks for your help!

     

    If your "empty" values are null, this will do it. You'll have to edit it when you put it in your class to make it work a bit.

     

    public static void main (String[] args) throws java.lang.Exception {
    	String[] x = {"C", null, "B", "D", null};
    	System.out.println(getNextEmpty(x, 0));
    }
    
    private static int getNextEmpty(Object[] x, int start) {
    	if (x[++start] != null) {
    		return getNextEmpty(x, start);
    	} else {
    		return start;
    	}
    }
    
    Produces: 1
    

    Thank you for the help! I'm not going to use this, because it's a bit off what i want to achieve (just a quick empty check without too much hassle with methods). Thanks for your help really appreciated it though!

    Link to comment
    Share on other sites

    If I am getting this right you are filling the array with 3 guesses made by the computer. If this is the case all you need is a global counter variable that you increase when you place a value into the array. If that's not what you meant... then I have no clue what your question is :o

    Link to comment
    Share on other sites

    If I am getting this right you are filling the array with 3 guesses made by the computer. If this is the case all you need is a global counter variable that you increase when you place a value into the array. If that's not what you meant... then I have no clue what your question is :o

    Hehe let me explain it again:

     

    The computer is guessing a number the user has set. Let's say the user sets number 5.

    I made an Array with 3 elements, so that i can store (and show) the guessed numbers by the computer.

     

    The user sets number 5. Hits enter and the computer guesses a number, lets say 2. Array[0] now has number 2 stored in it.

    The user tells the computer via button HIGHER or button LOWER that the next number needs to be higher or lower, corresponding to the user number (5).

    When the user hits button HIGHER, the computer now guesses a number higher than 2. (i got all the code for this sorted of, so don't worry about that). The second number will be stored in Array[1]. But now comes the problem: the actionlistener for button LOWER and button HIGHER are seperate. This means that i can't know if the button LOWER or HIGHER is pressed for Array[1], meaning that i need to get the next empty index of the array instead of telling the code to store it in Array[1]. why? because either Array[1] can be called from button LOWER and button HIGHER, so if in example Array[1] is LOWER, then there has to be also an option to put a LOWER or HIGHER number in Array[2]. Without a getter i need to write code to store Array[1] and Array[2] under button LOWER AND ALSO under button HIGHER, because i don't know what the computer will guess.

     

    To clear things up from this (still) vague explanation, check this out:

    Chances are that the following will appear:

     

    1)

    Winning number = 5.

    Computer guess = 2 -> stored in array[0] by default because the first guess will always be stored in [0].

    Button HIGHER - computer guesses 3 -> stored in array[1]

    Button HIGHER - computer guesses 4 -> stored in array[2] but game over, reset (3 chances per game to guess).

     

    2)

    Winning number = 5.

    Computer guess = 2 -> stored in array[0].

    Button HIGHER - computer guesses 8 -> stored in array[1]

    Button LOWER - computer guesses 6 -> stored in array[2]

     

    The bold letters actually is the problem. At the 3rd guess from the computer, i never know if the button LOWER or HIGHER was clicked. So i don't know if the 3rd number, array[2], comes from button HIGHER or button LOWER. But since they are seperate actionlisteners/methods i want to GET the next empty array element, because i don't know if button HIGHER is clicked at the second or third guess, or if it's clicked at all!

     

    I think the last sentence made more clear what i actually want lol.

    Link to comment
    Share on other sites

    Hehe let me explain it again:

     

    The computer is guessing a number the user has set. Let's say the user sets number 5.

    I made an Array with 3 elements, so that i can store (and show) the guessed numbers by the computer.

     

    The user sets number 5. Hits enter and the computer guesses a number, lets say 2. Array[0] now has number 2 stored in it.

    The user tells the computer via button HIGHER or button LOWER that the next number needs to be higher or lower, corresponding to the user number (5).

    When the user hits button HIGHER, the computer now guesses a number higher than 2. (i got all the code for this sorted of, so don't worry about that). The second number will be stored in Array[1]. But now comes the problem: the actionlistener for button LOWER and button HIGHER are seperate. This means that i can't know if the button LOWER or HIGHER is pressed for Array[1], meaning that i need to get the next empty index of the array instead of telling the code to store it in Array[1]. why? because either Array[1] can be called from button LOWER and button HIGHER, so if in example Array[1] is LOWER, then there has to be also an option to put a LOWER or HIGHER number in Array[2]. Without a getter i need to write code to store Array[1] and Array[2] under button LOWER AND ALSO under button HIGHER, because i don't know what the computer will guess.

     

    To clear things up from this (still) vague explanation, check this out:

    Chances are that the following will appear:

     

    1)

    Winning number = 5.

    Computer guess = 2 -> stored in array[0] by default because the first guess will always be stored in [0].

    Button HIGHER - computer guesses 3 -> stored in array[1]

    Button HIGHER - computer guesses 4 -> stored in array[2] but game over, reset (3 chances per game to guess).

     

    2)

    Winning number = 5.

    Computer guess = 2 -> stored in array[0].

    Button HIGHER - computer guesses 8 -> stored in array[1]

    Button LOWER - computer guesses 6 -> stored in array[2]

     

    The bold letters actually is the problem. At the 3rd guess from the computer, i never know if the button LOWER or HIGHER was clicked. So i don't know if the 3rd number, array[2], comes from button HIGHER or button LOWER. But since they are seperate actionlisteners/methods i want to GET the next empty array element, because i don't know if button HIGHER is clicked at the second or third guess, or if it's clicked at all!

     

    I think the last sentence made more clear what i actually want lol.

     

    I'm kinda confused as to what your problem is. If you post the code you have, it might be easier. I don't understand why you wouldn't use what I posted for what you're describing. Pressing a different button shouldn't have a different behavior. You could make both your actionlisteners point to a singular method that handles the logic.

    Link to comment
    Share on other sites

    So yeah what I said would work perfectly. Have a global int counter. Set it to 0 initially. Then each time you add a value to the array increment it by 1. When acessing the array do array[counter]

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.