1

I am struggling with Arrays (java)

Since this is the first time to learn about java in my life, I have no idea how to start it. I have just learned how to declare arrays and so on. However, this is too complicated for me. I think I can get guidelines if I see this answer. Can anybody help me out?

the prog

4
  • 2
    Homework? :) I'd suggest using a class to represent each team member rather than 3 separate variables. You can then create each team member object as you load them from file, and add this object to an array. Your function would then need to loop through this array (or use lambda) to calculate the scores (use an if statement to determine which team to add the score to); then store the winning team's color as a string value, loop again to display all team members that match that color (again, using an if statement). Does this help? Commented May 5, 2016 at 20:54
  • 1
    You have your variable names Capitalized, which is very much not usual in Java; it's going to make it harder to read, and your instructor will take points off. Make "Team, Member, and Score" lowercase. (team, member, and score.) Commented May 5, 2016 at 21:08
  • 1
    Also, in your code sample... you're not using any arrays, yet. Commented May 5, 2016 at 21:08
  • 1
    Also, if it is required to use an array rather than an ArrayList, you will need to re-create your array continuously, as you do not know how many team members you're loading. It's not difficult, take a look at system.arraycopy() Commented May 5, 2016 at 21:15

2 Answers 2

1

Read the java.util.Scanner (API). Your code works. You can do what you want with the scores, just read them from the file and cast them to the appropriate data types (integers) for calculating average scores etc.

The variables are strings, so you must cast them to numbers to make your calculation.

You can use arraylist ArrayList<TeamMember> as an instance variable of TeamMembers for your Teams or a collection with primitive types for the team but I think best is if you make classes for Team, TeamMember and Score that you instanciate in your Bowling class. You can find this information anywhere.

import java.util.Scanner;

//...
Team usa = new Team();
Team mexico = new Team();
TeamMember person = new TeamMember(usa); //Harry plays for the US
...
Scanner in = new Scanner(System.in);
int num = in.nextInt();

If you know that every third input is a score then you can check modulo 3 (% 3) to know which iteration is divisible by 3. .

Sign up to request clarification or add additional context in comments.

4 Comments

...why would Score be a separate class? Is it not a field in both TeamMember (individual score), and Team (if you're implementing that class, a cumulative score?).
@CoolBots You might want to compare scores with a custom comparator?
Are they not integers? I personally think it's a bit overkill to create a Score class and custom comparator for what essentially amounts to an int... Do you have a use case for such custom comparator?
@CoolBots Yes but if you know only a score you might want to know more about the game just from the score.
0

I have done about 40% of your request, i think that's enough for you to go on. You should be able to finish it on your own.

If you have further question,just leave a comment. ( There are some hidden bugs for you,to handle it you should have an understanding of scope first, just for your learning. )

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

// declaration of the class
public class Bowling2 {

// declare arrays below
String Team, Member;
int Score, Scorew, Scoreb;
int[][] teamw = new int[10][3];
int[][] teamb = new int[10][3];

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

// 2) initialize array accumulators to zero
int i, j, Scoreb, Scorew = 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.toString() == "blue" ){
            // 7) then store blue member and score
            teamb[i][0] = Member;
            teamb[i][1] = Team;
            teamb[i][2] = Score;
            // 8) increase blue array accumulator
            sumArray("blue");
            i++;
        }
        // 9) else store white member and score
        else { 
            teamw[j][0] = Member;
            teamw[j][1] = Team;
            teamw[j][2] = Score;
            // 10) increase white array accumulator
            sumArray("white");
            j++;
        }                     
    }

    // 11) if blue team score is larger

    // 12) then display blue team as winner

    // 13) else display white team as winner

// 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 double sumArray(string color) {
    if (color == "blue") {
        for (int k = 0;k < teamb.length(); k++) {
            //do the calculation here, and return it
        }
    }else {
        for (int h = 0;h < teamw.length(); h++) {
            //do the calculation here, and return it
        }
    }
} 

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.