Java 尝试用文本捕获块问题

Java 尝试用文本捕获块问题,java,try-catch,Java,Try Catch,因此,基本上我正在尝试创建一个程序,当提示用户询问他们的名字时,如果他给出的名字出现在文件“badWords.txt”中,用户将无法继续。仅当用户输入文档中找不到的名称时,程序才会循环。我试图做到这一点,但失败惨重,我可以得到任何帮助吗?我知道我用catch语句纠正了第二部分,只是需要第一部分的帮助。谢谢大家! import java.util.Scanner; import java.util.Random; import java.io.*; public class NameExperi

因此,基本上我正在尝试创建一个程序,当提示用户询问他们的名字时,如果他给出的名字出现在文件“badWords.txt”中,用户将无法继续。仅当用户输入文档中找不到的名称时,程序才会循环。我试图做到这一点,但失败惨重,我可以得到任何帮助吗?我知道我用catch语句纠正了第二部分,只是需要第一部分的帮助。谢谢大家!

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.");
  }
   }
  }   
import java.util.Scanner;
导入java.util.Random;
导入java.io.*;
公开课实验
/**
*提示用户5个快速开火添加问题和次数
*提供的响应的用户。
*/
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(系统输入);
Random rand=新的Random();
System.out.print(“请输入您的姓名:”);
字符串名称=in.nextLine();
尝试
{
Scanner badWords=新的扫描仪(新文件(“badWords.txt”);
}
while(badWords.hasNext())
抓住{
{
if(name.equalsIgnoreCase(badWords.next()))
{
抛出(新的坏词例外(“注意你的舌头”);
}
}
}
System.out.println(“Hello”+name+
“。请尽快回答问题。”);
对于(int i=0;i<5;i++)
{
System.out.println(“准备好提问时点击”);
in.nextLine();
int a=兰特·耐克斯汀(100);
int b=兰特·耐克斯汀(100);
long startTime=System.currentTimeMillis();
系统输出打印(a+“+”+b+“=”);
字符串响应=in.nextLine();
尝试
{
int number=Integer.parseInt(响应);
long-endTime=System.currentTimeMillis();
字符串结果=(数字==a)+
b) “正确!”:“不正确。”;
系统输出打印LN(结果);
System.out.println(“这花费了”+(endTime-
开始时间)+“毫秒”);
}
捕获(NumberFormatException异常)
{
系统输出打印(“输入不正确:请输入数字”);
}
}
System.out.println(“谢谢”+name+“,再见”);
}
}
}   

编辑第一部分并立即测试其工作

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.");
        }
}
公共类名实验{
/**
*提示用户5个快速开火添加问题和次数
*提供的响应的用户。
*/
公共静态void main(字符串[]args)引发IOException{
扫描仪输入=新扫描仪(系统输入);
Random rand=新的Random();
Scanner badWords=新的扫描仪(新文件(“src”、“badWords.txt”);;
System.out.print(“请输入您的姓名:”);
字符串名称=in.nextLine();
while(badWords.hasNext())
if(name.equalsIgnoreCase(badWords.next()))
{
抛出新的NumberFormatException(“注意你的舌头”);
}
System.out.println(“你好”+name+”。请尽快回答问题。”);
对于(int i=0;i<5;i++)
{
System.out.println(“准备好提问时点击”);
in.nextLine();
int a=兰特·耐克斯汀(100);
int b=兰特·耐克斯汀(100);
long startTime=System.currentTimeMillis();
系统输出打印(a+“+”+b+“=”);
字符串响应=in.nextLine();
尝试
{
int number=Integer.parseInt(响应);
long-endTime=System.currentTimeMillis();
字符串结果=(数字==a)+
b) “正确!”:“不正确。”;
系统输出打印LN(结果);
System.out.println(“这花费了”+(endTime-
开始时间)+“毫秒”);
}
捕获(NumberFormatException异常)
{
系统输出打印(“输入不正确:请输入数字”);
}
}
System.out.println(“谢谢”+name+“,再见”);
}
}

编辑第一部分并立即测试其工作

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.");
        }
}
公共类名实验{
/**
*提示用户5个快速开火添加问题和次数
*提供的响应的用户。
*/
公共静态void main(字符串[]args)引发IOException{
扫描仪输入=新扫描仪(系统输入);
Random rand=新的Random();
Scanner badWords=新的扫描仪(新文件(“src”、“badWords.txt”);;
System.out.print(“请输入您的姓名:”);
字符串名称=in.nextLine();
while(badWords.hasNext())
if(name.equalsIgnoreCase(badWords.next()))
{
抛出新的NumberFormatException(“注意你的舌头”);
}
System.out.println(“你好”+name+”。请尽快回答问题。”);
对于(int i=0;i<5;i++)
{
System.out.println(“准备好提问时点击”);
in.nextLine();
int a=兰特·耐克斯汀(100);
int b=兰特·耐克斯汀(100);
long startTime=System.currentTimeMillis();
系统输出打印(a+“+”+b+“=”);
字符串响应=in.nextLine();
尝试
{
int number=Integer.parseInt(响应);
long-endTime=System.currentTimeMillis();
字符串结果=(数字==a)+
b) “正确!”:“不正确。”;
系统输出打印LN(结果);
System.out.println(“这花费了”+(endTime-
开始时间)+“毫秒”);
}
捕获(NumberFormatException异常)
{
s