2016-05-27 1 views
0

Dans mon programme, j'ai un LString.java qui n'est pas censé avoir une entrée ou une sortie, mais comment tester la compilation via LStringTest.java. Ce programme passe également par junit.jar.JAVA - LString Java, chaînes liées

Mon programme fait du bien pour la première moitié, mais commence alors à me donner des messages d'erreur que je ne suis pas exactement sûr de ce qu'il me dit de faire et comment y remédier. Je suppose que ma question est comment puis-je arriver à résoudre ces erreurs afin que mon programme puisse passer le reste du test sans erreur. Merci d'avance!

Ci-dessous, est la sortie de mon code à ce jour et les erreurs qu'il me donne:

----jGRASP exec: java LStringTest 
Running constructor, length, toString tests (10 tests) 
Starting tests: .......... 
Time: 0.010 
OK! (10 tests passed.) 

Running compareTo and equals tests (18 tests) 
Starting tests: .................. 
Time: 0.022 
OK! (18 tests passed.) 

Running charAt and setCharAt tests (18 tests) 
Starting tests: .................. 
Time: 0.032 
OK! (18 tests passed.) 

Running substring tests (63 tests) 
Starting tests: ............................................................... 
Time: 0.076 
OK! (63 tests passed.) 

Running replace tests (31 tests) 
Starting tests: E...EEE....EEEEEEE...EEEEEEE... 
Time: 0.046 

There were 18 failures: 
1) test61aReplaceEmptyString(LStringTest$LStringReplaceTestSpecial) 
java.lang.NullPointerException 
     at LString.replace(LString.java:171) 
     at LStringTest$LStringReplaceTestSpecial.test61aReplaceEmptyString(LStringTest.java:553) 
     ... 10 more 
2) test63aReplaceOneChar(LStringTest$LStringReplaceTestSpecial) 
java.lang.NullPointerException 
     at LString.replace(LString.java:190) 
     at LStringTest$LStringReplaceTestSpecial.test63aReplaceOneChar(LStringTest.java:576) 
     ... 10 more 
3) test63bReplaceOneChar(LStringTest$LStringReplaceTestSpecial) 
java.lang.AssertionError: replace returned different LString expected same:<a> was not:<axyzzy> 
     at org.junit.Assert.fail(Assert.java:88) 
     at org.junit.Assert.failNotSame(Assert.java:737) 
     at org.junit.Assert.assertSame(Assert.java:680) 
     at LStringTest$LStringReplaceTestSpecial.test63bReplaceOneChar(LStringTest.java:587) 
     ... 10 more 
4) test63cReplaceOneChar(LStringTest$LStringReplaceTestSpecial) 
org.junit.ComparisonFailure: Replace of One Character LString is wrong expected:<[]xyzzy> but was:<[a]xyzzy> 
     at org.junit.Assert.assertEquals(Assert.java:115) 
     at LStringTest$LStringReplaceTestSpecial.test63cReplaceOneChar(LStringTest.java:593) 
     ... 10 more 
5) test71aReplacePrepend[0](LStringTest$LStringReplaceTest) 
java.lang.NullPointerException 
     at LString.replace(LString.java:190) 
     at LStringTest$LStringReplaceTest.test71aReplacePrepend(LStringTest.java:660) 
     ... 10 more 
6) test71bReplaceAppend[0](LStringTest$LStringReplaceTest) 
java.lang.AssertionError: replace returned different LString expected same:<abcd> was not:<abcdxyzzy> 
     at org.junit.Assert.fail(Assert.java:88) 
     at org.junit.Assert.failNotSame(Assert.java:737) 
     at org.junit.Assert.assertSame(Assert.java:680) 
     at LStringTest$LStringReplaceTest.test71bReplaceAppend(LStringTest.java:673) 
     ... 10 more 
7) test71cReplaceInMiddle[0](LStringTest$LStringReplaceTest) 
java.lang.AssertionError: replace returned different LString expected same:<abcd> was not:<abxyzzycd> 
     at org.junit.Assert.fail(Assert.java:88) 
     at org.junit.Assert.failNotSame(Assert.java:737) 
     at org.junit.Assert.assertSame(Assert.java:680) 
     at LStringTest$LStringReplaceTest.test71cReplaceInMiddle(LStringTest.java:683) 
     ... 10 more 
