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 21 of 21

Thread: Array Issues

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array Issues

    Hi everyone,

    Only new to programming and having issues with arrays and inputing values into that array.

    My mission is to ... select a random number .... get the user to guess that random number ... and from what inputs the user has inputed, find the middle number of that array length.

    I have worked out how to generate the random number and guess the random number from a previous method, but i cant work out how to implement the guessed numbers into an array to find the middle number.

    Could someone please point me in the right direction .. as to how i would acomplish this .... i dont have the code i'm working on as im in transit ... but will post when available.


    Any help would be greatly appreciated


  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array Issues

    I don't know how your methods are set up, but if you have a global array of integers ( int[] guesses or something like that ), each the user inputs a guess, which has to be looped somehow I assume, you could add that value to the array. To do this, you increment a counter each time they guess, and make a statement in the loop where they guess (or method, if it's separate) such as guesses[counter] = guess (guess being the integer that was input by the user).

    This could be done with doubles in the same way, if you're using doubles.

  3. #3
    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: Array Issues

    find the middle number of that array length.
    how to implement the guessed numbers into an array to find the middle number
    Can you define what a "middle number" is? Perhaps give an example.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array Issues

    the middle number is the 'middle number of the array length ... e.g. 1 2 3 4 5 .. so 3 would be the middle number

  5. #5
    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: Array Issues

    Your example is an array with an odd number of elements. What if there are an even number?
    Is the middle element = (array.length+1)/2

    Or are you talking the the value of the element?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array Issues

    yes even numbers would be considered as well .. and the value of the element doesnt matter .. i just need the middle element of an array

  7. #7
    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: Array Issues

    Which is the middle element in an array with an even number of elements?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Apr 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array Issues

    from the information provided to us ... that the middle number of an array if an even number of elements existed, would be 2 elements e.g. 1 2 3 4 5 6 middle numbers = 3,4

  9. #9
    Junior Member
    Join Date
    Apr 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array Issues

    I'm still not understanding how this would be done .... could you provide some coding to help please arsparfven !

  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: Array Issues

    how to implement the guessed numbers into an array to find the middle number
    from what inputs the user has inputed, find the middle number of that array length.
    Not sure what those mean. The "middle" number(s) is a function of the size of the array. There is no need for guessed numbers to find it.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array Issues

    Sorry if ive confused people ... the guessed numbers are what a user inputs into the program to guess a random number(guessed numbers could be 1 to whatever until the user gets the correct random number). Those guessed numbers need to be implemented into an array to be able to find the middle number/s of that array.

  12. #12
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array Issues

    I know for just finding the middle index, you could use the line of code Norm put up for you..."middle element = (array.length+1)/2"
    If you have a count, you could use "count/2" as count would essentially be the same as the length.
    If you need two, you're going to have to check with some if statements.

    IF the length can be divisible by two (using the mod operation, like length % 2 = 0) THEN the middle is the length divided by two.
    IF the length cannot be divisible by two (length % 2 != 0) THEN the middle is floor(length/2) and floor(length/2) + 1
    Hopefully that makes sense, if not please ask

    If you are using integers, you can just use integer division (floor will not be necessary), if you are using doubles, you need to use floor. Floor is a method from the Math class. You need to import that above your class and the formal code would be Math.floor(operation). It's just a method of rounding, it will give you the lower integer, if you look it up, it's a very common java operation if you want to understand it in depth

  13. #13
    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: Array Issues

    Still not clear what you are trying to do with an array. Given an array, by your definition, the middle can be found from the length of the array.
    Where do the guessed numbers get involved?
    Can you give an example of what you mean?
    Say there is an array with 25 elements
    and the user guesses: 44, 33, 5,77,55,22,4,7
    What is supposed to happen?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Apr 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array Issues

    Hey Norm,

    From a previous method i have a 'random number' and a 'users guessed numbers' , lets say they users guesses are ... 65, 75, 70, 67, 69, 68 .. which the last element being 68 and the user has guessed the correct random number being the last element 68. I need to be able to implement those guessed number into an arrray but not sorted, so i can find the middle number/s e.g. 70 and 67.

  15. #15
    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: Array Issues

    The middle number would be found by some formula like: (the count of numbers entered+1/2) with a test if there are an even count and then there would be two middle numbers.
    An example: with 6 numbers entered: (6+1)/2 = 3 and because there are an even number also 4.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    Apr 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array Issues

    Any help ... I've worked out how to process the middle and median but i cannot work out how to continur to input numbers into the array list, it will one process one number ... I also cant work out how to stop adding numbers to the array if a preset value is entered .e.g randomNumber = 45.

    import java.util.Arrays;
    import java.util.Scanner;

    public class ArrayTest
    {
    Scanner input = new Scanner( System.in );

    int usersGuess;
    int randomNumber = 45;

    public void enter()
    {

    System.out.printf("Please enter numbers: ");
    usersGuess = input.nextInt();
    }

    public void testArray()
    {


    int testArray [] = new int[ 20 ];

    for ( int i = 0; i < testArray.length; i++ )
    testArray[ i ] = usersGuess ;

    System.out.println( "Middle is: " + testArray[ (testArray.length+1)\2] );

    System.out.println();

    Arrays.sort( testArray );

    System.out.println( "Median is: " + testArray[ testArray.length/2] );
    System.out.println();

    }


    }


    public class ArrayTestArgs
    {
    public static void main( String [] args )
    {
    ArrayTest arrayTest = new ArrayTest();

    arrayTest.enter();
    arrayTest.testArray();

    }

    }
    Last edited by daemonlies; April 28th, 2012 at 07:42 PM.

  17. #17
    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: Array Issues

    how to stop adding numbers to the array if a preset value is entered
    Use an if statement to test the value entered against the preset value and if equal, use the break statement to exit the loop.

    The order of the statements in the program do not make any sense. Before you write any more code you need to get organized. Make a list of what the code needs to do and the order it needs to do it. Write the list in pseudo code and post it. When the steps make sense then write the code.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    Last edited by Norm; April 28th, 2012 at 07:48 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Junior Member
    Join Date
    Apr 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array Issues

    Ok .. so what will i do about the input not continuing to add numbers .. if i enter say '5' it doesnt ask for anymore entered numbers it justs calucaltes the median and middle

  19. #19
    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: Array Issues

    Please read my last post, we crossed.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Junior Member
    Join Date
    Apr 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array Issues

    Hey Norm .. sorry but you mention about a puesdo list .. can you enlighten me on this please as i have no idea

  21. #21
    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: Array Issues

    Here's a sample of pseudo code for some program:
    begin loop
    ask user for input
    read input
    test that input is valid
    if not valid go to top of loop and ask again
    process input
    add input to list
    if list is full exit loop
    end loop

    The list of the above steps describes what that program is supposed to do.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Having Issues Wish this Program
    By userct in forum What's Wrong With My Code?
    Replies: 17
    Last Post: February 9th, 2012, 03:46 PM
  2. String and Bit Array conversion issues
    By GeekWarth in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 16th, 2011, 07:13 PM
  3. [SOLVED] Method issues
    By hello_world in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 13th, 2011, 06:45 PM
  4. Recursion issues.
    By ender16 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 30th, 2011, 09:03 PM