Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Need Debug Help!

    Alright, so i've been assigned a review program for my g12 programming class. g11 i programmed in C++, so its very similar. That said, I can't get one particular error out of my program, and it seems the teacher is having just about as much luck. If anyone here could try and fix it, or offer some other kind of help, I'd appreciate it greatly.

    Code:
    import java.io.*;
    
    public class IcsMarkbook
    {
        public static void methodOutput (int sortedMarks, double median, double average)
        {
            // method responsible for outputting the data and calculations.
    
    
            System.out.print ("\t Student \t Mark\n");  // \t tabs, \n goes down a line.
    
            for (int i = 0 ; i < 25 ; i++)
            {
                System.out.print ("\t " + i + " \t" + sortedMarks [i] + "\n"); // outputs the list of sorted marks, and what cell they are in.
            }
    
            System.out.print ("\n \f"); // \f adds a seperator
    
            System.out.print ("\t Median \t Average\n");
            System.out.print ("\t " + median + " \t" + average + "\n");
    
            System.out.print ("\n\n\t\t ~~ Mitchell Swirski ~~ ");
        }
    
        public static void randMarkGen () throws IOException
        {
            // 25 integer array, loop cycles through each cell, putting in a random number from 40 to 100. it saves them in marks.txt
            int array[] = new int [25];
    
            PrintWriter fileOut = new PrintWriter (new FileWriter ("marks.txt"));
    
            for (int i = 0 ; i < 25 ; i++)
            {
                array [i] = (int) (Math.random () * 100) + 40;          // Goes over 100, why?
                fileOut.println (array [i] + "\n");
            }
            fileOut.close ();
        }
        public static int sortMarks (int rawMarks[])
        {
    
            // the function which uses bubblesort to put the list of marks in decending order from highest to lowest.
    
            for (int pass = 1 ; pass < rawMarks.length - 1 ; pass++)   // controls the number of times algorithm analyzes the list
            {
    
    
                for (int element = 0 ; element < rawMarks.length - 1 ; element++) // controls the number of comparisons it will make
                {
    
                    //below: compares two elements. If the first is smaller then the second, they switch. This means the highest will be in the first cell.
    
                    if (rawMarks [element] < rawMarks [element + 1])
                    {
                        int hold = rawMarks [element]; // hold is used to assist in swapping the data from one cell to the other.
                        rawMarks [element] = rawMarks [element + 1];
                        rawMarks [element + 1] = hold;
    
                    }
                }
            }
    
    
            return rawMarks; // returns the array to the main. This becomes sortedMarks.
    
        }
     
        public static int markCalcs (int sortedMarks, int trigger)
         // does the calculations for median and average of the marks.
        {
    
            if (trigger = 1)  // if a 1 is sent with the list, then calc the median.
            {
                double median = sortedMarks [13];
    
                return median;   // returns median to the main function.
            }
    
            else // If something other than a 1 is sent with the list, then calculate the average.
    
                {
                    double total = 0; // collective total of the marks.
    
                    for (int i = 0 ; i < 25 ; i++)
                    {
                        total = total + sortedMarks [i]; //adds all the marks together
                    }
    
                    double average = total / 25; //divides by 25 to get the average
                    return average; // returns the average to the main program.
                }
        }
    
    
    
        public static void main (String[] args) throws IOException
        {
            BufferedReader readFile = new BufferedReader (new FileReader ("marks.txt")); // Used to read the file of Random Numbers.
    
            System.out.println ("\t Mitchell Swirski ~ 2008 - ICS Markbook\n");
    
    
            randMarkGen (); // Calls a random mark generator. Stores 25 marks ranging from 40-100 in a text file called marks.txt. Nothing is returned.
    
            int rawMarks[] = new int [25];  // the array the marks are stored in before they are sorted.
    
            for (int i = 0 ; i < 25 ; i++) // for loop used to cycle from cell 0 of the array to cell 25.
            {
                rawMarks [i] = Integer.parseInt (readFile.readLine ()); // parses the text file to fill the array
            }
    
            int sortedMarks[] = int sortMarks (int rawMarks); // calls the function sortMarks, which takes the unsorted marks and returns them sorted.
    
            // below; calls the markCalcs function, with the list of sorted marks, with 1 or 0 so that markCalcs can decide which to calculate.
            double median, average;
            int trigger1 = 1, trigger2 = 0;
            //median = int markCalcs (int sortedMarks, int trigger1);
            //average = int markCalcs (int sortedMarks, int trigger2);
    
            //methodOutput (int sortedMarks, double median, double average)
                // Calls the method that outputs median, average, and the list of marks.
        }
    
    
    // End of Program

  • #2
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    Can you please print the compile error, or the runtime error
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #3
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Heh, no problem. The compile error I get is "misplaced construct(s)" highlighting my main method line (public static void main (String [] args) throws IOException)

  • #4
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Any help?

  • #5
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    Well you are missing an ending bracket at the end of your code (to close the class, but I'm unsure if that is an error on your part or not). But I also see a lot of various errors in your code.

    I'm not sure if you're using an IDE, but you should use something like eclipse or netbeans.
    To give you an idea of the Errors I see..

    Code:
    public static int markCalcs (int sortedMarks, int trigger)
        {
    
            if (trigger = 1)  // should be ==
            {
                double median = sortedMarks [13];  // sortedMarks is an int, not an array
    
                return median;   // the function returns an int, NOT a double
            }
    
            else 
                {
                    double total = 0;
    
                    for (int i = 0 ; i < 25 ; i++)
                    {
                        total = total + sortedMarks [i]; // once again, not an array
                    }
    
                    double average = total / 25;
                    return average; // once again, returning a double, not an int
                }
        }
    Anyways, there are a lot of these.

    Can you please copy and paste the exact compiler error you're getting so we can figure out which error in particular you're talking about?

    P.S. You may want to switch Java Teachers :P
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #6
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Aradon View Post
    Well you are missing an ending bracket at the end of your code (to close the class, but I'm unsure if that is an error on your part or not). But I also see a lot of various errors in your code.

    * The error is mine, i just cut off the end bracket by mistake.


    I'm not sure if you're using an IDE, but you should use something like eclipse or netbeans.
    I'm using Ready to Program, recently became freeware.

    To give you an idea of the Errors I see..

    Code:
    public static int markCalcs (int sortedMarks, int trigger)
        {
    
            if (trigger = 1)  // should be ==  
            {
                double median = sortedMarks [13];  // sortedMarks is an int, not an 
     
                return median;   // the function returns an int, NOT a double
            }
    
            else 
                {
                    double total = 0;
    
                    for (int i = 0 ; i < 25 ; i++)
                    {
                        total = total + sortedMarks [i]; // once again, not an array
                    }
    
                    double average = total / 25;
                    return average; // once again, returning a double, not an int
                }
        }
    Anyways, there are a lot of these.
    * Yes, i know. I happened to post an older version by mistake, which sucks, but the error is still the same. The program isnt getting far enough for any of those errors to matter.

    Can you please copy and paste the exact compiler error you're getting so we can figure out which error in particular you're talking about?

    * I already have told you what the IDE is telling me. Unless there is somewhere different to get a specific code, I am not sure what you expect.

    P.S. You may want to switch Java Teachers :P
    *Turns out she runs all 4 online courses, and does another job for the board.
    I think its more a lack of time, then a lack of knowledge.
    Thanks for any help.

  • #7
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    I still need the exact error message that the java compiler is telling you (even if you have to run it outside the IDE). I also need your updated code. Without the updated code I can't possibly even attempt to reproduce the error on my own system.
    "To iterate is human, to recurse divine." -L. Peter Deutsch


  •  

    Posting Permissions

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