2014-03-03 22 views
0

我已經創建了一個c#程序,它接受一個掃描輸入,然後從輸入中獲取信息。我一直注意到的是,對於較大的字符串,由於某種原因,我的程序將字符串分成兩部分(不是一半),這就讓我得到信息的方式變得麻煩。我的字符串也有十六進制值。如何爲變量分配更多內存

例如,當我掃描代碼到我的控制檯,它讀取字符串

[)>065JUN1234567892300167Q205GT21L123 ABC06P123456787Q100PL7Q10PKQ1006P98356877Q100PL7Q5PKQ2006P235265437Q200PL7Q40PKQ5 

但它拆分該字符串爲:

[)>065JUN1234567892300167Q205GT21L123 ABC06P123456787Q100PL7Q10PKQ1006P98356877Q100" 

PL7Q5PKQ2006P235265437Q200PL7Q40PKQ5 

任何想法如何解決這個問題,或分配更多的內存到我的變量,它讀取控制檯的輸入掃描?

這是我的代碼,其種類很長。

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"); 
    } 

完整的字符串永遠不會從代碼的開頭出現。

+2

這不是內存問題,字符串很小。問題在於你如何閱讀它。發佈你的代碼。 –

+2

分裂如何?當你只要求一個時,c#不會給你兩個字符串。 –

+0

@Mark嘿,我已經添加了我的代碼,認爲這是一個內存問題,這就是爲什麼我沒有包括它,我的壞。 – AlvinJ

回答

0

如果你調試你的程序,我想你會發現string都在那裏。 我不知道你爲什麼認爲stringsplit ...一個System.String只能容納一個string。在某個時候你會得到一個string[]

廣場這條線scanInput = Console.ReadLine();後在Visual Studio懸停在scanInput斷點,它會告訴你整個字符串....,或嘗試剛剛印刷出來的字符串,以表明它都在那裏。

編輯:如果您使用十六進制,請嘗試查找造成ascii table上的異常的十六進制值。也許十六進制值導致回車或換行。

+0

我剛剛給我的'Console.ReadLine()'添加了一個斷點,並且該字符串與輸入到控制檯中的內容不同。我的字符串中有十六進制值,你認爲這可能是問題嗎? – AlvinJ

+0

你可以截取這個截圖。控制檯可能會將這些值誤解爲控制檯指令。 – BenVlodgi

+0

@AlvinJ'String' *不能*有十六進制值,只有'Char'。你是在輸入字符串,還是通過重定向輸入? – crashmstr