문제

I had this program with which i have to check if the value the user entered is present in the text file i created in the source file. However it throws me an error everytime i try to call the method with IOException. Please help me out thanks.

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

    }
}
도움이 되었습니까?

해결책

After looking on your method sequentialSearch there is everythink ok. But try to change main: But remember that in your Names.txt file you should have only numbers because you use scanner.nextInt();, so there should be only numbers or method will throw exeption InputMismatchException.

Check also path to Names.txt file you should have it on classpath because you use relative path in code File file = new File ("Names.txt"); Names.txt should be in the same folder.

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

}

다른 팁

Few tips and suggestions:

First of all: Don't forget to close the Scanner objects! (you left one unclosed)

Second of all: Your main method is highly inefficient, you are using two loops, in the first one you read and store variables, in the second one you check for a match, you can do both at the same time (I've written an alternative method for you)

Third little thing: The variable "element" is not used at all in your code, I've removed it in the answer.

And last but not least: The file "Names.txt" needs to be located (since you've only specificied it's name) in the root folder of your project, since you mention an IOException, I figure that's what's wrong with the app. If your project is called Accounts, then it's a folder called Accounts with it's source and whatever else is part of your project, make sure the file "Accounts/Names.txt" exists! and that it is in the desired format.

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();
        }
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top