3 Replies - 11366 Views - Last Post: 29 August 2009 - 09:25 AM Rate Topic: -----

#1 villalandron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 24-September 08

Charge Account validation

Posted 28 August 2009 - 06:35 PM

Hello! I have a problem completing this program. I have to create a class with a method that accepts a charge card account number as its argument. The method should determine whether the number is valid by comparing it with a list of valid charge card account numbers. The comparison should be accomplished using a search. If the account number to be checked is in the list of valid account numbers, the method should return true and if the number is not in the list, the method should return false.
The valid account numbers are stored in a file named valid_accounts.txt. A copy of this file should have accompanied this exam. You should create a method that will read the valid charge card account number list and return an array or arrayList object with the 7 digit integer values stored in the file (none of the numbers start with the digit zero).
The account numbers to be checked are stored in a file named accounts_to_check.txt. A copy of this file should have accompanied this exam. You should use the same method as above that will read the charge card account number list and return an array or arrayList object with the 7 digit integer values stored in the file (none of the numbers start with the digit zero).
You should also create a method that takes an array or arrayList object, arranges the charge card account numbers in ascending order and returns the sorted array or ArrayList object.
Demonstrate your class methods in a driver program by reading the charge card account numbers to be checked for validity from a file named accounts_to_check.txt, testing to see if each number that is in the file is valid, and displaying the result of that check in a report. Consider your output a report that someone could easily verify against a sorted valid accounts list. Output two additional reports: A sorted valid accounts list and a sorted accounts to check list.

I finished the program the way it is to do the basic concept. What I don't know to do is to have it check the text files instead of the numbers inside the program. Any help on this will be appreciated. Thank you!

import java.util.Scanner;

public class Driver {
	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 number is valid.");
		} else {
			System.out.println("That number is invalid.");
		}
	}
}



and

public class ChargeAccount {
	private static final int validChargeAccountNumbers[] = {5658845,4520125,7895122,8777541,8451277,1302850,
															8080152,4562555,5552012,5050552,7825877,1250255,
															1005231,6545231,3852085,7576651,7881200,4581002};
	
	// 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;
	}
}



I also included the text files!

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Charge Account validation

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,268
  • Joined: 27-December 08

Re: Charge Account validation

Posted 28 August 2009 - 06:51 PM

Just like you use the Scanner to read in from the console, you can use it to read in from files. The only difference is that the File path is in the Scanner's constructor instead of System.in. Let me demonstrate:

Scanner scan = new Scanner("valid_accounts.txt");
ArrayList<Integer> validAccounts = new ArrayList<Integer>();
while(scan.hasNextInt()) validAccounts.add(scan.nextInt());

Collections.sort(validAccounts); //you can also use your own algorithm to sort




Once you have read in the accounts to check in the same way, you can use the following method:

Collections.binarySearch(validAccounts, elmentFromAccountstoCheck);



Hope this helps some! If you run into any more trouble, check out the Collections class:

http://java.sun.com/...ollections.html

Good luck!
Was This Post Helpful? 0
  • +
  • -

#3 villalandron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 24-September 08

Re: Charge Account validation

Posted 29 August 2009 - 07:35 AM

So, does the Collections.binary search goes in driver.java as well?
Was This Post Helpful? 0
  • +
  • -

#4 villalandron  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 24-September 08

Re: Charge Account validation

Posted 29 August 2009 - 09:25 AM

I was going through my book on how to get the program to check information from a txt file and tried to mimic everything from the book but I get error messages. Here's what I got:

import java.io.*; // needed for file classes
import java.util.Scanner;

public class Driver
{
	public static void main(String args[]) 
		{
		Scanner keyboard = new Scanner(System.in);
		
		System.out.print("Please enter an account number: ");
		// get the number from the user
		int number = keyboard.nextInt();
		
		File file = new File(valid_accounts.txt);
		Scanner inputFile = new Scanner(valid_accounts.txt);
		// ask the user for an account number
		
		
		// check to see if the number is valid
		if ( valid_accounts.txt(number) == true ) 
			{
			System.out.println("That number is valid.");
			} 
		else {
			System.out.println("That number is invalid.");
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1