Question

I have created a c# program that takes a scan input, and then takes the information from the input. What I have been noticing is that for larger strings, my program for some reason splits the string into two parts (not in half), which screws up the way I get my information. My string has hexadecimal values in it as well.

For example, when I scan a code into my console, it reads the string

[)>065JUN1234567892300167Q205GT21L123 ABC06P123456787Q100PL7Q10PKQ1006P98356877Q100PL7Q5PKQ2006P235265437Q200PL7Q40PKQ5

but it splits that string into:

[)>065JUN1234567892300167Q205GT21L123 ABC06P123456787Q100PL7Q10PKQ1006P98356877Q100"

and

PL7Q5PKQ2006P235265437Q200PL7Q40PKQ5

Any idea how to fix this, or allocate more memory to my variable which reads the console for the input scan?

Here is my code, its kind of long.

static void Main(string[] args)
    {
        Console.WriteLine("Please enter the date and lane number");
        Console.WriteLine("like so: ddmmyylanenumber.");
        string lanenum = Console.ReadLine();
        Console.WriteLine("When done scanning, please type");
        Console.WriteLine("\"finish\" into the console.");
        string scanInput;

        do
        {
            Console.Write("Scan now:");
            scanInput = Console.ReadLine();

                //The number before "JUN" identifies the type of label it is.
                int posOfJUN = scanInput.IndexOf("JUN");
                //Finding the type of label
                string typeOfLabel = scanInput.Substring(posOfJUN - 1, 1);
                string label;
                if (typeOfLabel == "5")
                {
                    label = "mixed";
                }
                else if (typeOfLabel == "1")
                {
                    label = "individual";
                }
                else
                {
                    label = null;
                }

                switch (label)
                {
                    case "individual":
                        partNumber = scanInput.Substring(8, 8);

                        int posOfQ1 = scanInput.IndexOf("Q");
                        int posOf1JUN = scanInput.IndexOf("1JUN");
                        //Quantity of the pack
                        total = scanInput.Substring(posOfQ1 + 1, posOf1JUN - posOfQ1 - 1 - 1);
                        //number of packs is 1 because individual
                        numOfPacks = "1";
                        dunsNumber = scanInput.Substring(posOf1JUN + 4, 9);
                        //used to find the duns and serial number
                        posOf20L = scanInput.IndexOf("20L");
                        posOf21L = scanInput.IndexOf("21L");

                        //Setting the serial number
                        if (posOf21L == -1 || posOf20L < posOf21L)
                        {
                            serialNumber = scanInput.Substring(posOf1JUN + 4, posOf20L - posOf1JUN - 4 - 1);
                        }
                        else if (posOf20L == -1 || posOf21L < posOf20L)
                        {
                            serialNumber = scanInput.Substring(posOf1JUN + 4, posOf21L - posOf1JUN - 4 - 2);
                        }
                        else
                        {
                            serialNumber = null; //else clause if serial number can't be created
                            Console.WriteLine(new ArgumentException("Error obtaining Serial Number"));
                        }

                        partObject part2 = new partObject(partNumber, total, numOfPacks);

                        newPacks = int.Parse(numOfPacks);
                        total = total.ToString();
                        newtotal = int.Parse(total);
                        part2.callSQL(partNumber, newtotal, newPacks, dunsNumber, serialNumber, lanenum);
                        break;

                    case "mixed":
                        posOfJUN = scanInput.IndexOf("JUN");
                        dunsNumber = scanInput.Substring(posOfJUN + 3, 9);
                        int posOf7Q = scanInput.IndexOf("7Q");
                        posOf20L = scanInput.IndexOf("20L");
                        posOf21L = scanInput.IndexOf("21L");

                        //Finding serial number
                        serialNumber = scanInput.Substring(posOfJUN + 3, posOf7Q - posOfJUN - 3);

                        //The following lines are to find how many different parts are in the mixed load. 
                        posOfPK = scanInput.IndexOf("PK");
                        int PKTemp;
                        int parts = 1;
                        //Each time a "PK" is seen, it means there is another part so the count increments.
                        while (scanInput.IndexOf("PK", posOfPK + 1) != -1)
                        {
                            PKTemp = scanInput.IndexOf("PK", posOfPK + 1);
                            posOfPK = PKTemp;
                            parts++;
                        }
                        //Creating an array of size "parts"
                        int posOf06 = scanInput.IndexOf("06");
                        int temp06 = scanInput.IndexOf("06", posOf06 + 2);
                        posOf06 = temp06;
                        int indexOfP = scanInput.IndexOf("P", posOf06 + 1);
                        partNumber = scanInput.Substring(indexOfP + 1, 8);

                        posOfPK = scanInput.IndexOf("PK", indexOfP);
                        posOfPL = scanInput.IndexOf("PL", indexOfP);
                        posOf7Q1 = scanInput.IndexOf("7Q", indexOfP);
                        partObject[] arrayOfParts = new partObject[parts];

                        for (int i = 0; i < parts; i++)
                        {

                            //Finds the different values, creates an object and puts it into the array of parts
                            partNumber = scanInput.Substring(indexOfP + 1, 8);
                            total = scanInput.Substring(posOf7Q1 + 2, posOfPL - posOf7Q1 - 2);
                            numOfPacks = scanInput.Substring(posOfPL + 5, posOfPK - posOfPL - 5);
                            arrayOfParts[i] = new partObject(partNumber, total, numOfPacks);

                            //resetting the variables for the next iteration, so a new object can be created with the next part
                            posOf06 = scanInput.IndexOf("06", temp06 + 1);
                            indexOfP = scanInput.IndexOf("P", posOf06 + 1);
                            temp06 = posOf06;
                            posOfPK = scanInput.IndexOf("PK", indexOfP);
                            posOfPL = scanInput.IndexOf("PL", indexOfP);
                            posOf7Q1 = scanInput.IndexOf("7Q", indexOfP);

                            //putting each object into SQL
                            newtotal = int.Parse(total);
                            newPacks = int.Parse(numOfPacks);
                            serialNumber = serialNumber + "P" + (i + 1);
                            arrayOfParts[i].callSQL(partNumber, newtotal, newPacks, dunsNumber, serialNumber, lanenum);

                        }


                        break;
                    default:
                        break;
                }

            }
        } while (scanInput != "finish");
    }

The full string is never there right from the start of the code.

No correct solution

OTHER TIPS

If you debug your program I think you will find that the string is all there. I'm not sure why you think the string is split... a System.String can only hold one string. At some point are you getting a string[] ?

Place a breakpoint after this line scanInput = Console.ReadLine(); and in Visual Studio hover over scanInput, and it will show you the whole string.... or try just printing out the string to show that it is all there.

EDIT: If you're using hexadecimal, try looking up the hex values which cause the anomaly on the ascii table. Perhaps the hex value is resulting in a carriage return or newline.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top