2017-03-14 2 views
0

Aus irgendeinem Grund, wenn ich diesen Code ausführen merke ich, dass der Buchstabe "I oder ich" oder das Wort "es" das Programm abstürzt. Auch wenn ich einfach auf übersetzen mit nichts reinst, stürzt es auch ab. Ich bin diesen Code immer wieder durchgegangen, aber ich kann das Problem nicht finden. Irgendwelche Vorschläge?PigLatin Translator Crashing C#

public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 


    private void btnTranslate_Click(object sender, EventArgs e) 
    { 

     String input = Convert.ToString(txtInput.Text.Trim()); 
     String inputTr = Regex.Replace(input, " {2,}", " "); 
     String pigLatin = ""; 
     String temp = ""; 
     String restOfWord = ""; 
     String vowels = "AEIOUaeiou"; 
     String consonants = "YBCDFGHJKLMNPQRSTVXWZbcdfghjklmnpqrstvxwzy"; 

     string[] words = inputTr.Split(); 
     foreach (string word in words) 
     { 
      if (string.IsNullOrEmpty(txtInput.Text)) 
      { 
       MessageBox.Show("Text must be entered"); 
      } 

      int index = word.IndexOfAny(new char[] { 'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u' }); 

      if (Regex.IsMatch(word, "[@#$%0-9]")) 
      { 
       pigLatin += word + " "; 
      } 

      else if (!(char.IsPunctuation(word.Last())) && vowels.Contains(word[0]) && word.Contains(word.Substring(1, 2).ToLower())) 

      { 
       pigLatin += word + "way" + " "; 
      } 
      else if (char.IsPunctuation(word.Last()) && vowels.Contains(word[0]) && word.Contains(word.Substring(1, 2).ToLower())) 

      { 
       pigLatin += word.Substring(0, word.Length - 1) + "way" + word.Last() + " "; 
      } 
      else if (!(char.IsPunctuation(word.Last())) && consonants.Contains(word[0]) && word.StartsWith(word.Substring(0, 1).ToLower())) 
      { 
       string firstPart = word.Substring(0, index); 
       restOfWord = word.Substring(index, word.Length - index); 
       pigLatin += restOfWord + firstPart + "ay" + " "; 
      } 

      else if (char.IsPunctuation(word.Last()) && consonants.Contains(word[0]) && word.StartsWith(word.Substring(0, 1).ToLower())) 
      { 
       string firstPart = word.Substring(0, index); 
       restOfWord = word.Substring(index, word.Length - index); 
       pigLatin += restOfWord.Substring(0, restOfWord.Length - 1) + firstPart + "ay" + restOfWord.Last() + " "; 
      } 

      else if (!(char.IsPunctuation(word.Last())) && word.Contains(word.ToUpper()) && vowels.Contains(word.Substring(0, 1).ToUpper())) 
      { 

       pigLatin += word + "WAY" + " "; 

      } 
      else if (char.IsPunctuation(word.Last()) && word.Contains(word.ToUpper()) && vowels.Contains(word.Substring(0, 1).ToUpper())) 
      { 

       pigLatin += word.Substring(0, word.Length - 1) + "WAY" + word.Last() + " "; 

      } 
      else if (!(char.IsPunctuation(word.Last())) && word.StartsWith(word.Substring(0, 1).ToUpper()) && consonants.Contains(word.Substring(0, 1).ToUpper()) && word.Contains(word.Substring(1, 2).ToLower())) 
      { 
       string firstPart = word.Substring(0, index); 
       restOfWord = word.Substring(index, word.Length - index); 
       pigLatin += restOfWord.Substring(0, 1).ToUpper() + restOfWord.Substring(1, restOfWord.Length - 1).ToLower() + firstPart.ToLower() + "ay" + " "; 
      } 

      else if (char.IsPunctuation(word.Last()) && word.StartsWith(word.Substring(0, 1).ToUpper()) && consonants.Contains(word.Substring(0, 1).ToUpper()) && word.Contains(word.Substring(1, 2).ToLower())) 
      { 
       string firstPart = word.Substring(0, index); 
       restOfWord = word.Substring(index, word.Length - index); 
       temp = restOfWord.Substring(0, 1).ToUpper() + restOfWord.Substring(0, restOfWord.Length - 1).ToLower() + firstPart.ToLower() + "ay" + restOfWord.Last() + " "; 
       temp = temp.Remove(0, 1); 
       pigLatin += temp.Substring(0, 1).ToUpper() + temp.Substring(1, temp.Length - 1).ToLower() + " "; 
      } 

      else if (!(char.IsPunctuation(word.Last())) && word.Contains(word.ToUpper()) && consonants.Contains(word.Substring(0, 1).ToUpper())) 
      { 
       string firstPart = word.Substring(0, index); 
       restOfWord = word.Substring(index, word.Length - index); 
       pigLatin += restOfWord.ToUpper() + firstPart.ToUpper() + "AY" + " "; 
      } 

      else if (char.IsPunctuation(word.Last()) && word.Contains(word.ToUpper()) && consonants.Contains(word.Substring(0, 1).ToUpper())) 
      { 
       string firstPart = word.Substring(0, index); 
       restOfWord = word.Substring(index, word.Length - index); 
       pigLatin += restOfWord.Substring(0, restOfWord.Length - 1).ToUpper() + firstPart.ToUpper() + "AY" + word.Last() + " "; 
      } 
      txtOutput.Text = pigLatin; 
     } 

    } 

    private void btnClear_Click(object sender, EventArgs e) 
    { 
     txtInput.Text = ""; 
     txtOutput.Text = ""; 
     txtInput.Focus(); 
    } 
    private void btnExit_Click(object sender, EventArgs e) 
    { 
     this.Close(); 
    } 

    private void txtInput_TextChanged(object sender, EventArgs e) 
    { 

    } 
} 
+2

