这是我创建的计算器和文件阅读器。但是,当我输入程序读取的文件名时,它会读取该文件,该文件是一个包含 24 个和的文本文档,例如“10 10 +”。然而我的程序只读取其中大约 11 个,然后返回一个错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at Calculator.main(Calculator.java:104)
我缺少什么才能修复该错误?谢谢。
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Calculator {
public static void main(String[] args) {
String option;
while(true){
Scanner scanner3 = new Scanner(System.in);
System.out.println("Please input the letter K for keyboard or F for file entry:");
option=scanner3.nextLine();
switch(option)
{
case "K":
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter an expression:");
String line = scanner.nextLine();
if (line.equals(""))
{
System.out.println("Calculator Closing");
System.exit(0);
}
char letter;
String [] elements = line.split(" ");
double number1 = 0, number2 = 0;
if (elements.length == 3) // 3 elements entered validation
{
try{
number1 = Double.parseDouble(elements[0]);
number2 = Double.parseDouble(elements[1]);
}
catch (NumberFormatException e) {
System.out.println("Error");
}
if (elements[2].equals("-")) //validation for when the expressions are entered
{
System.out.println("Result as follows:" + (number1 - number2));
}
else if (elements[2].equals("/"))
{
System.out.println("Result as follows:" + (number1 / number2));
}
else if (elements[2].equals("*"))
{
System.out.println("Result as follows:" + (number1 * number2));
}
else if (elements[2].equals("+"))
{
System.out.println("Result as follows:" + (number1 + number2));
}
}
else if(elements.length != 3){ //validation to check that all 3 elements have been entered
System.out.println("Invalid number of elements entered");
}
break; // separates the code
case "F":
try{
System.out.println("Please enter the filename:");
Scanner file = new Scanner (System.in);
String filename = file.nextLine();
Scanner s = new Scanner(new File(filename)); // creates a scanner which scans from a file
boolean value = true;
while (value){
while ( s.hasNext() ) {
line = s.nextLine(); // reads the next line of text from the file
String [] fileinput = line.split(" ");
double expression1 = 0, expression2 = 0;
try{
expression1 = Double.parseDouble(fileinput[0]);
expression2 = Double.parseDouble(fileinput[1]);
}
catch (NumberFormatException e) {
System.out.println("error");
}
if (fileinput[2].equals("-"))
{
System.out.println("Result as follows:" + (expression1 - expression2));
}
else if (fileinput[2].equals("/"))
{
System.out.println("Result as follows:" + (expression1 / expression2));
}
else if (fileinput[2].equals("*"))
{
System.out.println("Result as follows:" + (expression1 * expression2));
}
else if (fileinput[2].equals("+"))
{
System.out.println("Result as follows:" + (expression1 + expression2));
}
}
System.out.println("\nEOF"); // Outputs the End Of File message
value = false;
}
}
catch(FileNotFoundException ef) {
System.out.println("Error, please enter a correct file name.");
}
break;
default:
System.out.println("invalid letter entered");
}
}
}}
最佳答案
我用我自己的文件文本测试你的代码,每一行都有一个后修复操作,它工作没有异常(没有问题),如果你的操作输入中有一些错误,你应该检查你的文件。
但是您的代码中有一些注释:
- 您应该为每个源使用一个扫描程序,例如 System.in
或 new File("name of file")
。
- 对于键盘扫描仪 (System.in
),建议使用静态 Scanner 变量,例如:private static Scanner Scanner = new Scanner(System.in);
。
关于java - 需要验证我的计算器的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33599267/