如何将程序重置为主字符串args?

时间:2011-09-05 07:56:28

标签: java main

我正在编写一个程序,如果它遇到异常,我想重置整个程序,无论如何请告诉我,我真的需要今晚完成它吗?

public static void readinfile(ArrayList<ArrayList> table,
            int numberOfColumns,ArrayList<String> header, 
            ArrayList<ArrayList<String>> original,
            ArrayList<String> sntypes, ArrayList<Integer> displaySize, 
            ArrayList<String> writeOut, Scanner inputStream) {
        //System.out.print("enter data file: ");
        Scanner keyboard = new Scanner(System.in);
        System.out.print("enter data file: ");
        String fileName = keyboard.nextLine();
        try {
            System.out.println("try " + fileName);
            inputStream = new Scanner(new FileInputStream(fileName));
            System.out.println(inputStream);
        } catch (FileNotFoundException E) {
            System.out.println("Error in opening file ");
            //readinfile(table, numberOfColumns, header,
            //original, sntypes,displaySize, writeOut, inputStream );
        }
        // file is now open and input scanner attached
        if (inputStream.hasNextLine()) {
            String Line = inputStream.nextLine();
            Scanner lineparse = new Scanner(Line);
            lineparse.useDelimiter(",");
            ArrayList<String> rowOne = new ArrayList<String>();
            while (lineparse.hasNext()) {
                String temp = lineparse.next();
                String originaltemp = temp;
                writeOut.add(temp);
                temp = temp + "(" + (++numberOfColumns) + ")";
                displaySize.add(temp.length());
                // row.add(lineparse.next());
                if (temp.trim().substring(0, 2).equalsIgnoreCase("S ")
                        || temp.trim().substring(0, 2).equalsIgnoreCase("N ")) {
                    rowOne.add(originaltemp);
                    header.add(temp.substring(2));
                    sntypes.add(temp.toUpperCase().substring(0, 2).trim());
                } else {
                    System.out.println("Invalid file please enter a new file: ");
//readinfile(table, numberOfColumns, header, original, sntypes,displaySize,writeOut,Name);                  
                    readinfile(table, numberOfColumns, header,
                            original, sntypes, displaySize, writeOut, inputStream);
                }
            }
            // add table here it gives problem later on...
            original.add(rowOne);
        }

        while (inputStream.hasNextLine()) {
            String Line = inputStream.nextLine();
            Scanner lineparse = new Scanner(Line);
            lineparse.useDelimiter(",");
            ArrayList row = new ArrayList();
            int j = 0;
            while (lineparse.hasNextLine()) {
                String temp = lineparse.next().trim();
                int sizeOfrow = temp.trim().length();
                if (sizeOfrow > displaySize.get(j)) {
                    displaySize.set(j, sizeOfrow);
                }
                if (j < numberOfColumns && sntypes.get(j).equalsIgnoreCase("N")) {
                    try {
                        if (temp.equalsIgnoreCase("")) {
                            row.add(new Double(0.0));
                        } else {
                            row.add(new Double(temp.trim()));
                        }
                    } catch (NumberFormatException E) {
                        System.out.println("Opps there is a mistake "
                                + "I was expecting a number and I found: " + temp);
                        System.out.println("This row will be ignored");
//                      break;
                    }
                } else {
                    if (temp.equalsIgnoreCase("")) {
                        row.add((" "));
                    } else {
                        row.add(temp);
                    }
                }
                j++;
            }
            if (row.size() == numberOfColumns) {
                table.add(row);
            }
        }// close for while
        inputStream.close();
    }

3 个答案:

答案 0 :(得分:2)

功课?

以下是关于如何思考的线索:

  main:
    start loop
      start
        do stuff
        set ok to end
      catch exception
        set not ok to end
    loop if not ok to end

答案 1 :(得分:1)

我不确定你的意思是否如此,但以下代码将一次又一次地运行,直到成功为止(如:不引发异常):

public static void main(String[] args){

    while(true){
        try{
            // execute your code
            break; // if successful, exit loop
        }catch(SomeException e){
            // handle exception
        }catch(SomeOtherException e){
            // handle exception
        }finally{
            // clean up, if necessary
        }
    }

}

注意:while(true)是一个糟糕的构造,我相信你的老师不会喜欢。也许你会找到一种更好的方式来改写它。

答案 2 :(得分:0)

这有点像黑客,但你可以尝试再次调用main方法,传递参数。只要您没有修改参数的字符串数组,只需从主例程中的try / catch块调用main(args);即可。当然,如果异常继续发生,你将无限循环并吹掉堆栈:P