开发者

Why is it still storing my previous inputstream

I am going through the inputStream and checking if the first input of the file has the valid input but the problem is if it doesn't I call my method to read in file again but it still has the information from the previous scan of the file. Is there anyway I can tell it to just reset what was in the inputStream and start all over again ?

 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) 
{

    inputStream = null;
     table.clear(); 
    sntypes.clear();
    displaySize.clear();
    header.clear();
    numberOfColumns = 0;
    original.clear();
    Scanner keyboard = new Scanner(System.in);
    String fileName = keyboard.nextLine();
    System.out.println(inputStream);
    boolean done = false;
    while(!done)
    {
        try 
        {
            inputStream = new Scanner(new FileInputStream(fileName));
            done = true;

            header.clear();
        }
        catch(FileNotFoundException E)
        {   System.out.println("Error in opening file ");
        System.out.print("enter data file: ");
            fileName = keyboard.nextLine();

        }
    }

    // file is now open and input scanner attached
    boolean done1 = false;
    if ( inputStream.hasNextLine() ) {

        String csvLine = inputStream.nextLine();

        Scanner lineparse = new Scanner(csvLine);
        lineparse.useDelimiter(",");
        ArrayList<String> rowOne = new ArrayList<String>();

        while (lineparse.hasNextLine()) {
            String temp = lineparse.next();
            String originaltemp = temp; 
            writeOut.add(temp);
            temp = temp + "(" + (++numberOfColumns) + ")";
            displaySize.add(temp.length());


            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, inputStream );

            }

        }
        // add table here it gives problem later on...
        original.add(rowOne);

    }

    while (inputStream.hasNextLine()) 
    {
        String csvLine = inputStream.nextLine();

        Scanner lineparse = new Scanner(csvLine);
        lineparse.useDelimiter(",");

        ArrayList row = new ArrayList();
        int j = 0;
        while (lineparse.hasNext()) {
            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.o开发者_开发技巧ut.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();
}


You're calling the method recursively - which is actually not ideal, but we'll leave that for the moment (the method is also way longer than is good for maintainability) - but the problem is that after you've made the recursive call and it's completed, you're still continuing with the original scanner. I suspect if you just add a return; statement after the recursive call, it will be fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