Good day everyone!
I have a program that I need to modify so that it reads a list of valid charge account numbers from a file. Currently the list is in an array in the program. I am also attaching the text file(valid_accounts.txt) that it needs to read from.

import java.util.Scanner; 

   public class ChargeAccount 
   { 
      static int[] validChargeAccountNumbers = 
      { 
         5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 8080152, 
         4562555, 5552012, 5050552, 7825877, 1250255, 1005231, 6545231, 
         3852085, 7576651, 7881200, 4581002 
         }; 
   
      public static void main(String[] args) 
      { 
         Scanner in = new Scanner(System.in); 
      
      // Ask the user for an account number 
         System.out.print("Please enter an account number: "); 
      
      // Get the number from the user 
         int number = in.nextInt(); 
      
      // Check to see if the number is valid 
         if (ChargeAccount.isValid(number) == true) 
         { 
            System.out.println("That account number is valid."); 
         } 
         
         else 
         { 
            System.out.println("You did not enter a valid account number."); 
         } 
      } 
   
   	// Check to see if an account number is valid by comparing it to the entries in the array of valid numbers 
      public static boolean isValid(int number)       
      { 
      
      // Perform sequential search through list of valid account numbers 
         for (int i = 0; i < validChargeAccountNumbers.length; i++) 
         { 
         
      // Check to see if the number we were given is at the ith position in the list 
         if (validChargeAccountNumbers[i] == number) 
         { 
            return true; 
         } 
      } 
      
      // If we get down here, then we never found it in the list 
         	return false; 
      } 
   }

Recommended Answers

All 4 Replies

Whats the problem?

I need to modify the program so that it reads the valid charge account numbers from a text file that I have attached and not from the static integers in the program.

Did you try searching 'file-handling in java' or something similar in google?

Member Avatar for coil

Check out this link. From the Java website.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.