CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2012
    Posts
    1

    Average grade program

    i need help with my array ad parameters i don't know how to fix this program.
    This is a program that allows a user to enter class grades into an array of floats. The app will prompt for the total number of grades to be entered, then call a method to average the grades, and display the grades and the resulting average.

    This is my code
    import javax.swing.JOptionPane;
    public class Average {
    public static void main(String[]args)
    {
    String input,amount;
    double data[], gradetotal1;
    double sum=0,average;

    System.out.println("\tAverage Program");
    amount=JOptionPane.showInputDialog("Input Number of grades to enter");
    gradetotal1=Double.parseDouble(amount);

    data[]=gradetotal1;

    for (int i=0;i<data.length;i++){
    input=JOptionPane.showInputDialog("Enter grades");
    data[i]=Double.parseDouble(input);
    }
    for(int i=0;i<data.length;i++){
    sum+=data[i];

    average=sum/data.length;

    JOptionPane.showMessageDialog(null, "The total is:"+sum+"The Average is:"+average, JOptionPane.INFORMATION_MESSAGE);
    }
    }

    }

  2. #2
    Join Date
    Jan 2011
    Posts
    24

    Re: Average grade program

    Try this, now finding out what I have changed and why is your job

    Code:
    import javax.swing.JOptionPane;
    
    public class Average{
    
    	public static void main(String[] args) {
    		String input, amount;
    		double data[];
    		int gradetotal1;
    		double sum = 0, average = 0;
    
    		System.out.println("\tAverage Program");
    		amount = JOptionPane.showInputDialog("Input Number of grades to enter");
    		gradetotal1 = Integer.parseInt(amount);
    		data = new double[gradetotal1];
    
    		for (int i = 0; i < data.length; i++) {
    			input = JOptionPane.showInputDialog("Enter grades");
    			data[i] = Double.parseDouble(input);
    		}
    		for (int i = 0; i < data.length; i++) {
    			sum += data[i];
    
    			average = sum / data.length;
    
    		}
    		JOptionPane.showMessageDialog(null, "The total is: " + sum
    				+ " The Average is: " + average, "Result",
    				JOptionPane.INFORMATION_MESSAGE);
    	}
    
    }
    Last edited by javinpaul; March 26th, 2012 at 05:15 AM.

  3. #3
    Join Date
    Nov 2011
    Posts
    189

    Re: Average grade program

    data[] = new double(gradetotal1);

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Average grade program

    Code:
    data[] = new double(gradetotal1);
    What is that supposed to do?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Nov 2011
    Posts
    189

    Smile Re: Average grade program

    Just corrected a mistake he said data[] = gradetotal1; which is a mistake.

  6. #6
    Join Date
    Jan 2009
    Posts
    596

    Re: Average grade program

    Quote Originally Posted by cens View Post
    data[] = new double(gradetotal1);
    Quote Originally Posted by cens View Post
    Just corrected a mistake he said data[] = gradetotal1; which is a mistake.
    The correct code (which is in javinpaul's last post) is
    Code:
    double data[];
    ...
    data = new double[gradetotal1];
    You don't need the [] in the assignment, just in the declaration.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured