Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 23 of 23

Thread: Need help displaying the location of array index

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Need help displaying the location of array index

    Hi all, just joined this forum and looking for a little advice with my code. My project was to create an array holding 10 integers and populate the array with 10 random numbers. Then ask the user to guess the number. Prompt the user with a while loop if their input is out of range. Determine if the users number is in the array, and display which index location the number is in. I got most of the code done but am having trouble displaying the index location. Any help is appreciated.

    import javax.swing.*;
     
    public class Homework4 {
     
    	public static void main(String[] args) {
     
    		int[] numarray = new int [10];		
    		char repeatcode = 'y';
     
    		for ( int i = 0 ; i < numarray.length ; i++ ) {
     
    			while ( repeatcode == 'y' ) {
     
    				numarray[i] = (int) (Math.random() * 100);
    				int userinput = Integer.parseInt(JOptionPane.showInputDialog ( "Guess a number between 1 and 100" ));
     
    				if ( userinput < 1 || userinput > 100 ) {
     
    					JOptionPane.showMessageDialog(null, "Your number wasn't in the parameters" );
    				}
    				else {
    					if ( userinput != numarray[i] ) {
     
    						JOptionPane.showMessageDialog(null, "THE NUMBER IS WRONG! Thanks for playing" );
     
    					} // ends if
    					else  {
     
    						JOptionPane.showMessageDialog(null, "WINNER!!!!\n\nYou guessed the number! Thanks for playing " + "\nThe number is stored at position " + numarray.length[i] );
     
    					} // ends else
     
    				}
    				repeatcode = JOptionPane.showInputDialog ( "Do you want to play again? (y/n)" ).charAt(0);
     
    			} // ends while loop
    		} // ends for loop
    	} // ends public static void main
    } // ends public class Homework4


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help displaying the location of array index

    having trouble displaying the index location.
    What variable holds the index location? What problem are you having displaying the contents of that variable?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help displaying the location of array index

    I have to create a variable that holds the index location? I didn't know that, how can i go about doing that? and where would i declare that variable?? I appreciate your help. Just keep in mind this is an intro class to java

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help displaying the location of array index

    What is in the variable: i?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help displaying the location of array index

    "i" would be the random number that gets inserted into the 10 index locations

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help displaying the location of array index

    Where is the i you are talking about defined?
    Where is i assigned the random values?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help displaying the location of array index

    isnt the for loop and math.random assigning random numbers to those locations? or should i just have the math.random only in the for loop?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help displaying the location of array index

    Should ALL the random numbers be set BEFORE the user is asked for input?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help displaying the location of array index

    yea it should populate the 10 random numbers. and keep on asking the user to guess the number, the numbers shouldn't change

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help displaying the location of array index

    What does the code do now?
    When does it completely fill the array with numbers?
    When does it ask the user for input?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help displaying the location of array index

    the code compiles and runs, but im not sure if its populating all 10 allocated spots.

    how can i test it to see if it does completely fill the array?

    If the number is negative or over 100 it prompts the user that their number was invalid. If gives me the wring number prompt and after a while if i guess that correct number it works. but im not sure if it completely fills all 10 or its just checking the first index position

  12. #12
    Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    47
    My Mood
    Bored
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default Re: Need help displaying the location of array index

    Quote Originally Posted by PCn3rd51 View Post
    the code compiles and runs, but im not sure if its populating all 10 allocated spots.
    I guess all the 10 locations are not getting populated. First location gets populated and then it asks the player to enter the number, then in next iteration the second location gets populated and it again asks the user for the input.

    Quote Originally Posted by PCn3rd51 View Post


    how can i test it to see if it does completely fill the array?
    To debug, you can print the values of the array in each iteration.

  13. The Following User Says Thank You to ankurt For This Useful Post:

    PCn3rd51 (February 10th, 2014)

  14. #13
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Need help displaying the location of array index

    You've got some logic errors I saw pretty early on. I do believe your assignment wants you to populate an array with 10 random numbers THEN start asking the user for their answers.
    The way you have yours setup, it will only ever have 1 number in the array on most occasions. You should have your while loop outside of your for loop.

  15. The Following User Says Thank You to Parranoia For This Useful Post:

    PCn3rd51 (February 10th, 2014)

  16. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help displaying the location of array index

    how can i test it to see if it does completely fill the array?
    Use this statement (with your array name) to print the contents of the array :
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    PCn3rd51 (February 10th, 2014)

  18. #15
    Junior Member
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help displaying the location of array index

    All of you have been a big help. I fixed it so that it is now generating 10 random numbers every time the while loop starts. My only problem now is that its saying that int i needs to be initialized. Its on the second if statment **** if ( userinput != numarray[i] ) ****

    You all have been a huge help and cant thank you enough

    import javax.swing.*;
     
    public class Homework4 {
     
    	public static void main(String[] args) {
     
    		int[] numarray = new int [10];
    		char repeatcode = 'y';
     
    			while ( repeatcode == 'y' ) {
     
    				for ( int i = 0 ; i < numarray.length ; i++ ) {
     
    					numarray[i] = (int) (Math.random() * 100);
    				}
     
    				System.out.println("The random numbers are: "+ java.util.Arrays.toString(numarray));
     
    				int userinput = Integer.parseInt(JOptionPane.showInputDialog ( "Guess a number between 1 and 100" ));
     
    				if ( userinput < 1 || userinput > 100 ) {
     
    					JOptionPane.showMessageDialog(null, "Your number wasn't in the parameters" );
    				}
    				else {
    					if ( userinput != numarray[i] ) {
     
    						JOptionPane.showMessageDialog(null, "THE NUMBER IS WRONG! Thanks for playing" );
     
    					}
    					else  {
     
    						JOptionPane.showMessageDialog(null, "WINNER!!!!\n\nYou guessed the number! Thanks for playing " + "\nThe number is stored at position "  );
     
    					}
     
    				}
    				repeatcode = JOptionPane.showInputDialog ( "Do you want to play again? (y/n)" ).charAt(0);
     
    			}
    	} //
    } //

  19. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help displaying the location of array index

    Where is the variable i defined and given a value?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #17
    Junior Member
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help displaying the location of array index

    well i would be one of the 10 random numbers. so how would i phrase my if statement if i want the userinput to not equal one of those 10 numbers?

  21. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help displaying the location of array index

    i would be one of the 10 random numbers
    One place I see i being used is in the for loop to index into the array where the random numbers are being stored. i does not contain a random number.

    userinput to not equal one of those 10 numbers?
    There needs to be a loop that iterates through all the elements in the array and compares the user input against each element in the array.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #19
    Junior Member
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help displaying the location of array index

    Quote Originally Posted by Norm View Post
    One place I see i being used is in the for loop to index into the array where the random numbers are being stored. i does not contain a random number.


    There needs to be a loop that iterates through all the elements in the array and compares the user input against each element in the array.
    It seems a little redundant to nest 10 if statements to test that, is there an easier way of doing that?


    if ( userinput != numarray[1] ) {
     
    	if userinput != numarray[2] ) {
     
    		if userinput != numarray[3] ) {
     
    			if userinput != numarray[4] ) {
     
    				if userinput != numarray[5] ) {
     
    					if userinput != numarray[6] ) {
     
    					}
    				}
    			}
    		}
    	}
    }

  23. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help displaying the location of array index

    easier way of doing that?
    use a for loop like where the array was filled. Instead of assigning a random number to the array element, compare it against the user's input.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #21
    Junior Member
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help displaying the location of array index

    Disregard my last post, wasn't thinking straight lol....


    for ( int arrayindex = 0 ; arrayindex < numarray.length ; arrayindex++ ) {
     
    					numarray[arrayindex] != userinput;
     
    				}

    and that would go in the else?

  25. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help displaying the location of array index

    The results of the condition being tested inside the loop is not being used for anything. The condition needs to be in an if statement.
    if(condition) {
       do this
    }else {
       do that
    }
    Then you need to decide what to do where "do this" and "do that" are written.
    If you don't understand my answer, don't ignore it, ask a question.

  26. #23
    Junior Member
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help displaying the location of array index

    SOLVED! Figured it out last night but it works perfectly. I want to thank all who helped me. Feel free to test it out for yourselves

    import javax.swing.*;
     
    public class Homework4a {
     
    	public static void main(String[] args) {
     
    		int[] numarray = new int [10];
     
    		for ( int i = 0 ; i < numarray.length ; i++ ) {
     
    			numarray[i] = (int) (Math.random() * 100 + 1);
    		}
     
    		System.out.println("The random numbers are: "+ java.util.Arrays.toString(numarray));
     
    		int userinput = Integer.parseInt(JOptionPane.showInputDialog ( "Guess a number between 1 and 100" ));
     
    		while ( userinput > 100 || userinput < 1 ) {
     
    			JOptionPane.showMessageDialog(null, "Your number wasn't in the parameters" );
    			userinput = Integer.parseInt(JOptionPane.showInputDialog ( "Guess a number between 1 and 100" ));
     
    		}
     
    		for ( int j = 0 ; j < numarray.length ; j++ ) {
     
    			if ( numarray[j] != userinput ) {
     
     
    			}
    			else {
     
    				JOptionPane.showMessageDialog(null, "WINNER!!!!\n\nYou guessed the number! Thanks for playing " + "\nThe number is stored at position " + j  );
    				return;
     
    			}
     
    		}
     
    		JOptionPane.showMessageDialog(null, "The answer is not in correct" );
     
    	}
     
    }

Similar Threads

  1. How to find the array index of the lowest value in the array?
    By namenamename in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 29th, 2013, 06:57 AM
  2. Array Help Needed- Testing if ArrayIsFull & Displaying Most Recently added Array data
    By FundamentalLearner in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 21st, 2013, 06:44 AM
  3. Linear Search / Index Location of Strings
    By xJavaLover in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 8th, 2013, 07:11 AM
  4. [SOLVED] Merge two 1D array's into a new 1D array. Trouble appending last index
    By norske_lab in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 30th, 2012, 12:57 PM
  5. first empty location in an array!?
    By keep smiling in forum Collections and Generics
    Replies: 2
    Last Post: February 10th, 2012, 07:29 AM

Tags for this Thread