Reading text file until a space and storing several values

Viewed 3710

I am creating an application to read and display Pokemon stats. Currently, I have 6 txt files, one for each stat. I have 6 arrays reading each txt file and displaying each stat in a label.

I want to condense this information into a single text file, splitting each stat by a " " to keep each Pokemon's data on a single line.

Current code if it helps explain the idea better:

using System;
using System.Windows.Forms;

using System.IO;

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

        //Read stat data from text files into string arrays
        string[] HP = File.ReadAllLines("HP.txt");
        string[] Atk = File.ReadAllLines("Atk.txt");
        string[] Def = File.ReadAllLines("Def.txt");
        string[] SpAtk = File.ReadAllLines("SpAtk.txt");
        string[] SpDef = File.ReadAllLines("SpDef.txt");
        string[] Spe = File.ReadAllLines("Spe.txt");

        private void cbxPokemon_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Get array index of currently selected Pokemon
            int index = cbxPokemon.SelectedIndex;

            //Get integer values out of the string arrays for appropriate use
            int intHP = int.Parse(HP[index]);
            int intAtk = int.Parse(Atk[index]);
            int intDef = int.Parse(Def[index]);
            int intSpAtk = int.Parse(SpAtk[index]);
            int intSpDef = int.Parse(SpDef[index]);
            int intSpe = int.Parse(Spe[index]);

            //Update labels with stat values in string forme, could also assign intStat.ToString()
            lblDexNum.Text = (index + 1).ToString("d3");

            lblHP.Text = HP[index];
            lblAtk.Text = Atk[index];
            lblDef.Text = Def[index];
            lblSpAtk.Text = SpAtk[index];
            lblSpDef.Text = SpDef[index];
            lblSpe.Text = Spe[index];

            lblBST.Text = (intHP + intAtk + intDef + intSpAtk + intSpDef + intSpe).ToString();

            //Update bar width based on stat value
            barHP.Width = intHP;
            barAtk.Width = intAtk;
            barDef.Width = intDef;
            barSpAtk.Width = intSpAtk;
            barSpDef.Width = intSpDef;
            barSpe.Width = intSpe;

            //Disable Previous and Next buttons when they cannot be used
            if (index == 0) { btnPrev.Enabled = false; }
            else { btnPrev.Enabled = true; }

            if (index == cbxPokemon.Items.Count - 1) { btnNext.Enabled = false; }
            else { btnNext.Enabled = true; }
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            cbxPokemon.SelectedIndex -= 1;
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            cbxPokemon.SelectedIndex += 1;
        }
    }
}

Ideally, the file would contain the 6 stats, for example "100 90 80 70 60 50" on each line, with each of those valued being stored.

Is there a simple way to read until a space, store that value, and keep doing that until the end of the line?

4 Answers
Related