Arrays and Text Files

In a Golf Score Program

Page 1 of 1

7 Replies - 557 Views - Last Post: 15 June 2009 - 06:04 PM Rate Topic: -----

#1 jimsjam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-June 09

Arrays and Text Files

Posted 14 June 2009 - 10:24 PM

I'm relatively new to the world of programming and would really benefit from some help.
I'm trying to make windows form that can read in golf scores from a text file. The first line in the file indicates the number of holes the player has completed. This is known as 'int holes' in my code. There are 'holes' numbers after the first line about the par of each hole, and 'holes' numbers after the par about what the player scored.

i.e. text file
3 [completed holes] (if this number were 4, then there would be 4 par values and 4 score values)
2 [par]
3 [par]
2 [par]
3 [score]
2 [score]
3 [score]


The problem is I don't know how to make two arrays with 'holes' sizes with 'par' and 'score' values and display them to the user. All I've managed to do so far is gotten the program to read in the first line of the text file and declare the variable for holes as an int.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace GolfScore
{
	public partial class frmMain : Form
	{
		public frmMain()
		{
			InitializeComponent();
		}

		private void loadToolStripMenuItem_Click(object sender, EventArgs e)
		{
			OpenFileDialog fileDialog = new OpenFileDialog();
			
			if (fileDialog.ShowDialog() == DialogResult.OK)
			{
				StreamReader sr = new StreamReader(fileDialog.OpenFile());
				string line = sr.ReadLine();
				int holes = int.Parse(line);
				
			}

		}
	}
}




I've tried to be as descriptive as I can be, so please, help me. :^:

Is This A Good Question/Topic? 0
  • +

Replies To: Arrays and Text Files

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 994
  • View blog
  • Posts: 3,547
  • Joined: 05-June 09

Re: Arrays and Text Files

Posted 15 June 2009 - 02:00 AM

What I'd do would be to create a GolfHole class which stores ID (asin, hole number), par, and score.

Then you can have an array of GolfHoles and edit accordingly.
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1511
  • View blog
  • Posts: 5,916
  • Joined: 21-March 08

Re: Arrays and Text Files

Posted 15 June 2009 - 06:15 AM

try this...

static void Main(string[] args)
{
    int lineCounter = 1;
    int numberOfHoles = 0;
    List<GolfHole> golfHoleList = new List<GolfHole>();

    using (TextReader tr = new StreamReader(@"C:\test\test.txt"))
    {
        while (tr.Peek() >= 0)
        {
            if (lineCounter == 1)
            {
                // Gets the number of holes from first line
                numberOfHoles = int.Parse(tr.ReadLine());
            }
            else
            {
                if (lineCounter - 1 <= numberOfHoles)
                {
                    // getting par score
                    GolfHole gh = new GolfHole();
                    gh.HoleNumber = lineCounter - 1;
                    gh.ParScore = int.Parse(tr.ReadLine());

                    golfHoleList.Add(gh);
                }
                else
                {
                    golfHoleList[lineCounter - numberOfHoles - 2].Score = int.Parse(tr.ReadLine());
                }
            }

            lineCounter++;
        }
    }

    foreach (GolfHole gh in golfHoleList)
    {
        Console.WriteLine("Hole #{0} - Par {1} - Score {2}", gh.HoleNumber, gh.ParScore, gh.Score);
    }

    Console.Read();
}



and the GolfHole class like MageUK posted about...

public class GolfHole
{
    private int m_holeNumber = 0;
    private int m_parScore = 0;
    private int m_score = 0;

    public int ParScore
    {
        get { return m_parScore; }
        set { m_parScore = value; }
    }

    public int HoleNumber
    {
        get { return m_holeNumber; }
        set { m_holeNumber = value; }
    }

    public int Score
    {
        get { return m_score; }
        set { m_score = value; }
    }
}


This post has been edited by eclipsed4utoo: 15 June 2009 - 06:16 AM

Was This Post Helpful? 1
  • +
  • -

#4 jimsjam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-June 09

Re: Arrays and Text Files

Posted 15 June 2009 - 11:49 AM

Ah, thank you, that works out fantastically!
I added that into my code, so it looks like this now:
namespace GolfScore
{
	public partial class frmMain : Form
	{
		public frmMain()
		{
			InitializeComponent();
		}

		private void loadToolStripMenuItem_Click(object sender, EventArgs e)
		{
			OpenFileDialog fileDialog = new OpenFileDialog();
			
			if (fileDialog.ShowDialog() == DialogResult.OK)
			{
				int lineCounter = 1;
				int numberOfHoles = 0;
				List<GolfHole> golfHoleList = new List<GolfHole>();

				using (TextReader tr = new StreamReader(fileDialog.OpenFile()))
				{
					while (tr.Peek() >= 0)
					{
						if (lineCounter == 1)

						{
							// Gets the number of holes from first line
							numberOfHoles = int.Parse(tr.ReadLine());
						}
						else
						{
							if (lineCounter - 1 <= numberOfHoles)
							{
								// getting par score
								GolfHole gh = new GolfHole();
								gh.HoleNumber = lineCounter - 1;
								gh.ParScore = int.Parse(tr.ReadLine());

								golfHoleList.Add(gh);
							}
							else
							{
								golfHoleList[lineCounter - numberOfHoles - 2].Score = int.Parse(tr.ReadLine());
							}
						}

						lineCounter++;
					}
				}

				foreach (GolfHole gh in golfHoleList)
				{
					MessageBox.Show(" | Hole: " + gh.HoleNumber + " | Par: " + gh.ParScore + " | Score: " + gh.Score + " | ");
				}
			}

		}
	}
	
