Working on a project that is basically a simple bowling program that uses a gui to read in from a given text file a pre-determined amount of names and scores...runs through the standard 10 frame scoring system, prints out the score, and who won.

Now I can't seem to get it to print that out into the defined gui text field I have got. Any ideas? Here is what I have:

// Homework #3 Bowling Program Template
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JFrame;
import java.util.Scanner;
import java.io.*;
class BFrame{
    // represents the number of pins knocked down for ball 1
    private int ball1;

    // represents the number of pins knocked down for ball 1
    private int ball2;

    // represents the score up to and including the frame score
    private int fscore;

    // constructor for a BFrame object
    public BFrame(){
        ball1 = -1;
        ball2 = -1;
        fscore = -1;
    }

    // mutator methods
    public void setBall1(int x){
        ball1 = x;
    }
    public void setBall2(int x){
        ball2 = x;
    }
    public void setFscore(int x){
        fscore = x;
    }

    // accessor methods
    public int getBall1(){
        return ball1;
    }
    public int getBall2(){
        return ball2;
    }
    public int getFscore(){
        return fscore;
    }
}
class Bowler{
    // represents the name of the bowler
    private String name;

    // represents the 10 frames for the bowler
    private BFrame[] frames = new BFrame[10];

    // represents the extra ball for the 10th frame
    private int extraball;

    // represents the current frame the bowler is on
    // acts as a "slot" value for the frames array
    private int currentframe;

    // constructor for a Bowler object
    public Bowler(String n){
        name = n;
        extraball = -1;
        currentframe = 0;
        for (int i=0; i<10; i++){
            frames[i] = new BFrame();
        }
    }

    // accessor for bowler name
    public String getName(){
        return name;
    }

    // mutator to add the ball to the appropriate location
    // part of this method is written
    //
    public void addBall(int pts){
        if (frames[currentframe].getBall1() == -1){
            frames[currentframe].setBall1(pts);
            if ((pts == 10) && (currentframe != 9)){ // strike
                currentframe++;
            }
        }
        else { // need to handle additional placement conditions
               // be careful with the 10th frame


        }
    }

    // This method should populate each frame's framescore
    // according to standard 10-pin scoring rules.  It is
    // assumed that this method will only be called AFTER all
    // the balls have been entered.
    public void calcScore(){
       //


    }
    // method to print the bowler's sheet to the console
    // this method is intended for debugging purposes only
    public void printSheet(){
        System.out.println("\nBowling Sheet for " + name);
        for (int i=0; i<=currentframe; i++){
            System.out.print("Frame " + (i+1));
            System.out.print("  Ball 1: " + frames[i].getBall1());
            System.out.print("  Ball 2: " + frames[i].getBall2());
            System.out.println("  Frame Score: " + frames[i].getFscore());
        }
        System.out.println("Extra Ball: " + extraball);
    }
    
}
class Bowling extends JPanel{
    // create GUI objects
    private JLabel fileLabel;
    private JButton loadButton;
    private JTextField infileText;
    private JButton clearButton;
    private JTextArea displayText;
    private Bowler[] bowlers;

    // Constructor to Initialize grade
    public Bowling(){
        // initialize objects
        bowlers = new Bowler[2];
        fileLabel = new JLabel("Enter name of data file and press Load Scores: ");
        loadButton = new JButton("Load Scores");
        clearButton = new JButton("Clear All");
        infileText = new JTextField(15);
        displayText = new JTextArea(15, 30);
        displayText.setEditable(false);
        displayText.setFont(new Font("Courier", Font.PLAIN, 12));

        // add action listeners for objects
        loadButton.addActionListener(new loadButtonListener());

        // add objects to panel
        add(fileLabel);
        add(infileText);
        add(loadButton);
        add(displayText);
        add(clearButton);

        // size the panel
        setPreferredSize(new Dimension(800, 300));
        setBackground(Color.gray);

    }

    // create action listener class and actionPerformed method
    private class loadButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent event){
            String file;
            file = infileText.getText();
            try{
                loadData(file);
            }
            catch(IOException e){
                System.out.println("Unable to load data file");
            }
        }
    }

    private void loadData(String s) throws IOException {
        Scanner infile = new Scanner(new File(s));
        String n;
        int p;
        bowlers[0] = new Bowler(infile.next());
        bowlers[1] = new Bowler(infile.next());
        while (infile.hasNext()){
            n = infile.next();
            p = infile.nextInt();
            // Add the code to do the following:
            // Based on the name read in add the ball score
            // to the appropriate bowlers sheet


        }
        // calculate the scores for each bowler and print the scores to the
        // GUI text area



        // prints the sheet to the console for debugging purposes only
        bowlers[0].printSheet();
        bowlers[1].printSheet();
    }
}
public class hw3bowling{
    public static void main(String [] args){
        JFrame frame = new JFrame("Bowling");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Bowling b = new Bowling();
        frame.getContentPane().add(b);
        frame.pack();
        frame.setVisible(true);
    }
}

I do have a couple of sections unfinished as I am still working on those also. Where I am stumped at is the public void loadData area. If I can get the program to at least load the info into the gui text box I can finish everything else up.

Any help would be great.

Recommended Answers

All 9 Replies

