我有这个程序,我必须检查用户输入的值是否存在于我在源文件中创建的文本文件中。但是,每当我尝试使用IOException调用该方法时,它都会抛出一个错误。请帮我谢谢。
import java.util.*;
import java.io.*;
public class chargeAccountModi
{
public boolean sequentialSearch ( double chargeNumber ) throws IOException
{
Scanner keyboard= new Scanner(System.in);
int index = 0;
int element = -1;
boolean found = false;
System.out.println(" Enter the Charge Account Number : " );
chargeNumber = keyboard.nextInt();
int[] tests = new int[18];
int i = 0;
File file = new File ("Names.txt");
Scanner inputFile = new Scanner(file);
while(inputFile.hasNext() && i < tests.length )
{
tests [i] = inputFile.nextInt();
i++;
}
inputFile.close();
for ( index = 0 ; index < tests.length ; index ++ )
{
if ( tests[index] == chargeNumber )
{
found = true;
element = index;
}
}
return found;
}
public static void main(String[]Args)
{
double chargeNumber = 0;
chargeAccountModi object1 = new chargeAccountModi();
try
{
object1.sequentialSearch(chargeNumber);
}
catch (IOException ioe)
{
}
System.out.println(" The search result is : " + object1.sequentialSearch (chargeNumber));
}
}
答案 0 :(得分:1)
查看你的方法sequentialSearch之后,每个方面都可以。但是尝试改变主要:
但请记住,在Names.txt文件中,您应该只有数字,因为您使用scanner.nextInt();
,因此应该只有数字或方法将抛出exeption InputMismatchException。
检查Names.txt文件的路径,你应该在classpath上拥有它,因为你在代码File file = new File ("Names.txt");
中使用相对路径Names.txt应该在同一个文件夹中。
public static void main(String[]Args)
{
double chargeNumber = 0;
chargeAccountModi object1 = new chargeAccountModi();
try
{
System.out.println(" The search result is : " + object1.sequentialSearch(chargeNumber));
}
catch (IOException ioe)
{
System.out.println("Exception!!!");
ioe.printStackTrace();
}
}
答案 1 :(得分:0)
几点提示和建议:
首先:不要忘记关闭扫描仪对象! (你留下一个未公开的)
第二个:你的主要方法非常低效,你使用两个循环,在你读取和存储变量的第一个循环中,在你检查匹配的第二个循环中,你可以同时做两个(I为你写了另一种方法)
第三件小事:代码中根本没有使用变量“element”,我在答案中删除了它。
最后但并非最不重要:文件“Names.txt”需要在项目的根文件夹中找到(因为你只是指定了它的名字),因为你提到了IOException,我认为这是错的。该应用程序。如果您的项目名为Accounts,那么它是一个名为Accounts的文件夹,包含它的源代码和项目的其他内容,请确保文件“Accounts / Names.txt”存在!并且它是所需的格式。
import java.util.*;
import java.io.*;
public class ChargeAccountModi {
public boolean sequentialSearch(double chargeNumber) throws IOException {
Scanner keyboard= new Scanner(System.in);
int index = 0;
boolean found = false;
System.out.print("Enter the Charge Account Number: " );
chargeNumber = keyboard.nextInt();
int[] tests = new int[18];
int i = 0;
File file = new File ("Names.txt");
Scanner inputFile = new Scanner(file);
while(inputFile.hasNext() && i < tests.length ) {
tests [i] = inputFile.nextInt();
i++;
}
inputFile.close();
for (index = 0 ; index < tests.length ; index ++ ) {
if (tests[index] == chargeNumber) {
found = true;
}
}
keyboard.close();
return found;
}
public boolean sequentialSearchAlternative(double chargeNumber) throws IOException {
Scanner keyboard= new Scanner(System.in);
boolean found = false;
System.out.print("Enter the Charge Account Number: " );
chargeNumber = keyboard.nextInt();
int tests = 18;
int i = 0;
File file = new File ("Names.txt");
Scanner inputFile = new Scanner(file);
while(inputFile.hasNext() && i<tests) {
if (inputFile.nextInt() == chargeNumber) {
found = true;
break;
}
i++;
}
inputFile.close();
keyboard.close();
return found;
}
public static void main(String[] args) {
double chargeNumber = 0;
ChargeAccountModi object1 = new ChargeAccountModi();
try {
System.out.println("The search result is : " + object1.sequentialSearch(chargeNumber));
} catch (Exception e) {
//Handle the exceptions here
//The most likely exceptions are:
//java.io.FileNotFoundException: Names.txt - Cannot find the file
//java.util.InputMismatchException - If you type something other than a number
e.printStackTrace();
}
}
}