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

    Default Trying to work out the middle number from an array

    Helllo everyone,

    Im having issues with trying to store values from a JTextfield located in method Game() and put them into an array located in middleNumber(). All i get when the program ends is the last input from the user which is the randomNumber.

    Could someone please have a look at my code and see where i'm gong wrong please


    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    
    public class Game extends JFrame
    {
        Container container;
        int currentDistance;
        
        int inputGuess;
        int lastDistance;
        
        int middleNumber;
            
        private JButton newGame;
        private JLabel message;
        private JLabel prompt; 
        private JLabel numberInput;
        private JTextField guessInput;
        private int countGuess;
        private int randomNumber;
        
        
          
        public Game()
        {
            super( "Play Guess A Number" );
            setLayout( new FlowLayout() );
            
            countGuess = 0;
                    
            prompt = new JLabel( "A random number 0 and 100 has been"
                    + " selected." );
            numberInput = new JLabel( "Write your number and press enter key: ");
            
            guessInput = new JTextField( 5 );
            guessInput.addActionListener( new GuessHandler() );
            message = new JLabel( "The result ." );
            
            
            newGame = new JButton( "Start New Game" );
            newGame.addActionListener( new ActionListener()
            {
                @Override
                public void actionPerformed( ActionEvent event )
                {
                   
                    guessInput.setText("");
                    guessInput.setEditable(true);
                                    
                    generateNumber();
                            
                }
            });
        
            container = getContentPane();
            container.add( prompt);
            container.add( numberInput );
            container.add( guessInput );
            container.add( message );
            container.add( newGame );
        
            setSize( 380, 150 );
            setVisible( true );
        
            generateNumber();
            
        }
        
        private void generateNumber()
        {
            randomNumber = ( int ) ( Math.random() * 100 );
            System.out.println( randomNumber );
        
        }        
           
        public void processNumber( int usersGuess )
        {
            countGuess++;
            
            if ( countGuess == 1 )
            {
                lastDistance = Math.abs( usersGuess - randomNumber );
                
                if ( usersGuess > randomNumber )
                    message.setText( "Higher than correst guess." );
                else
                    message.setText( "Lower than correct guess." );
                        
            }
        
            else 
            {
                currentDistance = Math.abs( usersGuess - randomNumber );
                
                if ( usersGuess > randomNumber )
                {
                    message.setText( "Higher than correct guess." );
                    
                    lastDistance = currentDistance;
                
                }
            
            else if ( usersGuess < randomNumber )
            {
                message.setText( "Lower than correct guess." );
                                       
                lastDistance = currentDistance;
            
            }
            
                else
                {
                    message.setText( "Congratulations, your guess is correct!" );
               
                    guessInput.setEditable( false );
                    countGuess = 0;
                    
                    findMiddleNumber();
                }
            }
        }
        public void MiddleNumber()
        {
            int num_of_guesses = 0; 
                    
            Scanner inputs = new Scanner( System.in );
            
            int [] testArray = new int[ 20 ]; 
            
            do 
            { 
                if ( inputGuess != randomNumber ) 
                { 
                    inputGuess  = inputs.nextInt();      
     
                } 
                else  
                    testArray[ num_of_guesses ] = inputGuess; 
                    num_of_guesses++; 
     
                    if ( inputGuess >= randomNumber) 
                    { 
                        break;      
                    }  
     
            }  
            while ( inputGuess != randomNumber); 
            
            middleNumber = testArray[( num_of_guesses)/ 2]; 
            
            System.out.printf( "\nMiddle is: " + middleNumber);
     
            finalOutput();
        }
        
        
        public void finalOutput()
        {
            JOptionPane.showMessageDialog( null,"Congratulations, your guess "
                + "is correct." + "\n\n1. Middle number from all guessed "
                + "numbers by the user is: " + middleNumber + "\n\r2. Median "
                + " value of all guessed numbers by the user is: " 
                + medianValue + "\n\r3. A position (array index) of correctly "
                + "guessed number in sorted array is: " + positionIndex, 
                "Final Output", JOptionPane.PLAIN_MESSAGE );  
        
        }
        
        public static void main( String [] args )
        {
            GuessGame application = new GuessGame();
            application.setDefaultCloseOperation( EXIT_ON_CLOSE );
                       
        }        
        
        class GuessHandler implements ActionListener
        {
            @Override
            public void actionPerformed ( ActionEvent event )
            {
                inputGuess = Integer.parseInt( guessInput.getText() );
                processGuessedNumber( inputGuess );
                
            }
        }   
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default Re: Trying to work out the middle number from an array

    Can you explain where it is you are getting the numbers? Add some printlns to the code that print out what numbers are being input and what numbers are being accessed and used so that the print out shows the problem.
    Copy and paste the print out here with some comments showing what the problem is.

    The array in the middleNumber method is local to that method. When the method exits, the array goes away. Make the array a class variable if you want to save its contents across calls to the method.
    Last edited by Norm; 04-30-2012 at 01:01 PM.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Trying to work out the middle number from an array

    You never initialize inputGuess before you enter the loop in "MiddleNumber()"... that is probably your fault as you will exit immediately... right?

    EDIT: Show us processGuessedNumber() function and print out yourself the num_of_guesses after your loop to see if that is correct
    Last edited by Sierra; 04-30-2012 at 02:31 PM.
    I like likes!

Similar Threads

  1. Finding MIddle number help
    By rosanator10 in forum New To Java
    Replies: 6
    Last Post: 02-01-2012, 12:55 AM
  2. Replies: 15
    Last Post: 06-22-2011, 07:47 PM
  3. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  4. Finding a number in array close to another number
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 12:37 AM
  5. My Simple Array Does Not Work!
    By Simplev_v in forum New To Java
    Replies: 16
    Last Post: 09-07-2009, 02:43 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
  •