简体   繁体   English

捕获错误以结束打印机

[英]Catch error to end the printwriter

Hi i'm trying to create a program which will allow the user when run to choose whether they want the results i have on a separate file in plain text the console or html on a web browser. 嗨,我正在尝试创建一个程序,该程序将允许用户在运行时选择是否希望在纯文本控制台或Web浏览器上的纯文本格式的单独文件中获得结果。 The only error that seems to be appearing at the moment is that the catch should be a finally but if i change it to finally it never closes my printwriter. 目前似乎出现的唯一错误是,接纸架应该是最后一个,但是如果我将其更改为最终,它永远不会关闭我的印刷机。 Thanks for any help. 谢谢你的帮助。

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);
            }
        }
    }

Error message: Exception in thread "main" java.lang.Error: Unresolved compilation problem: Syntax error, insert "Finally" to complete TryStatement 错误消息:线程“ main” java.lang.Error中的异常:未解决的编译问题:语法错误,插入“ Finally”以完成TryStatement

at HTML.main(HTML.java:117)

As far as I have read your code, you code logic does not make sense and does not have the follow that you are looking for 就我阅读过的代码而言,您的代码逻辑没有意义,并且没有您所需要的关注内容

You should seat and break down your issue to pieces 您应该就座并将问题分解成碎片

  1. you need to figure out what is gonna happen if your user type H for choosing HTML or T for plain Text. 您需要弄清楚如果用户输入H来选择HTML或T来输入纯文本,将会发生什么情况。

for example have this piece of code as your blue print 例如将这段代码作为您的蓝图

    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. After that you need to see what you want to do in each case, in general you can follow the follwing 之后,您需要查看每种情况下要执行的操作,通常可以按照以下步骤进行操作

      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 

Some Points you should know about the try catch block 关于try catch块,您应该了解的几点

  1. you can have try block with finally block and without catch block 您可以使用带有try块的try块和没有catch块的块
  2. if something will go wrong in your try block, your catch block will inform you about your issue like 如果您的try块出现问题,则catch块将通知您有关问题的信息,例如

for example: 例如:

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

In your try block you try to dived a number which is seven by zero which throw an exception 在您的try块中,您尝试将7乘以零的数字潜水,这将引发异常

  1. your finally block will be executed no matter what as you see 无论您看到什么,都将执行您的finally块
  2. if your try block works find your catch block never executed but again your finally block get executed 如果您的try块正常工作,则您的catch块从未执行过,但您的finally块再次执行了
  3. if something go wrong in your try block, other statements will not executed and you will go to your catch block if you have 如果您的try块出了问题,则其他语句将不会执行,并且如果有,您将进入catch块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM