Results 1 to 3 of 3
  1. #1
    cupOJava is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Help with Code, Please?

    I am working on a homework assignment for class and part of that is to find the mode of user input numbers. I keep getting errors w/n my mode code. Could anyone look at what I may be missing. The mode code is at the end. Thanks in advance.

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

    public class GradeBook {

    /**
    * @param args
    */
    public static void main(String[] args)
    {

    Scanner input = new Scanner(System.in);

    int[] scores = new int[21]; //declaring and setting the array for grades.

    int n = 0; //declaring number of scores entered

    double totalScores = 0; //declaring total of all scores entered
    double avgScore = 0; //declaring average of all scores


    System.out.println("Please enter up to 20 grade values from 0 to 100, inclusively."); //prompt for grades to be entered
    System.out.println("Enter -1 when you are done entering grades."); //prompt to stop entering grades



    for (int i = 1; i < scores.length; i++) //loop to continue to ask for new grade
    {
    System.out.print("Enter grade #" + i + ": ");
    scores[i] = input.nextInt();

    if (scores[i] == -1)
    {


    break;
    }

    while (scores[i] < 0 || scores[i] > 100) //limit to scores that can be entered
    {


    System.out.println("INVLALID! Re-enter grade #" + i + ":" ); //prompt for invalid number entry

    Scanner invalidInput = new Scanner(System.in);
    scores[i] = invalidInput.nextInt();

    }
    n = i;
    totalScores = totalScores + scores[i]; //calculation for all scores
    avgScore = (totalScores / n); //calculation for average of scores

    }

    System.out.println("You entered " + n + " grades: "); //prompt telling user number of grades entered
    System.out.println("You entered scores of: " + scores);
    selectionSort(scores); //sorting of grades in ascending order
    System.out.println(); //space b/w lines

    System.out.println("The mean of your scores is: " + avgScore); //average score prompt
    System.out.println("The Meadian of your scores is: " + median(scores)); //average median prompt
    System.out.println("The Mode of your scores is: " + mode); //average mode prompt

    }

    public static void selectionSort(int[] list) //sort process code
    {
    for (int i = 0; i < list.length; i++)
    {

    int min = list[i];
    int minIndex = i;
    for (int j = i + 1; j < list.length; j++)
    {
    if (min > list[j])
    {
    min = list[j];
    minIndex = j;
    }
    }


    if (minIndex != i)
    {
    list[minIndex] = list[i];
    list[i] = min;
    }

    }
    for (int i = 0; i < list.length; i++)
    System.out.print(list[i]);


    }


    public static int median(int [] list) //find median grade code
    {
    Arrays.sort(list);
    int middle = list.length / 2;
    if(list.length % 2 ==1)
    {
    return list[middle];

    }
    return (list[middle-1] + list[middle]);
    }

    public static int mode(int[] scores) //find mode of grades code.
    {
    int num = 0;
    for(int x = 0; x < scores.length; x++)
    {
    for(int num2 = 1; num2 < scores.length - x; num2++)
    if(scores[num2 - 1] > scores[num2])
    {
    num = scores[num2 - 1];
    scores[num2 - 1] = scores[num2];
    scores[num2] = num;
    }

    int mode = scores[0];
    int tempMode = 1;
    int tempMode2 = 1;

    for(int x1 = 1; x1 < scores.length; x1++)
    {
    if(scores[x1 -1] == scores[x1])
    {
    tempMode++;
    }
    else
    tempMode = 1;

    if(tempMode >= tempMode2)
    {
    mode = scores[x1];
    tempMode2 = tempMode;
    }

    }

    return mode;
    }
    return num;


    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Help with Code, Please?

    Find and go through the Forum Rules, particularly the third paragraph. Also find the FAQ that tells you how to use BBCode to retain the formatting of your code.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Help with Code, Please?

    Also, when seeking help with errors, post the entire error message or stack trace and indicate which line of your code is the cause.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 1
    Last Post: 03-25-2012, 04:40 AM
  2. Replies: 16
    Last Post: 01-31-2012, 09:36 PM
  3. Replies: 3
    Last Post: 08-10-2011, 10:17 AM
  4. Replies: 0
    Last Post: 08-07-2011, 09:32 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 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
  •