Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, I have a question I have a program and I am suppose to allow a user to selec

ID: 3787974 • Letter: H

Question

Hi, I have a question I have a program and I am suppose to allow a user to select bettwen differnt reports or outputs and I was wondering how I would be able to implement that. I have tried putting if and case statments but I keep getting an error I think it is because its part of the while loop I have. Here is the code that I have:

public class Bowler
{
String bowlerNumber;
int score1,score2,score3;
public String getBowlerNumber()
{
return bowlerNumber;
}
public void setBowlerNumber(String bowlerNumber)
{
this.bowlerNumber = bowlerNumber;
}
public int getScore1()
{
return score1;
}
public void setScore1(int score1)
{
this.score1 = score1;
}
public int getScore2()
{
return score2;
}
public void setScore2(int score2)
{
this.score2 = score2;
}
public int getScore3()
{
return score3;
}
public void setScore3(int score3)
{
this.score3 = score3;
}
  
}

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Scanner;

public class BowlerReports {
  
public static void main(String args[])
{
ArrayList<Bowler> scores;

// HashMap with key as Team No and Value as ArrayList of Bowlers
HashMap<Character, ArrayList<Bowler>> map = new HashMap<Character, ArrayList<Bowler>>();
try{
//Reading each value from file using FileReader
// and storing in StrinngBuffer
   Scanner s = new Scanner(System.in);
FileReader fr = new FileReader("Scores.txt");
BufferedReader br= new BufferedReader(fr);
String line;
StringBuffer sb = new StringBuffer();
while((line=br.readLine())!=null){
sb.append(line);
}

//Converting into string and splitting using comma
String data[] = sb.toString().split("," + " ");
int i = 0;
Bowler b;
  
//Iterating array by dividing each player and storing into ArrayList
// depends upon first character
//Storing into Map needs Key as First Character of ID i.e 1,2,3,4
//If key repates then appending to ArrayList
while(data.length > i){
scores = new ArrayList<Bowler>();
b = new Bowler();
b.setBowlerNumber(data[i]);
b.setScore1(Integer.parseInt(data[i+1]));
b.setScore2(Integer.parseInt(data[i+2]));
b.setScore3(Integer.parseInt(data[i+3]));
i=i+4;
scores.add(b);

//If Key repeats adding to list
if(map.containsKey(b.getBowlerNumber().charAt(0))){
ArrayList<Bowler> sl = map.get(b.getBowlerNumber().charAt(0));
sl.add(b);
map.put(b.getBowlerNumber().charAt(0), sl);
}

else
{
//Otherwise add to List normally
map.put(b.getBowlerNumber().charAt(0),scores);
}
}
// Getting Key and Value from HashMap using entrySet
//Iterating each Bowler using Entry in Map
Set<Entry<Character, ArrayList<Bowler>>> set = map.entrySet();
Iterator<Entry<Character, ArrayList<Bowler>>> itr = set.iterator();
while(itr.hasNext()){
Entry<Character, ArrayList<Bowler>> entry = itr.next();
ArrayList<Bowler> als = entry.getValue();
Iterator<Bowler> bow = als.iterator();
int sum1,sum2,sum3;
sum1=sum2=sum3=0;
int gt=0;
while(bow.hasNext()){
Bowler bb = bow.next();


//Each Bowler Individual Score

//Below Line is for Output-1
System.out.println(bb.getBowlerNumber()+" "+bb.getScore1()+" "+bb.getScore2()+" "+bb.getScore3());
float avg = (bb.getScore1()+bb.getScore2()+bb.getScore3())/3;
//Average Score of Each Bowler

  
//Below Line is for Output-2
System.out.println("Average Score of bowler"+bb.getBowlerNumber()+" is: "+avg);
sum1 = sum1 + bb.getScore1();
sum2 = sum2 + bb.getScore2();
sum3 = sum3 + bb.getScore3();
gt = gt + bb.getScore1()+bb.getScore2()+bb.getScore3();

}
//Team Each Game Score


//Below Line is for Output-3
System.out.println("Team-"+entry.getKey()+" games score are: "+sum1+" "+sum2+" "+sum3);
// Grand Total of Each Teams

//Below Line is for Output-4
System.out.println("Grand total for Team-"+entry.getKey()+" is: "+gt);
}

}
catch(Exception e)
{
e.printStackTrace();
}
  
}
}

Any help would be awsome!

Explanation / Answer

Comments : Edited the BowlerReports.java and pasted the same only. Made your code modular and added cases to get the input from user to select whether to generate complete report or for a particular team. 'A' is a team name I kept in my Scores.txt. You can add up your own choices and respond to the user input. Hope this answers your questions.