	public class GolfHole
	{
		private int m_holeNumber = 0;
		private int m_parScore = 0;
		private int m_score = 0;

		public int ParScore
		{
			get { return m_parScore; }
			set { m_parScore = value; }
		}

		public int HoleNumber
		{
			get { return m_holeNumber; }
			set { m_holeNumber = value; }
		}

		public int Score
		{
			get { return m_score; }
			set { m_score = value; }
		}
	}



But I was wondering if there would be any way to output the number of holes, the par score, and the player score directly on the windows form, instead of displaying it in a message box. Once you get to 18 holes, it takes a while to view them all that way.

I've tried to write a string directly onto the form using the gh.HoleNumber, gh.ParScore and gh.Score, but the numbers of each hole/par/score overlap since it's in a list. I've also tried to output the text in a label, but it only outputs the last hole, par and score, and doesn't output the first ones. Any additional help would be greatly appreciated!
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1511
  • View blog
  • Posts: 5,916
  • Joined: 21-March 08

Re: Arrays and Text Files

Posted 15 June 2009 - 12:27 PM

assuming you have a label on your form called "lblHolesPlayed", comment out the foreach statement at the end, and put this above it.

lblHolesPlayed.Text = numberOfHoles.ToString();



if you want to display a Total ParScore, Total PlayerScore, and TotalScore, then you can do this in the foreach loop.

int totalParScore = 0;
int totalPlayerScore = 0;
int totalScore = 0;

foreach (GolfHole gh in golfHoleList)
{
     totalParScore += gh.ParScore;
     totalPlayerScore += gh.Score;
}

totalScore = totalPlayerScore - totalParScore;

lblTotalParScore.Text = totalParScore.ToString();
lblTotalPlayerScore.Text = totalPlayerScore.ToString();
lblTotalScore.Text = totalScore.ToString();


Was This Post Helpful? 1
  • +
  • -

#6 jimsjam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-June 09

Re: Arrays and Text Files

Posted 15 June 2009 - 02:18 PM

Thank you for your help, but I believe I should have elaborated a bit more. What I was trying to do was to display the score in a chart form to the user.
i.e. Text file
3 [holes]
4 [par]
4 [par]
3 [par]
4 [score]
5 [score]
2 [score]

i.e. displayed to user
Hole | 1| 2 | 3
Par | 4 | 4 | 3
Score | 4| 5 | 2


To do this, I think I need to read in the text file and make two arrays, one for par and one for score, and then display them in relation to the hole number, but I've not the slightest idea of how to do that! If you could help me with this problem, you're officially my programming idol, haha. :^:
Was This Post Helpful? 0
  • +
  • -

#7 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 61
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Arrays and Text Files

Posted 15 June 2009 - 02:36 PM

Well, from the get go you're setting yourself up for world of hurt. That text file is not formatted to help you out as a programmer.

Change your .txt file to this format:

Text File:
Hole 1|4|4
Hole 2|4|5
Hole 3|3|2


Each line in your text file would be in the format: Hole, Par, Score, with the "|" character acting as the separator.

With that out of the way it would just be a matter of using a simple .Split() method to get what you want from each line.

I know this doesn't really answer your question but I think it will ultimately make your job MUCH easier. :pirate:

This post has been edited by papuccino1: 15 June 2009 - 02:42 PM

Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1511
  • View blog
  • Posts: 5,916
  • Joined: 21-March 08

Re: Arrays and Text Files

Posted 15 June 2009 - 06:04 PM

View Postjimsjam, on 15 Jun, 2009 - 04:18 PM, said:

Thank you for your help, but I believe I should have elaborated a bit more. What I was trying to do was to display the score in a chart form to the user.
i.e. Text file
3 [holes]
4 [par]
4 [par]
3 [par]
4 [score]
5 [score]
2 [score]

i.e. displayed to user
Hole | 1| 2 | 3
Par | 4 | 4 | 3
Score | 4| 5 | 2


To do this, I think I need to read in the text file and make two arrays, one for par and one for score, and then display them in relation to the hole number, but I've not the slightest idea of how to do that! If you could help me with this problem, you're officially my programming idol, haha. :^:


this should work.

StringBuilder sbHoles = new StringBuilder();
StringBuilder sbParScores = new StringBuilder();
StringBuilder sbPlayerScores = new StringBuilder();

sbHoles.Append("Holes	 |");
sbParScores.Append("Par	   |");
sbPlayerScores.Append("Scores	|");

foreach (GolfHole gh in golfHoleList)
{
	sbHoles.Append(" " + gh.HoleNumber + " |");
	sbParScores.Append(" " + gh.ParScore + " |");
	sbPlayerScores.Append(" " + gh.Score + " |");
}

sbPlayerScores.Remove(sbPlayerScores.Length - 2, 2);
sbHoles.Remove(sbHoles.Length - 2, 2);
sbParScores.Remove(sbParScores.Length - 2, 2);

Console.WriteLine(sbHoles.ToString());
Console.WriteLine(sbParScores.ToString());
Console.WriteLine(sbPlayerScores.ToString());


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1