8) test71dReplaceAtStart[0](LStringTest$LStringReplaceTest) 
java.lang.NullPointerException 
     at LString.replace(LString.java:190) 
     at LStringTest$LStringReplaceTest.test71dReplaceAtStart(LStringTest.java:690) 
     ... 10 more 
9) test71eReplaceAtEnd[0](LStringTest$LStringReplaceTest) 
java.lang.AssertionError: replace returned different LString expected same:<abcd> was not:<abxyzzy> 
     at org.junit.Assert.fail(Assert.java:88) 
     at org.junit.Assert.failNotSame(Assert.java:737) 
     at org.junit.Assert.assertSame(Assert.java:680) 
     at LStringTest$LStringReplaceTest.test71eReplaceAtEnd(LStringTest.java:703) 
     ... 10 more 
10) test71fReplaceInMiddle[0](LStringTest$LStringReplaceTest) 
java.lang.AssertionError: replace returned different LString expected same:<abcd> was not:<abxyzzycd> 
     at org.junit.Assert.fail(Assert.java:88) 
     at org.junit.Assert.failNotSame(Assert.java:737) 
     at org.junit.Assert.assertSame(Assert.java:680) 
     at LStringTest$LStringReplaceTest.test71fReplaceInMiddle(LStringTest.java:713) 
     ... 10 more 
11) test71gReplaceAll[0](LStringTest$LStringReplaceTest) 
org.junit.ComparisonFailure: replace(0, length) is wrong expected:<[]xyzzy> but was:<[a]xyzzy> 
     at org.junit.Assert.assertEquals(Assert.java:115) 
     at LStringTest$LStringReplaceTest.test71gReplaceAll(LStringTest.java:721) 
     ... 10 more 
12) test71aReplacePrepend[1](LStringTest$LStringReplaceTest) 
java.lang.NullPointerException 
     at LString.replace(LString.java:190) 
     at LStringTest$LStringReplaceTest.test71aReplacePrepend(LStringTest.java:660) 
     ... 10 more 
13) test71bReplaceAppend[1](LStringTest$LStringReplaceTest) 
java.lang.AssertionError: replace returned different LString expected same:<Yet another even longer string.> was not:<Yet another even longer string.xyzzy> 
     at org.junit.Assert.fail(Assert.java:88) 
     at org.junit.Assert.failNotSame(Assert.java:737) 
     at org.junit.Assert.assertSame(Assert.java:680) 
     at LStringTest$LStringReplaceTest.test71bReplaceAppend(LStringTest.java:673) 
     ... 10 more 
14) test71cReplaceInMiddle[1](LStringTest$LStringReplaceTest) 
java.lang.AssertionError: replace returned different LString expected same:<Yet another even longer string.> was not:<Yet another evexyzzyn longer string.> 
     at org.junit.Assert.fail(Assert.java:88) 
     at org.junit.Assert.failNotSame(Assert.java:737) 
     at org.junit.Assert.assertSame(Assert.java:680) 
     at LStringTest$LStringReplaceTest.test71cReplaceInMiddle(LStringTest.java:683) 
     ... 10 more 
15) test71dReplaceAtStart[1](LStringTest$LStringReplaceTest) 
java.lang.NullPointerException 
     at LString.replace(LString.java:190) 
     at LStringTest$LStringReplaceTest.test71dReplaceAtStart(LStringTest.java:690) 
     ... 10 more 
16) test71eReplaceAtEnd[1](LStringTest$LStringReplaceTest) 
java.lang.AssertionError: replace returned different LString expected same:<Yet another even longer string.> was not:<Yet another evexyzzy> 
     at org.junit.Assert.fail(Assert.java:88) 
     at org.junit.Assert.failNotSame(Assert.java:737) 
     at org.junit.Assert.assertSame(Assert.java:680) 
     at LStringTest$LStringReplaceTest.test71eReplaceAtEnd(LStringTest.java:703) 
     ... 10 more 
17) test71fReplaceInMiddle[1](LStringTest$LStringReplaceTest) 
java.lang.AssertionError: replace returned different LString expected same:<Yet another even longer string.> was not:<Yet anotxyzzy string.> 
     at org.junit.Assert.fail(Assert.java:88) 
     at org.junit.Assert.failNotSame(Assert.java:737) 
     at org.junit.Assert.assertSame(Assert.java:680) 
     at LStringTest$LStringReplaceTest.test71fReplaceInMiddle(LStringTest.java:713) 
     ... 10 more 