Would you mind provide sample file with names and scores for easier debugging?

Sure can:

. If I can get the program to at least load the info into the gui text box I can finish everything else up.

Any help would be great.

Howdy, without delving too deep into your code, but looking at the question you've submitted, your output is resolved in the printSheet Method right? So you've set your output to stdOut using the system.out.Println methods. To display that text in a text field is pretty easy. Replace your println methods with a formatted string. Use concat and other formatting methods to create newlines etc.
You have created a textbox called displayText which I am assuming is where you want to display your data. So, set your formatted string as a private data member of your Bowler class, create an accessor method called getScores or something like that. Have it return a String object and simpy return your formatted string. Use the setText method for your displayText object, passing it the string. So you will want to do something like:

//in Bowler:
private String scoreText; 
. . .
//in Bowler.printSheet:
printSheet( . . .) {
//format your output string (scoreText)
}
public String getScores()
{
return scoreText;
}
//in Main:
displayText.setText(b.getScores());

Provided that your class algorithm is correct, it should work. I suggest stubbing out printSheet and just set scoreText to a constant string like: "Testing the text field here" and making sure that it will display, then start working on formatting your string. Godd luck!

Howdy, without delving too deep into your code, but looking at the question you've submitted, your output is resolved in the printSheet Method right? So you've set your output to stdOut using the system.out.Println methods. To display that text in a text field is pretty easy. Replace your println methods with a formatted string. Use concat and other formatting methods to create newlines etc.
You have created a textbox called displayText which I am assuming is where you want to display your data. So, set your formatted string as a private data member of your Bowler class, create an accessor method called getScores or something like that. Have it return a String object and simpy return your formatted string. Use the setText method for your displayText object, passing it the string. So you will want to do something like:

//in Bowler:
private String scoreText; 
. . .
//in Bowler.printSheet:
printSheet( . . .) {
//format your output string (scoreText)
}
public String getScores()
{
return scoreText;
}
//in Main:
displayText.setText(b.getScores());

Provided that your class algorithm is correct, it should work. I suggest stubbing out printSheet and just set scoreText to a constant string like: "Testing the text field here" and making sure that it will display, then start working on formatting your string. Godd luck!

I like this but technically it's supposed to happen within the public void loadData area.

I like this but technically it's supposed to happen within the public void loadData area.

Right, that's where you've defined the displayText pane. Then implement it there instead of main.

Right, that's where you've defined the displayText pane. Then implement it there instead of main.

Ok...now you have lost me. I thought it is defined and implemented within loadData.

Ok...now you have lost me. I thought it is defined and implemented within loadData.

displayText is a private data member of your Bowling class:

#
class Bowling extends JPanel{
#
// create GUI objects
. . .
    private JTextArea displayText;

That is where it is defined.
As far as I can tell, displayText isn't implemented anywhere yet. Your loadData method calls printSheet, which by the way your code is desinged, seems like the ideal place to actually display your text. So, all you need to do is format a string to display data within print sheet (Instead of your println calls) and then just call displayText.setText(your_formatted_string).
Test it to make sure it works by stubbing out your output lines in printSheet and just call displayText.setText("Making sure it works") so that you know you're on the right track.
You're code looks good, and well formed. You should be able to tackle this with ease. Good luck!

displayText is a private data member of your Bowling class:

#
class Bowling extends JPanel{
#
// create GUI objects
. . .
    private JTextArea displayText;

That is where it is defined.
As far as I can tell, displayText isn't implemented anywhere yet. Your loadData method calls printSheet, which by the way your code is desinged, seems like the ideal place to actually display your text. So, all you need to do is format a string to display data within print sheet (Instead of your println calls) and then just call displayText.setText(your_formatted_string).
Test it to make sure it works by stubbing out your output lines in printSheet and just call displayText.setText("Making sure it works") so that you know you're on the right track.
You're code looks good, and well formed. You should be able to tackle this with ease. Good luck!

That works great...thanks. One last question that has me completely stumped.....what call do I need with the "()" to have grab the correct data?

Seriously you's think this would be the easy part of my project....but it has turned out to be one of the hardest parts.

I almost have the 2 other sections complete.

One last question that has me completely stumped.....what call do I need with the "()" to have grab the correct data?

Are you asking about the argument for displayText.setText? I would suggest formatting a string, As of now, You're printing to stdout with:

System.out.println("\nBowling Sheet for " + name);
for (int i=0; i<=currentframe; i++){
System.out.print("Frame " + (i+1));
System.out.print(" Ball 1: " + frames[i].getBall1());
System.out.print(" Ball 2: " + frames[i].getBall2());
System.out.println(" Frame Score: " + frames[i].getFscore());
}
System.out.println("Extra Ball: " + extraball);

So instead, create a string called output string and append your display output to it.
Something like:

String outputScores = new String();
outputScores =  "\nBowling Sheet for " + name;
for (int i=0; i<=currentframe; i++){

   outputScores.concat("Frame " + (i+1) + "\n") // You will need to convert your int i to a string though. 
   outputScores.concat(" Ball 1: " + frames[i].getBall1() + "\n")
// and so on, 
}

then after the extra ball statement, just call displayText.setText(outputScores) . That should work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.