CaptainTeancum
CaptainTeancum

Reputation: 47

Trouble with the split method

I'm trying to use the split method to separate the students from the scores and then if the score is good or bad plus your minus 100 than output a message. The split method is having trouble turning the char into a string

class Program
    {   //Here we declare some Const variables 
        const int MAX = 1;
        const int ZERO = 0;
        const int ONE = 1;

        static void Main(string[] args)
        {   //here we declare the variables and the 2 arrays for the main method

            int perfecto = 100;
            string input;
            string[] student = new string[MAX];
            int[] score = new int[MAX];            


            //this will be the introduction for the program nice and friendly.
            Console.WriteLine("Welcome to the Test score calculator!!");



                    Console.Write("\nPlease Input your name and your score, seperated by a space: ");
                    input = Console.ReadLine();



                    Console.WriteLine("Welcome to the Test score calculator!!");
                    Console.Write("\nPlease Input your name and your score, seperated by a space and Press Enter: ");
                    input = Console.ReadLine();




                    //SPLIT METHOD ACTIVATED.. here we call the split method 
                    SplitMethod(input, ref student, ref score);




            //Here we call the output           
            Output(student, score, perfecto);

            Console.ReadLine();
        }
        //Here is the split method. this will take the two kinds of data and split them into 2 arrays
        //the string and the int seperate so that its easyer to make calculations. 
        //also we referenced these arrays 
        static void SplitMethod(string input, ref string[] student, ref int[] score)
        {
            char rules = { ' ', '\r' };
            string splitArray = input.Split();

            //here is the actual split
            student = splitArray[ZERO];
            score = int.Parse(splitArray[]);
            return;
        }     

        static void Output(string[] student, int[] score, int perfecto)
        {         


                //here is the added if statement for the perfect score scenario 
                if (score[i] > perfecto)
                {
                    //here is the output incase someone scores a perfect game
                    Console.WriteLine("\n{0}'s score was {1}*...Congrats {2} you qualify for the TEAM!!!", student[], score[], student[]);
                }
                else
                {
                    //and if they dont it displayes here.
                    Console.WriteLine("\nSorry {0}, {1} is not good enough. \nIm afraid you dont qualify, but keep it up!", student[], score[]);
                }
            }

        }
    }

Upvotes: 1

Views: 196

Answers (3)

Nikola Davidovic
Nikola Davidovic

Reputation: 8656

Your code has many flaws, I corrected them. The first and the most obvious is that you should not be using arrays at all. Even if you want to repeat this name and score entering, you don't need arrays because the user is entering one name and score at a time. This is your code changed to work:

class Program
    {   

        static void Main(string[] args)
        {   //here we declare the variables and the 2 arrays for the main method

            int perfecto = 100;
            string input;
            string student = string.Empty;
            int score = 0;            


            //this will be the introduction for the program nice and friendly.
            Console.WriteLine("Welcome to the Test score calculator!!");
            Console.Write("\nPlease Input your name and your score, seperated by a space and Press Enter: ");
            input = Console.ReadLine();

            //SPLIT METHOD ACTIVATED.. here we call the split method 
            SplitMethod(input, ref student, ref score);

            //Here we call the output           
            Output(student, score, perfecto);

            Console.ReadLine();
        }
        //Here is the split method. this will take the two kinds of data and split them into 2 arrays
        //the string and the int seperate so that its easyer to make calculations. 
        //also we referenced these arrays 
        static void SplitMethod(string input, ref string student, ref int score)
        {
            char [] rules = { ' ', '\r' };
            string [] splitArray = input.Split(rules);

            //here is the actual split
            if(splitArray.Length>1)
            {
                student = splitArray[0];
                int.TryParse(splitArray[1], out score);
            }
        }     

        static void Output(string student, int score, int perfecto)
        {         
            //here is the added if statement for the perfect score scenario 
            if (score > perfecto)
            {
                //here is the output incase someone scores a perfect game
                Console.WriteLine("\n{0}'s score was {1}*...Congrats {2} you qualify for the TEAM!!!", student, score, student);
            }
            else
            {
                //and if they dont it displayes here.
                Console.WriteLine("\nSorry {0}, {1} is not good enough. \nIm afraid you dont qualify, but keep it up!", student, score);
            }
        }
    }

Now I suppose that you want to do this for many inputs until user enters end, you should then change your Main method like this:

static void Main(string[] args)
        {   //here we declare the variables and the 2 arrays for the main method

            int perfecto = 100;
            string input;
            string student = string.Empty;
            int score = 0;            


            //this will be the introduction for the program nice and friendly.
            Console.WriteLine("Welcome to the Test score calculator!!");
            Console.Write("\nPlease Input your name and your score, seperated by a space and Press Enter, insert END if you want to finish: ");

            while (!(input = Console.ReadLine()).Equals("end", StringComparison.CurrentCultureIgnoreCase))
            {
                SplitMethod(input, ref student, ref score);
                Output(student, score, perfecto);
                Console.Write("\nPlease Input your name and your score, seperated by a space and Press Enter, insert END if you want to finish: ");
            }
            Console.ReadLine();
        }

Upvotes: 0

first off, you should make your character array declared as an array

        char[] rules = { ' ', '\r' };

secondly you should pass rules into your split

        string[] splitArray = input.Split(rules);

I believe you have other compiler errors as well. you should try doing some on your own

you should also tell us what your compilation errors are when you ask a question.

Upvotes: 1

Matt Burland
Matt Burland

Reputation: 45135

Read the docs on String.Split

string[] splitArray = input.Split(rules);

Also you need to fix you definition of rules so that it's a char array. Again, look at the docs

Upvotes: 1

Related Questions