Java文件未正确读取

时间:2015-11-05 15:15:11

标签: java

这是一个使用文件输入进行操作的修复后表达式计算器和文件阅读器。当按下F并输入文件名时,程序假定对文件的内容进行操作并在有效的情况下给出输出,但是它会一直跳到catch并抛出错误。我不明白为什么,有什么建议吗?

import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class assignment {

    public static void main(String[] args) {
        String option;
        char letter;
    while(true)
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter K to input data or F to use file input");
        option = scanner.nextLine();
        letter = option.charAt(0);

        switch (Character.toUpperCase(letter)) 
        {
        case 'K': 
        System.out.println("Please enter a calculation, or press enter to close");

        String line = scanner.nextLine();

            if (line.equals(""))
            {
                System.out.println("Calculator has been closed");
                System.exit(0); 
            }

        String [] element = line.split(" "); 

            if (element.length == 3) 
            {
            System.out.println("You enter: " + element[0]+ " " + element[1]+" " + element[2]); 
            try{
                double number1, number2;
                number1 = Double.parseDouble(element[0]);
                number2 = Double.parseDouble(element[1]); 

                    if (element[2].equals("+"))
                    {
                        System.out.println("Total:" + (number1 + number2));
                    }
                    else if (element[2].equals("/"))
                    {
                        System.out.println("Total:" + (number1 / number2));
                    }
                    else if (element[2].equals("-"))
                    {
                        System.out.println("Total:" + (number1 - number2));
                    }
                    else if (element[2].equals("*"))
                    {
                        System.out.println("Total:" + (number1 * number2));
                    }
                    else{
                    System.out.println("That is incorect input; please try again");
                    scanner.close();
                    }
                }
            catch(NumberFormatException e)
                {
                System.out.println("Error"+e.getMessage());
                }
        }
            else{
                System.out.println("You have inserted Incorrect input");
            }

        break;
        case 'F':
            try{
            System.out.println("you have chosen to use a file input");
            Scanner file = new Scanner (System.in);
            System.out.println("input file name:");
            String input = file.nextLine ();
            Scanner s = new Scanner (new File (input));


                while ( s.hasNext() ) {

            line = s.nextLine();
            String FileInput [] =input.split(" ");

                try
                {
                    double inp1, inp2;
                    inp1 = Double.parseDouble(FileInput[0]);
                    inp2 = Double.parseDouble(FileInput[1]);

                    if (FileInput[2].equals("+"))
                    {
                        System.out.println("Total:" + (inp1 + inp2));
                    }
                    else if (FileInput[2].equals("/"))
                    {
                        System.out.println("Total:" + (inp1 / inp2));
                    }
                    else if (FileInput[2].equals("-"))
                    {
                        System.out.println("Total:" + (inp1 - inp2));
                    }
                    else if (FileInput[2].equals("*"))
                    {
                        System.out.println("Total:" + (inp1 * inp2));
                    }
                    else
                    {
                    System.out.println("That is incorect input; please try again");
                    }
                }
                catch(NumberFormatException e)
                {
                    System.out.println("invalid number" + e);
                }
            }
            System.out.println("\nEOF");
            file.close();
            s.close();
            break;
            }
             catch (FileNotFoundException e) {
                System.out.println("incorrect filename");
            }   
            default:
    System.out.println("Incorrect letter entered");
    }           
 }
}
}

1 个答案:

答案 0 :(得分:1)

您应该只创建一次Scanner实例 地点 Scanner scanner = new Scanner(System.in); 作为主要的第一个语句(在循环之前),并删除其他new Scanner
原因是每个扫描仪都被缓冲。