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

Thread: Program not executing correctly

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

    Default Program not executing correctly

    I'm having issues with executing a program that I have written. The program simply gets a user to input values whereby trying to guess a random number. I get to finding the median and the middle number but the program won't calculate them both. I need to use a insertion sort to be able to execute the median, but for some reason the program wont even do middle and median.

    Could someone have a look at my code and see what I'm doing wrong please! ...
    public void findMiddleNumber()
        {
            middleNumber = test[( guesses / 2 )]; 
     
            calculateMedian();
        }
     
        public void calculateMedian()
        {
            System.out.println( "Before:" );
            System.out.println( sort ); // print unsorted array
     
            insertionSort();
     
            System.out.println( "After:" );
            System.out.println( sort ); //
     
     
            if (( size % 2 ) == 0 ) 
               // even
                median = ( sort[ size / 2 ] + sort[ size / 2 - 1 ]) / 2.0;
     
            else 
                median = sort[ size / 2 ];
     
            findGuessedNumbers();
        }
     
        public void insertionSort()
        {
     
            sort = new int[ size ];
     
            for ( int i = 0; i < size; i++)
                sort[ i ] = inputGuess;
     
            sort();
        }
     
        public void sort()
        {
            int insert;
     
            for( int next = 1; next < sort.length; next++)
            {
                insert = sort[ next ];
     
                int moveItem = next;
     
                while( moveItem > 0 && sort[ moveItem -1 ] > insert )
                {
                    sort[ moveItem ] = sort[ moveItem -1 ];
                    moveItem--;
                }
                sort[ moveItem ] = insert;
     
            }   
     
        }
     
       public void findGuessedNumbers()
        {
            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: " 
                + median + "\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() );
                test[guesses] = inputGuess; 
                processGuessedNumber( inputGuess );
                guesses++;
     
     
     
     
            }
        }   
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Program not executing correctly

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/59151-program-not-executing-array-correctly.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


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

    Default Re: Program not executing correctly

    how do i delete thread

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Program not executing correctly

    Another: java - Trying to get input from JTextfield and use in another method - Stack Overflow

    Got the code posted here from someone else there.

    db

    Oh lookee, a double post on SO: http://stackoverflow.com/questions/1...ray-in-program
    Last edited by Darryl.Burke; May 1st, 2012 at 10:42 PM.

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

    Default Re: Program not executing correctly

    Im in need of help ... not criticism ... and its asking a different question thankyou

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Program not executing correctly

    Quote Originally Posted by daemonlies View Post
    Im in need of help ... not criticism ... and its asking a different question thankyou
    Its not criticism, it is help...I do recommend reading the last link I provided in post #2, as it might help you understand the point of linking to crossposts.

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

    Default Re: Program not executing correctly

    I understand your post on the 'cross posting' point ... and if i could i would have done it correctly in the first place ... and only posted once ... as for the stackoverflow, i have aready been in talks with the members from stack about my coding as they see that as a beginners level and should not have been posted on their site.

Similar Threads

  1. executing a process in backhand
    By jack_nutt in forum Java IDEs
    Replies: 1
    Last Post: November 9th, 2011, 08:58 PM
  2. Executing query from java program
    By ashu000 in forum JDBC & Databases
    Replies: 1
    Last Post: June 25th, 2011, 12:43 PM
  3. Executing a program on my computer with an ActionListener?
    By FoxBoiii in forum Java Theory & Questions
    Replies: 1
    Last Post: April 29th, 2011, 05:22 PM
  4. Executing Oracle Procedure
    By java_mein in forum JDBC & Databases
    Replies: 0
    Last Post: May 14th, 2010, 02:41 PM
  5. Help Executing C Program From Java! (Ubuntu)
    By penguinGirl in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 18th, 2010, 01:47 PM