2017-04-13 6 views
-1

Ich weiß, es ist ein bisschen Code, aber es macht mich wirklich verrückt :( Es wirft den Fehler, dass Methode dfs() erwartet einen Rückgabetyp in Zeile 31, aber es wird erklärt, zurückzukehren eine Leere! Was ist hier los? ich scheine die Klammern alle überprüft und der Umfang sieht gut aus iDK, was ist der Grund dafür! vielen Dank für die Hilfe!Fehler: ungültige Methodendeklaration; Rückgabetyp erforderlich (Zeile 31)

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

public class Project10 { 

public static void main(String[] args) throws Exception { 
    BufferedReader br = new BufferedReader(new FileReader(new File("dictionary.txt"))); 
    BufferedReader boardLoad = new BufferedReader(args[0]); 
    ArrayList<String> dictionary = new ArrayList<String>(); 
    HashSet<String> hashDict = new HashSet<String>(); 

    while (br.ready()) { 
     String word = br.readLine(); 
     dictionary.add(word); 
     hashDict.add(word); 
    } 

    Collections.sort(dictionary); 

int boardSize = Integer.parseInt(boardLoad.readLine()); 
String[][] board = new String[boardSize][boardSize]; 
    for (int i = 0; i < boardSize - 1; i++) { 
    for (int j = 0; j < boardSize - 1; i++) { 
     board[i][j] = boardLoad.read(); 
    } 
    } 

int row = 0; 
int col = 0; 
String word = ""; 
dfs(row, col, board, dictionary, hashDict, word); 

} 

private static void dfs(int r, int c, String[][] board, 
         ArrayList<String> dict, HashSet set, String word) { 

word += board[r][c]; 

boolean isWord = isWord(set, word); 
boolean isPrefix = isPrefix(dict, word); 

if(!isWord && !isPrefix) { 
    return; 
} else if (isWord) { 
    System.out.println(word); 
} 

if (r != 0 && isLowerCase(board[r-1][c])) { 
    board[r][c] = board[r][c].toUpperCase(); 
    dfs(r-1, c, board, word); 
    board[r][c] = board[r][c].toLowerCase(); 
} 

if (r != 0 && c != board.length - 1 && isLowerCase(board[r-1][c+1])) { 
    board[r][c] = board[r][c].toUpperCase(); 
    dfs(r-1, c+1, board, word); 
    board[r][c] = board[r][c].toLowerCase(); 
} 

if (c != board.length - 1 && isLowerCase(board[r][c+1])) { 
    board[r][c] = board[r][c].toUpperCase(); 
    dfs(r, c+1, board, word); 
    board[r][c] = board[r][c].toLowerCase(); 
} 

if (r != board.length && c != board.length - 1 && isLowerCase(board[r+1][c+1])) { 
    board[r][c] = board[r][c].toUpperCase(); 
    dfs(r+1, c+1, board, word); 
    board[r][c] = board[r][c].toLowerCase(); 
} 

if (r != board.length - 1 && isLowerCase(board[r+1][c])) { 
    board[r][c] = board[r][c].toUpperCase(); 
    dfs(r+1, c, board, word); 
    board[r][c] = board[r][c].toLowerCase(); 
} 

if (r != board.length - 1 && c != 0 && isLowerCase(board[r+1][c-1])) { 
    board[r][c] = board[r][c].toUpperCase(); 
    dfs(r+1, c-1, board, word); 
    board[r][c] = board[r][c].toLowerCase(); 
} 

if (c != 0 && isLowerCase(board[r][c-1])) { 
    board[r][c] = board[r][c].toUpperCase(); 
    dfs(r, c-1, board, word); 
    board[r][c] = board[r][c].toLowerCase(); 
} 

if (r != 0 && c != 0 && isLowerCase(board[r-1][c-1])) { 
    board[r][c] = board[r][c].toUpperCase(); 
    dfs(r-1, c-1, board, word); 
    board[r][c] = board[r][c].toLowerCase(); 
} 

} 


private static boolean isWord(HashSet<String> hashDict, String word) { 
return hashDict.contains(word); 
} 

private static boolean isPrefix(ArrayList<String> dictionary, String word) { 
int lo = 0; 
int hi = dictionary.size() - 1; 
while(lo <= hi) { 
    int mid = lo + (hi - lo)/2; 
    if (dictionary.get(mid).startsWith(word)) { 
    return true; 
    } else if (dictionary.get(mid).compareTo(canonWord) < 0) { 
    lo = mid + 1; 
    } else { 
    hi = mid - 1; 
    } 
} 
    return false; 
} 

private static boolean isLowerCase(String str) { 
    return str.equals(str.toLowerCase()); 
} 

} 
+2

Bitte starten Sie mit Einrückungen Code korrekt zu lösen - entweder von Hand oder vorzugsweise unter Verwendung des „Format Source Code“ Funktion Ihres IDE. Ich bin mir sicher, dass du irgendwo ein "}" oder ein überflüssiges '{' 'finden wirst. –

+0

Das Problem liegt in Ihrem if-Block Wenn Sie rekursiv versuchen, die "dfs" -Methode aufzurufen, übergeben Sie den falschen Typ/Anzahl der Argumente. dies -> dfs (r-1, c, board, word); ist das Problem. Stellen Sie sicher, dass Sie die erforderliche Anzahl von Argumenten hinzufügen, und stellen Sie sicher, dass die Typen ebenfalls übereinstimmen. –

+1

Sie haben mehrere Kompilierungsfehler, der erste ist 'BufferedReader boardLoad = neuer BufferedReader (neuer FileReader (neue Datei (args [0]));' - der nächste ist ziemlich groß, Ihre rekursiven dfs-Aufrufe stimmen nicht mit der Methodensignatur überein . –

Antwort

1

Ihr Code viele Compiler-Fehler und nichts über diese Methode dfs() erwartet einen Rückgabetyp in Zeile 31.

Die gleichen Compilerfehler in Ihrem Code könnten wie folgt korrigiert werden:

//First line to correct 
BufferedReader boardLoad = new BufferedReader(new FileReader(new File(args[0]))); 

//Second line to correct 
board[i][j] = String.valueOf(boardLoad.read()); 

//Next N lines to correct 
dfs(r - 1, c + 1, board, dict, set,word); 

Ich empfehle Ihnen, eine beliebige IDE zu verwenden, um das Problem

Verwandte Themen