Absturz = Ausnahme. Sie müssen uns sagen, welche und an welcher Linie. – Sinatr

+0

Welche Ausnahme gibt es? Hast du einen Anruf-Stack? –

+0

InvalidOperationException aufgetreten Ausnahme geworfen: 'System.InvalidOperationException' in System.Core.dll @CallumBradbury –

Antwort

1

An mehreren Stellen im gesamten Code haben Sie „Substring (1, 2)“ - wenn das Wort, das Sie gerade bearbeiten als drei Zeichen kurz ist lang, dann werden Sie eine Ausnahme erhalten, weil Sie eine bekommen versuchen, Teilzeichenfolge, die über das Ende der Zeichenfolge hinausgeht.

Sie müssen die Längenprüfung in Ihren Code einfügen. z.B.

... 
... 
else if (!(char.IsPunctuation(word.Last())) && vowels.Contains(word[0]) && 
&& (word.Length >= 3) && word.Contains(word.Substring(1, 2).ToLower())) 
... 
... 

Nur als Hinweis auf dem Debuggen - könnten Sie einen (bedingten) Haltepunkt auf der Linie setzen, wo die Ausnahme auftritt & dann jeden einzelnen Teil Ihrer prüfen, ob Anweisung in den Direkt-Fenstern (Kopie & Paste) zu sehen, which-Klausel verursacht die Ausnahme.

+1

Plus die 'if (string.IsNullOrEmpty (txtInput.Text))' sollte am Anfang der Methode, außerhalb der 'foreach', plus die' txtOutput.Text = pigLatin; 'sollte nach der' foreach' gesetzt werden ... – xanatos

+0

@PaulF Ich habe jetzt seit ungefähr 10 Stunden damit herumhantiert ... Können Sie mir helfen, Code für die Längenprüfung hinzuzufügen? Ich verstehe jedoch, was Sie mit der Teilzeichenfolge meinen. –

+0

Entschuldigung wegen der Verzögerung - ich bin mir nicht sicher, was Sie mit der Überprüfung der 2-stelligen Teilzeichenkette zu tun versuchen, aber die einfache Lösung besteht darin, (word.Length> = 3) in die if-Anweisung einzufügen, bevor Sie versuchen, die Teilzeichenfolge - siehe oben. – PaulF