简体   繁体   中英

Java -file not being read correctly

this is a post-fix expression calculator and file reader that uses file input to operate on. When F is pressed and the file name is entered the program is suppose to operate on the contents of the file and give output if valid, however it keeps jumping straight to the catch and throwing up an error. I cant see why, any suggestions?

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

you should create Scanner instance only once.
Place Scanner scanner = new Scanner(System.in); as a first statement in your main (before the loop), and remove other new Scanner
The reason is that each Scanner is buffered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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