18) test71gReplaceAll[1](LStringTest$LStringReplaceTest) 
org.junit.ComparisonFailure: replace(0, length) is wrong expected:<[]xyzzy> but was:<[Y]xyzzy> 
     at org.junit.Assert.assertEquals(Assert.java:115) 
     at LStringTest$LStringReplaceTest.test71gReplaceAll(LStringTest.java:721) 
     ... 10 more 

Test Failed! (18 of 31 tests failed.) 

Test failures: abandoning other phases. 

----jGRASP wedge: exit code for process is 1. 
----jGRASP: operation complete. 

Voici mon code, LString.java:

import java.io.*; 
import java.util.*; 

public class LString { 

    node front; 
    int size; 

    // Creating a node class 
    private class node { 
      char data; 
      node next; 

      public node() { 

      } 

      public node (char newData) { 
       this.data = newData; 
      } 

      public node (char newData, node newNext) { 
       this.data = newData; 
       this.next = newNext; 
      } 

    } 

    // Constructors 
    public LString(){ 
      this.size = 0; 
      this.front = null; 
    } 

    public LString(String original) { 
      this.size = original.length(); 
      if (original.length() > 0) { 

       this.front = new node(original.charAt(0)); 
       node curr = this.front; 

       for (int i =1; i < original.length(); i++) { 
        curr.next = new node(original.charAt(i)); 
        curr = curr.next; 
       } 
      } 
    } 

    // Length method, returns the length of LString 
    public int length() { 
     return this.size; 
    } 

    // compareTo method, compares this LString to anotherLString, returns 0 if equal, 
    // -1 if lexicogrpahically less, and 1 if lexicographically greater 
    public int compareTo(LString anotherLString) { 
     int len1 = length(); 
     int len2 = anotherLString.length(); 
     int lim = Math.min(len1, len2); 

     node cn1 = front; 
     node cn2 = anotherLString.front; 

     int k = 0; 
     while (k < lim) { 
      char c1 = cn1.data; 
      char c2 = cn2.data; 
      if (c1 != c2) { 
       return c1-c2; 
      } 
      k++; 
      cn1 = cn1.next; 
      cn2 = cn2.next; 
     } 

     return len1 - len2; 
    } 

    // a boolean equals method that returns true if LString and other are the same, false if not 
    public boolean equals(Object other) { 
     if (this == other) { 
      return true; 
     } 

     if (other instanceof LString) { 
      LString otherLString = (LString)other; 
      int n = length(); 
      if (n == otherLString.length()) { 
       node n1 = front; 
       node n2 = otherLString.front; 

       while (n1 != null) { 
        if (n1.data != n2.data) { 
         return false; 
        } 
        n1 = n1.next; 
        n2 = n2.next; 
       } 
       return true; 
      } 
     } 

     return false; 
    } 


    // charAt returns the character of LString at the argument index 
    public char charAt(int index) { 

     if ((index < 0) || (index >= this.length())) { 
      throw new IndexOutOfBoundsException(); 
     } 

     node curNode = front; 
     for (int i = 0; i < this.length(); i++, curNode = curNode.next) { 
      if (i == index) { 
       return curNode.data; 
      } 
     } 

     throw new IllegalStateException(); 
    } 

    public void setCharAt(int index, char ch) { 
     if (index < 0 || index >= this.length()) { 
     throw new IndexOutOfBoundsException(); 
     } 

     else { 
     node currNode = front; 
     for (int i = 0; i <this.length(); i++, currNode = currNode.next) { 
      if (i == index) { 
      currNode.data = ch; 
      } 
     } 
     } 
    } 

    public LString substring(int start, int end) { 
     if (start < 0 || end > this.length() || start > end) { 
     throw new IndexOutOfBoundsException(); 
     } 

     LString substring = new LString(); 
     if (start == end) { 
     return substring; 
     } 
     node node = this.front; 
     for (int i = 0; i < start; i++) { 
     node = node.next; 
     } 

     node copy = new node(node.data); 
     substring.front = copy; 
     for (int i = start+1; i < end; i++) { 
     node = node.next; 
     copy = copy.next = new node(node.data); 
     } 

     substring.size = end - start; 
     return substring;  
    } 

