捕获错误以结束 printwriter

Catch error to end the printwriter

您好,我正在尝试创建一个程序,当 运行 时,用户可以选择他们是否想要我在单独的纯文本文件中获得的结果,控制台或 html网页浏览器。目前似乎出现的唯一错误是 catch 应该是 finally 但如果我将其更改为 finally 它永远不会关闭我的印刷机。感谢您的帮助。

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileWriter;

import java.io.PrintWriter;

import java.lang.reflect.Array;

import java.util.Scanner;

public class HTML {

    public static void main(String[] args) throws FileNotFoundException {

        int scorehome = 0;
        int scoreaway = 0;
        int invalid = 0;
        int goals = 0;
        int valid = 0;

        Scanner scanner = new Scanner(System.in);

        System.out.println("Do you want to generate plain (T)ext or (H)TML");

        String input = scanner.nextLine();

        boolean generateHTML = false;       

        if ( input.equalsIgnoreCase("H") ) {
            generateHTML = true;
        }
        String line;    // stores the each line of text read from the file

        PrintWriter online = new PrintWriter(new FileWriter("C:/temp/htmloutput.html"));

        while ( scanner.hasNext() ) {
            line = scanner.nextLine();  // read the next line of text from the file

            //split the line
            String [] elements = line.split(":");


                //System.out.println("Element " + (i+1) + " was : " + elements[i]);
                if (elements.length == 4) {


                String home = elements[0].trim();
                String away = elements[1].trim();
                String homescore = elements[2].trim();
                String awayscore = elements[3].trim();

                boolean homescoreVal = false;
                boolean awayscoreVal = false;
                boolean homenameVal = false;
                boolean awaynameVal = false;

                    try {   // "try" is a special statement which allows us to deal with "exceptions"
                        scorehome = Integer.parseInt(homescore);    // attempt to convert the String into an Integer type value
                        homescoreVal = true;
                    } catch (NumberFormatException e) {
                        homescoreVal = false;
                    }
                    try {
                        scoreaway = Integer.parseInt(awayscore);    // attempt to convert the String into an Integer type value
                        awayscoreVal = true;
                    } catch (NumberFormatException e) {
                        homescoreVal = false;
                    }

                    if (home.length() <= 1) {
                        homenameVal = false;
                    } else {
                        homenameVal = true;
                    }

                    if (away.length() <= 1) {
                        awaynameVal = false;
                    } else {
                        awaynameVal = true;
                    }

                    if (homescoreVal == true && awayscoreVal == true
                            && homenameVal == true && awaynameVal == true){ 

                        System.out.println(home + " [" + scorehome + "] | "
                                + away + " [" + scoreaway + "]\r");



                    goals = (scorehome + scoreaway) + goals;

                    valid = 1 + valid;
                    } else {
                        invalid = 1 +invalid;
                    }

                }

                else {
                    invalid = 1 + invalid;
                }
            }

        System.out.println("\rValid match was " + valid);
        System.out.println("Total goals scored was " + goals);
        System.out.println("Invalid match count was " + invalid + ".");

        System.out.println("\nEOF");    // Output and End Of File message.

        if (generateHTML == true) {
            online.println("\rValid match was " + valid);
            online.println("Total goals scored was " + goals);
            online.println("Invalid match count was " + invalid + ".");
            }

        String locationOfFile = "C:\temp\htmloutput.html";

        try { 
            Runtime.getRuntime().exec("cmd.exe /C start " + locationOfFile);

        }   catch {
            System.err.println("Error: unable to open " + locationOfFile);
            }
        }
    }

错误信息:线程异常"main"java.lang.Error:未解决的编译问题: 语法错误,插入 "Finally" 以完成 TryStatement

at HTML.main(HTML.java:117)

据我阅读你的代码,你的代码逻辑没有意义,没有你正在寻找的内容

你应该坐下来分解你的问题

  1. 您需要弄清楚如果您的用户键入 H 来选择 HTML 或 T 来选择纯文本会发生什么。

例如将这段代码作为您的蓝图

    System.out.println("Either enter t for plain text\n or h for HTML");
    Scanner input = new Scanner(System.in);
    String  answer = input.nextLine();

    if(answer.equalsIgnoreCase("H")){
        System.out.println("wowwwwww you chose HTML");

        }
    else if( answer.equalsIgnoreCase("T")){
        System.out.println("wowwwwww you chose plain text");
     }
}
  1. 之后需要看每个case要做什么,一般按照下面的就可以了

      1. you gonnna read your file that you have how? using Scanner 
      2. when you have your data inside a array or whatever data structure 
      3. you gonna do your manipulation 
      4.the last job is to write back to wherever you wanna 
    

关于 try catch 块你应该知道的一些要点

  1. 你可以有 try 块和 finally 块而没有 catch 块
  2. 如果您的 try 块出现问题,您的 catch 块会通知您您的问题,例如

例如:

 try{
        System.out.print(7/0);
    }catch(Exception e){
        System.out.println(e);
    }finally{
        System.out.println("no matter what I gonna be executed");
    } 

在您的 try 块中,您尝试将一个 7 乘以 0 的数字潜水,这会引发异常

  1. 无论你看到什么,你的 finally 块都会被执行
  2. 如果你的 try 块工作发现你的 catch 块从未执行但你的 finally 块再次执行
  3. 如果您的 try 块出现问题,其他语句将不会执行,如果您有
  4. ,您将转到您的 catch 块