Results 1 to 2 of 2

Thread: Array issue

  1. #1
    daemonlies is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Array issue

    Hey Everyone,

    Only new to this so here goes ... i want to add number/s to an array by a user until a predetermined number 'randomNumber' has been selected, then print the middle and median of that selected array group..

    The coding works for the user inputs but i get nothing from the middle or median ... it just says '0'

    Could anyone please help me with this ....



    Java Code:
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class ArrayTest 
    {
            Scanner input = new Scanner( System.in );
            
            int usersGuess;       
            int randomNumber = 45;
            
        public void enterNumbers()
        {
            do
            {
                System.out.printf("Please enter numbers: ");
                usersGuess  = input.nextInt();
            
                if ( usersGuess != randomNumber )
                {
                    System.out.printf("Please enter numbers: ");
                    usersGuess  = input.nextInt();     
                                      
                }
                else 
                      System.out.println("Correct guess");   
            
            } 
            while ( usersGuess != randomNumber );
            
       
                   
            int testArray []  = new int[ 20 ];
            
            boolean correctGuess = false;
                   
            for ( int i = 0; i < testArray.length; i++ )
            {
                testArray[ i ] = usersGuess ;
            
                if ( testArray[ i ] == randomNumber )
                {
                    correctGuess = true;
                    break;
            
                }
            }
                    
            System.out.println();System.out.println( "Middle is: " + testArray
                    [ ( testArray.length + 1)/2] );
                    
            Arrays.sort( testArray );
            
               
            System.out.println( "Median is: " + testArray[ testArray.length/2] );
            System.out.println();
            
        }
         
    }
    Java Code:
    public class Test 
    {
        public static void main( String [] args )
        {
            ArrayTest arrayTest = new ArrayTest();
            
            arrayTest.enterNumbers();
            
            
        
        }
        
    }

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Array issue

    First: Do not put two statements into the same line
    Second: Print out your values at some locations in your code for debugging. Start with the "usersGuess" before you enter the for() loop
    Third: This is too complicated, keep it simple - simple code is less buggy:
    Java Code:
            do
            {
                System.out.printf("Please enter numbers: ");
                usersGuess  = input.nextInt();
             
                if ( usersGuess != randomNumber )
                {
                    System.out.printf("Please enter numbers: ");
                    usersGuess  = input.nextInt();    
                                       
                }
                else
                      System.out.println("Correct guess");  
             
            }
            while ( usersGuess != randomNumber );
    Remove the duplicate code, there is a way... :)
    Last edited by Sierra; 04-30-2012 at 01:25 AM.

Similar Threads

  1. Array game issue
    By Arom in forum New To Java
    Replies: 2
    Last Post: 11-30-2011, 09:33 PM
  2. Issue with setting values in an array
    By pyat77 in forum New To Java
    Replies: 4
    Last Post: 11-04-2011, 03:01 AM
  3. Java Beginner: Can't Figure out Loop Array Issue.
    By CMDR Reservoir in forum New To Java
    Replies: 4
    Last Post: 07-19-2011, 03:30 AM
  4. Sorting Array Issue
    By larson1118 in forum New To Java
    Replies: 2
    Last Post: 04-21-2011, 06:31 PM
  5. BufferedReader File into Array Issue
    By LifeElixer in forum New To Java
    Replies: 4
    Last Post: 04-13-2011, 01:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •