这是我创建的计算器和文件阅读器。但是当我输入我的程序的文件名来读取它时,它读取的文件是一个包含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");
}
}
}}
答案 0 :(得分:0)
我使用自己的文件文本测试您的代码,每行都有一个修复后操作,它没有异常(没有问题),如果您的操作输入有错误,您应该检查您的文件。
但是您的代码中有一些评论:
- 您应为每个来源使用一个扫描程序,例如System.in
或new File("name of file")
。
- 对于键盘扫描仪(System.in
),建议使用静态扫描仪变量,如:private static Scanner scanner = new Scanner(System.in);
。
答案 1 :(得分:0)
您的输入在其尝试读取的行中缺少一个有效输入。这意味着您的String [] fileinput
将不会在无法打印的行中包含3个元素。我冒昧地将你的输入分为3组:
44 3 +
9.99 + 0.09
12 0 *
. 10 -
10.2 2 *
12 4 /
66.1 0.12 -
.0 99.10 +
300 4.0 +
* 20 10
/ 10 20
5.2 + 1
2 & 100
139 - -
80 2 9
5 2 4
/ 3 3
A - 200.5
10 * 2
* 4 8
2 * 10
20 - 8
16 / -4
12 + +
4 2 x
y z
您可以通过最后一行清楚地看到有1个输入缺失。当您致电fileinput[2]
时会抛出异常。
答案 2 :(得分:0)
在这里,这将解决您的问题。
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");
}
switch (elements[2]) {
//validation for when the expressions are entered
case "-":
System.out.println("Result as follows:" + (number1 - number2));
break;
case "/":
System.out.println("Result as follows:" + (number1 / number2));
break;
case "*":
System.out.println("Result as follows:" + (number1 * number2));
break;
case "+":
System.out.println("Result as follows:" + (number1 + number2));
break;
}
} 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]);
switch (fileinput[2]) {
case "-":
System.out.println("Result as follows:" + (expression1 - expression2));
break;
case "/":
System.out.println("Result as follows:" + (expression1 / expression2));
break;
case "*":
System.out.println("Result as follows:" + (expression1 * expression2));
break;
case "+":
System.out.println("Result as follows:" + (expression1 + expression2));
break;
}
} catch (Exception e) {
System.out.println("Syntax error in the expression - \"" + line + "\". Proceeding to next expression.");
}
}
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");
}
}
}
}
如果有效,请将其标记为已接受。