    public LString replace(int start, int end, LString lStr) { 
     if (start < 0 || end > length() || start > end) { 
     throw new IndexOutOfBoundsException(); 
    } 

    LString result = new LString(); 
    node node = this.front; 
    node copy = new node(node.data); 
     result.front = copy; 
    for (int i = 1; i < start; i++) { 
     node = node.next; 
     copy = copy.next = new node(node.data); 
    } 

    node replace = lStr.front; 
    for (int i = 0; i < lStr.length(); i++) { 
     copy = copy.next = new node(replace.data); 
     replace = replace.next; 
    } 

    for (int i = start; i < end; i++) { 
     node = node.next; 
    } 

    for (int i = end; i < length(); i++) { 
     node = node.next; 
     copy = copy.next = new node(node.data); 
    } 

    result.size = length() + lStr.length() - (end - start); 
    return result; 
} 

    public void append(char data) { 

     this.size++; 

     if (front == null) { 
      front = new node(data); 
      return; 
     } 

     node curr = front; 
     while (curr.next != null) { 
      curr = curr.next; 
     } 

     curr.next = new node(data); 

    } 

    public void prepend (char data) { 

     front = new node(data, front); 
     size++; 
    } 


    public void delete(int index) { 

     if (index == 0) { 
      front = front.next; 
     } else { 
      node curr = front; 
      for (int i = 0; i < index - 1; i++){ 
       curr = curr.next; 
      } 

      curr.next = curr.next.next; 
      } 

     size--; 
    } 

    public String toString() { 
     StringBuilder result = new StringBuilder(); 

     node curr = front; 
     while (curr != null) { 

      result.append(curr.data); 
      curr = curr.next; 
     } 

     return result.toString(); 
    } 

    public void add(int index, char data) { 
     if (index == 0) { 
       front = new node(data, front); 
     } else { 
       node curr = front; 
       for (int i = 0; i < index - 1; i++){ 
        curr = curr.next; 
       } 

       curr.next = new node(data, curr.next); 
      } 
     } 

    public void print() { 
    System.out.println(this.toString()); 

    } 
} 

Erreur 1 pour @ScaryWombat:

Voici la morue e de LStringTest.java:

@Test public void test61aReplaceEmptyString() { 
     LString testLString = new LString(); 
     LString newLString = testLString.replace(0, 0, replaceLString); 
     assertEquals("Replace of Empty LString is wrong", 
       replaceString, newLString.toString()); 
     assertSame("replace returned different LString", testLString, newLString); 
     } 

Ceci est l'extrait de code de LString.java qu'il génère des erreurs. A partir de la ligne 171 ...

LString result = new LString(); 
    node node = this.front; 
    node copy = new node(node.data); 
     result.front = copy; 
+0

Eh bien, il est dit - java.lang.NullPointerException à LString.replace (LString.java:171) –

+0

pourriez-vous m'aider à résoudre ce problème? –

+0

dites-moi quelle est la ligne 171 –

Répondre

1

Ok permet de briser ce un peu

En test61aReplaceEmptyString lorsque vous appelez

LString testLString = new LString(); 

il appelle le constructeur

// Constructors 
public LString(){ 
     this.size = 0; 
     this.front = null; 
} 

voir que this.front est null

Maintenant, lorsque vous appelez testLString.replace(0, 0, replaceLString);

vous verrez que dans replace il y a le code

node node = this.front; 

rappeler que this.front est null maintenant node est également null

BTW très déroutant que le nom de classe et le nom de l'objet est le même.

Quoi qu'il en soit, comme node est null alors

new node(node.data); 

jetteront un NPE

Pour résoudre ce problème, vous devez tester si le LString existant est vide (ie noeuds sont nuls)

public LString replace(int start, int end, LString lStr) { 
    if (start < 0 || end > length() || start > end) { 
    throw new IndexOutOfBoundsException(); 
    } 

    if (this.front == null) { 
     return lStr; // maybe correct logic for you? 
    } 
+0

Je comprends ce que vous voulez dire. Fondamentalement, node est nul à cause de this.front dans le constructeur. Comment puis-je réparer cela? –

+0

dans remplacer vérifier que 'node' n'est pas null. –

+0

parlez-vous de la ligne, this.front = null? Que pourrais-je changer pour ne pas donner l'erreur? –