Pouyaan Pf Pouyaan Pf - 21 days ago 8
Java Question

bad syntax method sumArray() and PrintArray(), No output as expected

I am doing my lab and Below is the lab description.

The problem is with the output, I am not getting what i was intended to get. Could you help me out please?

Write a program in a file named Bowling7.java that uses parallel arrays to store information that is on each line of the input file bowling.txt. Each line of that file contains a bowling team member’s first name (String), one of two bowling team names (String data equal to either "Blue" or "White"), and that bowling team member’s score in a bowling match (int). Each line of the file contains a unique bowling team member so that you do not need to worry about repeated team member’s names or embedded blanks within the field. Also, each team is guaranteed to have the same number of team members. Your program should use a method to sum the scores of each of the two teams. Once you determine the maximum score, your program should display on the monitor a message as to which team won and then use a method to display each of the winning team member’s name and score. The parallel array strategy implies that you will need 2 arrays of type String for the team member’s name and 2 arrays of type int for the team member’s score.

// java class for file I/O

import java.io.*;
import java.util.*;

// declaration of the class


public class Bowling7
{
// declaration of main program

public static void main(String[] args) throws FileNotFoundException
{

String[] blue_members = new String[10], white_members = new String[10];
int[] blue_scores = new int[10], white_scores = new int[10];


// 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;

// 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

if (Team.equals("blue"))
{

// 7) then store blue member and score

blue_member[teamb] = Member;
blue_score[teamb] = Score;
teamb++;

// 8) increase blue array accumulator

}

// 9) else store white member and score
else {
white_member[teamw]= Member;
white_score[teamw]= Score;
teamw++;
}

// 10) increase white array accumulator

// 11) if blue team score is larger

if(sumArray9blue_score>sumArray(white_score))

// 12) then display blue team as winner

{
printArray("blue", blue_member, blue_score);
}

// 13) else display white team as winner

else
{
printArray("white", white_member, white_score);
}

// 14 disconnect from the input file

fin.close();

}

// implement method sumArray() below
/* 1. initialize accumulator to 0
2. loop over initialized array indices
3. increase accumulator by indexed array element
4. return accumulator
*/

public int sumArray(int[] Score)
{
int sum=0;
for (int i=0; i<Score.length; i++)
sum= sum+=Score[i];
return sum;
}


// implement method printArray() below
/* 1. display the team name as the winner
2. loop over initialized array indices
3. display member and score for that array index
*/

public static void printArray(String Team, String[] Member, int[] Score)
{
for(int i=0; i<Member.length; i++)
System.out.printf("wining team: "+ Team + "\n" + Member + ":" + Score);
}

} // end of the class


The last two parts do not compile, and gives some syntax errors that dont make sense.

Answer

Your issues is you have mislabeled your variables:

 //add 's' to all these variables.
 blue_members[teamb] = Member;
           blue_scores[teamb] = Score;
           teamb++;

       // 8) increase blue array accumulator

       }

       // 9) else store white member and score
 else  {
white_members[teamw]= Member;
white_scores[teamw]= Score;  

You also have a typo here:

sumArray9blue_score>

You need to make it:

sumArray(blue_score)

All your blue/white variables are singular instead of plural. You also need to make your sumArray method static. I just fixed all your code. You can copy the code below and it will compile. I make not guarantee that it will run the way you expect it too:

// java class for file I/O

    import java.io.*;
    import java.util.*;

// declaration of the class


public class Bowling7
{
    // declaration of main program

    public static void main(String[] args) throws FileNotFoundException
    {

    String[] blue_members = new String[10], white_members = new String[10];
    int[] blue_scores = new int[10], white_scores = new int[10];


    // 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;

        // 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

           if (Team.equals("blue")) 
           {

           // 7) then store blue member and score

              blue_members[teamb] = Member;
               blue_scores[teamb] = Score;
               teamb++;

           // 8) increase blue array accumulator

           }

           // 9) else store white member and score
     else  {
    white_members[teamw]= Member;
    white_scores[teamw]= Score;            
     teamw++;
           }

          // 10) increase white array accumulator

        // 11) if blue team score is larger

    if(sumArray(blue_scores) > sumArray(white_scores))

          // 12) then display blue team as winner

        {
        printArray("blue", blue_members, blue_scores);
        }

          // 13) else display white team as winner

    else
        {
        printArray("white", white_members, white_scores);
        }

    // 14 disconnect from the input file

    fin.close();
           }

    }

// implement method sumArray() below
/* 1. initialize accumulator to 0
   2. loop over initialized array indices
      3. increase accumulator by indexed array element
   4. return accumulator
*/

     public static int sumArray(int[] Score)
     {
     int sum=0;
        for (int i=0; i<Score.length; i++)
        sum= sum+=Score[i];
    return sum;
     }


// implement method printArray() below
/* 1. display  the team name as the winner
   2. loop over initialized array indices
      3. display member and score for that array index
*/

    public static void printArray(String Team, String[] Member, int[] Score)
    {
    for(int i=0; i<Member.length; i++)
        System.out.printf("wining team: "+ Team + "\n" + Member + ":" + Score);
    }

} // end of the class
Comments