Note: Made the change in split method, since I have space separated text. My Scores.txt file I have pasted at the bottom. May be this could be the reason you are getting error.

package bowlerreports;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Scanner;
public class BowlerReports {
  
static HashMap<Character, ArrayList<Bowler>> map;

public static void main(String args[])
{
ArrayList<Bowler> scores;

// HashMap with key as Team No and Value as ArrayList of Bowlers
map = new HashMap<Character, ArrayList<Bowler>>();
try{
//Reading each value from file using FileReader
// and storing in StrinngBuffer
Scanner s = new Scanner(System.in);
FileReader fr = new FileReader("Scores.txt");
BufferedReader br= new BufferedReader(fr);
String line;
StringBuffer sb = new StringBuffer();
while((line=br.readLine())!=null){
sb.append(line);
}

//Converting into string and splitting using comma
String data[] = sb.toString().split(" ");
int i = 0;
Bowler b;
  
//Iterating array by dividing each player and storing into ArrayList
// depends upon first character
//Storing into Map needs Key as First Character of ID i.e 1,2,3,4
//If key repates then appending to ArrayList
while(data.length > i){
scores = new ArrayList<Bowler>();
b = new Bowler();
b.setBowlerNumber(data[i]);
b.setScore1(Integer.parseInt(data[i+1]));
b.setScore2(Integer.parseInt(data[i+2]));
b.setScore3(Integer.parseInt(data[i+3]));
i=i+4;
scores.add(b);

//If Key repeats adding to list
if(map.containsKey(b.getBowlerNumber().charAt(0))){
ArrayList<Bowler> sl = map.get(b.getBowlerNumber().charAt(0));
sl.add(b);
map.put(b.getBowlerNumber().charAt(0), sl);
}

else
{
//Otherwise add to List normally
map.put(b.getBowlerNumber().charAt(0),scores);
}
}
BowlerReports bReport = new BowlerReports();

int j = 0;
while(j == 0) {
   System.out.println("Select the option you want:");
System.out.println("1.Generate the report");
System.out.println("2.Get the report of First team");
System.out.println("3.Get the report of Second team");
System.out.println("4.To Exit");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
   switch(choice) {
   case 1:   bReport.generateReport(map);
              break;
   case 2:   bReport.generateReport('A');
              break;
   case 3:   bReport.generateReport('B');
                   break;
   case 4: j = 1;
              break;
   default: System.out.println("Enter a right option");
   }   
}
}
catch(Exception e)
{
e.printStackTrace();
}
  
}

private void generateReport(Character teamName) {
   ArrayList<Bowler> als = map.get(teamName);
Iterator<Bowler> bow = als.iterator();
int sum1,sum2,sum3;
sum1=sum2=sum3=0;
int gt=0;
while(bow.hasNext()){
Bowler bb = bow.next();
  
  
//Each Bowler Individual Score
//Below Line is for Output-1
System.out.println(bb.getBowlerNumber()+" "+bb.getScore1()+" "+bb.getScore2()+" "+bb.getScore3());
float avg = (bb.getScore1()+bb.getScore2()+bb.getScore3())/3;
//Average Score of Each Bowler

//Below Line is for Output-2
System.out.println("Average Score of bowler"+bb.getBowlerNumber()+" is: "+avg);
sum1 = sum1 + bb.getScore1();
sum2 = sum2 + bb.getScore2();
sum3 = sum3 + bb.getScore3();
gt = gt + bb.getScore1()+bb.getScore2()+bb.getScore3();
  
}
//Team Each Game Score

//Below Line is for Output-3
System.out.println("Team-"+teamName+" games score are: "+sum1+" "+sum2+" "+sum3);
// Grand Total of Each Teams

//Below Line is for Output-4
System.out.println("Grand total for Team-"+teamName+" is: "+gt);
}

public void generateReport(HashMap<Character, ArrayList<Bowler>> map) {
   // Getting Key and Value from HashMap using entrySet
//Iterating each Bowler using Entry in Map
Set<Entry<Character, ArrayList<Bowler>>> set = map.entrySet();
Iterator<Entry<Character, ArrayList<Bowler>>> itr = set.iterator();
while(itr.hasNext()){
Entry<Character, ArrayList<Bowler>> entry = itr.next();
this.generateReport(entry.getKey());
}
}
}

Scores.txt

A 10 20 30
B 20 10 30
C 10 20 30
B 10 20 30

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote