2017-02-17 4 views
1

ich dieses Programm schreibe, die [("/* ausgeglichen, wenn { prüft sind mit }])"*/ Wir sind verpflichtet, Sachen in den Kommentarblöcke zu ignorieren. Ich bin auf ein Problem gestoßen, als ich meinen Code im Terminal ausgeführt habe, nichts wird ausgedruckt. Ich weiß, wo der Fehler ist, aber ich weiß nicht, warum es falsch ist. Ich bekomme dieses seltsame Problem, nachdem ich versucht habe, Code zum Ignorieren von Kommentaren zu schreiben. Wir müssen eine eigene Stack-Klasse namens MyStack.java ausschreiben, aber das ist ziemlich einfach, daher werde ich hier nicht erwähnen.Mein Programm entspricht aber nicht druckt nichts

Ich denke Problem hier passiert:

int start = str.indexOf("/*"); 
int end = str.indexOf("*/"); 
if(start != -1){ 
    str = str.substring(0,start) + str.substring(end+2); 

Hier wird der gesamte Code ist: Ich hoffe, es gibt keine logischen Fehler ist

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 


public class SymbolBalance { 

    public static void main(String[] args){ 
     if(args.length>0){ 
      try{ 
       Scanner input = new Scanner(new File (args[0])); 
       MyStack<Character> ms = new MyStack<>(); 
       String str; 
       char ch; 
       boolean quoteStart = true; 
       loop: while(input.hasNext()){ 
        str = input.next(); 
        int start = str.indexOf("/*"); 
        int end = str.indexOf("/*"); 
        if(start != -1){ 
         str = str.substring(0,start) + str.substring(end+2); 
         for(int i=0;i<str.length();i++){ 
         ch = str.charAt(i);  
          if(ch == '{'||ch =='(' || ch=='[')  
           { 
           ms.push(ch); 
           } 
          else if((ch == '/'&&i<str.length() -1&&str.charAt(i+1)=='*')){ 
           ms.push(str.charAt(i+1)); 
          } 

          else if (ch==')'){ 
           if(ms.isEmpty()||ms.pop()!= '('){ 
            System.out.println(") is mismatched"); 
            break loop; 
           } 
          } 
          else if(ch == ']'){ 
           if(ms.isEmpty() || ms.pop() != '['){ 
            System.out.println("] is mismatched"); 
            break loop; 
           }  
          } 
          else if(ch == '}'){ 
           if(ms.isEmpty() || ms.pop() != '{'){ 
            System.out.println("} is mismatched"); 
            break loop; 
           } 
          } 
          else if(ch == '*' && i<str.length()-1&&str.charAt(i+1) == '/'){ 
           if(ms.isEmpty() || ms.pop() != '*'){ 
            System.out.println("*/ is mismatched"); 
            break loop; 
           } 
          } 
          else if(ch == '"'){ 
           if(quoteStart == true) { 
            ms.push(ch); 
            quoteStart = false; 
           } 
           else { 
            if(ms.isEmpty() || ms.pop() != '"'){ 
             System.out.println(" \" is mismatched"); 
             break loop; 
            } 
            quoteStart = true; 
           } 
          } 
         } 
        } 
       } 
       input.close(); 
      } 
      catch(FileNotFoundException e){ 
        System.out.println("Cannot find file"); 
      } 

     } 
     else{ 
       System.out.println("No command line argument"); 
     } 
    } 

} 

Hier habe ich ein Beispiel-Eingabedatei enthalten:

/* this is to test whether the program ignores imbalances in the comment blocks */ 
public class Test3 { 
    public static void main(String[] args) { 
     boolean haveYouWatchedHamiltonYet = true; 
     int theSchuylerSisters = 3; 
     int alexanderhamilton = 1; 
     int aaronburr = 1; 

     boolean amIintheroom = theRoomWhereItHappens(); 


     /* this is a commented block. We're testing if your code 
     can deal with unbalanced things in the comments: /* 
     that was the test for here */ 
    } 

    /*just a general comment */ 
    /* this one has some errors /* { [ { [ } ] */ 


    public boolean theRoomWhereItHappens() { 
     boolean intheRoomWhereItHappens = false; 
     boolean isHappyAboutThis = false; 
     return intheRoomWhereItHappens && isHappyAboutThis; 

    } 
} 
+0

Sie Ihren Code debuggen sollten, bevor Sie eine Frage stellen. –

+0

"* Ich glaube, hier passiert ein Problem *" Warum denkst du das? Was vermuten Sie das Problem * ist *? Was hast du probiert? Mit welchen Problemen sind Sie konfrontiert, dass Sie nicht sicher sind, wie Sie ansprechen sollen? StackOverflow ist eine Q & A-Site, kein Debugging-Service. – dimo414

+0

https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – EJoshuaS

Antwort

2

Sie verwenden die gleichen Zeichen für den Anfang und das Ende der Kommentare:

int start = str.indexOf("/*"); 
    int end = str.indexOf("/*"); 

Das Ende Kommentartext sollte sein:

int end = str.indexOf("*/"); 
+0

hallo, auch nach dieser Bearbeitung funktioniert der Code immer noch nicht. – Kat

Verwandte Themen