2016-03-26 6 views
-1

Beide Probleme sind in der getCity-Methode, in den Kommentaren gekennzeichnet. jede Hilfe wäre toll, auch wenn es andere Fehler gibt, die Sie sehen, während Sie lesen, werde ich jede Hilfe akzeptieren, die ich bekommen kann.Fehler in meiner Methode

//DO NOT ALTER THE MAIN METHOD 
public static void main(String[] args) { 
    //determine input file 
    String fileName = "coven_consulting.txt"; 
    //print method to output breakdown 
    printReport(fileName); 
} 

/* printReport - take the file name, open the file, read and process data, print out report 
* input: String fileName - the name of the file containing the data 
* returns: nothing 
*/ 
private static void printReport(String fileName) { 
    //implement this method 
} 

/* getCity - ask the user for a city, loop unitl the user gives you a valid one 
* input: none 
* returns: String - the name of the validated city 
*/ 
@SuppressWarnings("empty-statement") 
private static String getCity() { 
//implement this method, change the return statement to suit your needs 
    Scanner keyboard = new Scanner (System.in); 
    String input; 
    String city = ""; 

    do { 
     System.out.print("Which city do you want a report for?"); 
     input = keyboard.next(); 

     if (checkValidCity(input) == true) 
     input = city; 
     while (checkValidCity(input) == false); 
      System.out.print("Not a city we consult in, try another...");  
    } //Error: says while expected 
    return city; //Error: says illegal start to expression 
} 

private static boolean checkValidCity(String input) { 
    //implement this method, change the return statement to suit your needs 
    boolean result; 
    if (input.equalsIgnoreCase ("Uberwald") || 
     (input.equalsIgnoreCase ("Pseudopolis")) || 
     (input.equalsIgnoreCase ("Quirm")) || 
     (input.equalsIgnoreCase ("AnkhMorpork"))) 
     result = true; 
    else 
     result = false; 
    return result; 
} 

Antwort

0

Ihre Syntax ändern. Die while sollte außerhalb des Körpers von do sein.

Die korrekte Syntax ist:

do { 
    // code, bla, blaaa 
} while (condition); 

So Ihr Code sollte wie folgt aussehen:

do { 
    System.out.print("Which city do you want a report for?"); 
    input = keyboard.next(); 
    if (checkValidCity(input)) { 
     input = city; 
    } 
    System.out.print("Not a city we consult in, try another...");  
} while (!checkValidCity(input)); 

Ein Tipp für Sie, bitte bei der Verwendung von if-else Anweisung, bitte nie für Klammern vergessen {}.

+0

danke für die Hilfe, dies behoben – James

+0

Froh, es funktioniert. Wenn diese Antwort oder eine andere Lösung Ihr Problem behoben hat, markieren Sie es als akzeptiert. –

2
do { 
    // code 
} 

ist kein gültiger Ausdruck.

Sie suchen do {} while (booleanExpression);

Ihr zweiter Fehler ist auf die erste fällig.


while (checkValidCity(input) == false); 

Sie kein Semikolon am Ende brauchen, sonst wird nichts passieren.

0

Ändern Sie den Code zu below.Syntax für tun, während zu tun ist {// diese} while (Bedingung)

do { 
      System.out.print("Which city do you want a report for?"); 
      input = keyboard.next(); 



      if (checkValidCity(input) == true) 
      input = city; 


     }  while (checkValidCity(input) == false) 
Verwandte Themen