| | |
Accumulator
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
I'm supposed to be taking all the averages of the students grades and create an overall average...but it's not working
import java.io.*;
import java.util.*;
public class Grade
{
public static void main(String[] args) throws IOException
{
String []name = new String[50];
int num1, num2, num3;
Scanner inFile =
new Scanner(new FileReader("Average.txt"));
int []average = new int[50];
double sum;
int c = 0;
while(inFile.hasNext())
{
name[c] = inFile.next();
num1 = inFile.nextInt();
num2 = inFile.nextInt();
num3 = inFile.nextInt();
average[c]=((num1 + num2 + num3)/3);
c++;
}
for (int i = 0; i < c; i++)
{ System.out.println(name[i] + " " + average[i]);
}
for (int i = 0; i < average.length; i++)
{
sum += average[i] * (c));
}
}
}
it's the sum that isn't working...any ideas?...sorry to be so vague
import java.io.*;
import java.util.*;
public class Grade
{
public static void main(String[] args) throws IOException
{
String []name = new String[50];
int num1, num2, num3;
Scanner inFile =
new Scanner(new FileReader("Average.txt"));
int []average = new int[50];
double sum;
int c = 0;
while(inFile.hasNext())
{
name[c] = inFile.next();
num1 = inFile.nextInt();
num2 = inFile.nextInt();
num3 = inFile.nextInt();
average[c]=((num1 + num2 + num3)/3);
c++;
}
for (int i = 0; i < c; i++)
{ System.out.println(name[i] + " " + average[i]);
}
for (int i = 0; i < average.length; i++)
{
sum += average[i] * (c));
}
}
}
it's the sum that isn't working...any ideas?...sorry to be so vague
I think what you probably need to do is sum all of the students' scores and then divide by the number of students. The average should be either a float or a double. Since all the scores are presumably int's and the number of students is an int. In Java an int/int calculation equals an int, so you will need to perform a cast.
double average = (double) sum / numStudents;
double average = (double) sum / numStudents;
I need all the averages I collected in the array to be added together and divided by the number of students (c) to equal the sum. I just don't know how to write it out.
The problem isn't the logic it's just how to do it...whenever I type in something like sum += average[i] / c it says sum may not be initialized...I don't know how to put it.
The problem isn't the logic it's just how to do it...whenever I type in something like sum += average[i] / c it says sum may not be initialized...I don't know how to put it.
Last edited by cat8882; Nov 15th, 2007 at 4:57 pm.
Initialize sum to 0 when you declare it. The average is just
Java Syntax (Toggle Plain Text)
for (int i = 0; i < average.length; i++) { sum += average[i]; } double overallAvg = sum / c;
![]() |
Similar Threads
- intergrating ASM to C++ (C++)
- Loops (C++)
- The problem with using Java's maths.cos() (Java)
- Powering 11 computers (Cases, Fans and Power Supplies)
- system("PAUSE") (C++)
- To all the member (Java)
- reading a file into code (Java)
- Please Check The Errors'..Dont know what else to do (C++)
- C++ Syntax (C++)
- Duplication (C++)
Other Threads in the Java Forum
- Previous Thread: Testing something with classes
- Next Thread: Sorting
| Thread Tools | Search this Thread |
actuate android api applet application applications array arrays automation balls bank binary bluetooth business chat class classes clear client code codesnippet collections component database db defaultmethod development dice dragging draw ebook eclipse error event exception formatingtextintooltipjava fractal game givemetehcodez graphics gui html ide image infinite input integer invokingapacheantprogrammatically j2me java javaprojects jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie numbers openjavafx oracle parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads time tree windows






