Reputation: 19
I am doing my class lab in java.
This program is to output winning team by summing total score. (there is blue team and white team in the input file with member's first name and each member's score. )
I have error bad operand types for binary operator.(sumArray) I think it is because int type + int []. can anybody help me out? if you see any other problem, please let me know. I guess there is lots of error except for this.
import java.io.*;
import java.util.*;
// declaration of the class
public class Bowling
{
// declaration of main program
public static void main(String[] args) throws FileNotFoundException
{
// 1. connect to input file
Scanner fin = new Scanner(new FileReader("bowling.txt"));
// declare arrays below
String Team, Member;
int teamw, teamb, Score;
// 2) initialize array accumulators to zero
teamw=0;
teamb=0;
int [] white_score = new int[3];
String [] white_member = new String[3];
int [] blue_score = new int[3];
String [] blue_member = new String[3];
// 3) display a descriptive message
System.out.println(
"This program reads the lines from the file bowling.txt to determine\n"
+ "the winner of a bowling match. The winning team, members and scores\n"
+ "are displayed on the monitor.\n");
// 4) test Scanner.eof() condition
while (fin.hasNext())
{
// 5) attempt to input next line from file
Member = fin.next();
Team = fin.next();
Score = fin.nextInt();
// 6) test team color is blue
// 7) then store blue member and score
// 8) increase blue array accumulator
// 9) else store white member and score
// 10) increase white array accumulator
if (Team.equals("Blue"))
{
blue_member[teamb]=Member;
blue_score[teamb] = Score;
teamb++;
}
else
{
white_member[teamw]= Member;
white_score[teamw]= Score;
teamw++;
}
}
// 11) if blue team score is larger
// 12) then display blue team as winner
// 13) else display white team as winner
if(sumArray(blue_score)>sumArray(white_score))
{
printArray("Blue", blue_member, blue_score);
}
else
{
printArray("White", white_member, white_score);
}
// 14 disconnect from the input file
fin.close();
}
public static int sumArray(int[] Score)
{
int sum=0;
for ( int i=0; i<Score.length; i++)
sum = sum+Score; ////HERE IS problem!!!!!!
return sum;
}
public static void printArray(String Team, String[] Member, int[] Score)
{
for(int i=0; i<Member.length; i++)
System.out.printf("winning team:"+Team+"\n"+Member+":"+Score);
}
}
Upvotes: 0
Views: 1035
Reputation: 4256
Just use:
sum += Score[i];
to add elements of the array inside for
loop.
You are getting error because you can not add int
with array. In Java you can not use +
operator directly with arrays.
Upvotes: 1