2016-04-29 8 views
1

so grundsätzlich versuche ich ein Programm zu erstellen, wo, wenn der Benutzer aufgefordert wird, ihren Namen zu fragen, wenn er einen Namen gibt, der in der Datei "badWords.txt" erscheint der Benutzer nicht sein wird fähig, fortzufahren. Das Programm führt nur eine Schleife durch, wenn der Benutzer einen Namen eingibt, der nicht im Dokument gefunden wurde. Ich versuche das unten zu tun, scheitere aber kläglich, kann ich Hilfe bekommen? Ich weiß, dass ich den zweiten Teil mit der Fanganweisung richtig gemacht habe, brauche nur Hilfe mit der ersten. Vielen Dank!Try-Catch-Block Problem mit Text

import java.util.Scanner; 
import java.util.Random; 
import java.io.*; 

public class NameExperiment 
    /** 
    * Prompts user ith 5 quick-fire addition problems, and times 
    * the user for the response provided. 
    */ 
    public static void main(String[] args) 
    { 
    Scanner in = new Scanner(System.in); 
    Random rand = new Random(); 

    System.out.print("Please enter your name: "); 
    String name = in.nextLine(); 
    try 
     { 
     Scanner badWords = new Scanner(new File("badWords.txt")); 
     } 
     while (badWords.hasNext()) 
     catch{ 
     { 
     if (name.equalsIgnoreCase(badWords.next())) 
     { 
     throw (new BadWordException("Watch your tongue")); 
    } 
} 
} 
     System.out.println("Hello " + name + 
    ". Please Answer the questions as fast as you can."); 


    for (int i = 0; i < 5; i++) 
    { 
    System.out.println("Hit <ENTER> when ready for a question."); 
    in.nextLine(); 

    int a = rand.nextInt(100); 
    int b = rand.nextInt(100); 

    long startTime = System.currentTimeMillis(); 

    System.out.print(a + " + " + b + " = "); 
    String response = in.nextLine(); 
    try 
    { 
    int number = Integer.parseInt(response); 

    long endTime = System.currentTimeMillis(); 




    String outcome = (number == a + 
    b) ? "Correct!" : "Incorrect."; 

    System.out.println(outcome); 
    System.out.println("That took " + (endTime - 
    startTime) + " milliseconds"); 

    } 
    catch (NumberFormatException exception) 
    { 
    System.out.print("Inappropriate Input: please enter a number."); 
    } 

    } 
    System.out.println("Thank you "+ name + ", goodbye."); 
    } 
    } 
    } 
+0

Dieser Code ist nicht syntaktisch gültige Java. Bitte senden Sie den kompilierbaren Code. –

Antwort

0

bearbeiten ersten Teil und getestet jetzt seine Arbeit

public class NameExperiment{ 
     /** 
     * Prompts user ith 5 quick-fire addition problems, and times 
     * the user for the response provided. 
     */ 
     public static void main(String[] args) throws IOException { 
      Scanner in = new Scanner(System.in); 
      Random rand = new Random(); 
      Scanner badWords = new Scanner(new File("src", "badWords.txt"));; 

      System.out.print("Please enter your name: "); 
      String name = in.nextLine(); 

      while (badWords.hasNext()) 
       if (name.equalsIgnoreCase(badWords.next())) 
       { 
        throw new NumberFormatException("Watch your tongue"); 
       } 
      System.out.println("Hello " + name +". Please Answer the questions as fast as you can."); 


      for (int i = 0; i < 5; i++) 
      { 
       System.out.println("Hit <ENTER> when ready for a question."); 
       in.nextLine(); 

       int a = rand.nextInt(100); 
       int b = rand.nextInt(100); 

       long startTime = System.currentTimeMillis(); 

       System.out.print(a + " + " + b + " = "); 
       String response = in.nextLine(); 
       try 
       { 
        int number = Integer.parseInt(response); 

        long endTime = System.currentTimeMillis(); 

        String outcome = (number == a + 
          b) ? "Correct!" : "Incorrect."; 

        System.out.println(outcome); 
        System.out.println("That took " + (endTime - 
          startTime) + " milliseconds"); 

       } 
       catch (NumberFormatException exception) 
       { 
        System.out.print("Inappropriate Input: please enter a number."); 
       } 

      } 
      System.out.println("Thank you "+ name + ", goodbye."); 
     } 
} 
+0

Vielen Dank, ich schätze es wirklich, das hat perfekt funktioniert !!!! – Alex

+0

Ihr willkommener Freund – Hosseini