Open File Method to Validate Account Numbers

Trying to write a method that will open a file and create an array to

Page 1 of 1

2 Replies - 1648 Views - Last Post: 26 January 2010 - 03:55 PM Rate Topic: -----

#1 newFrau  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-January 10

Open File Method to Validate Account Numbers

Post icon  Posted 26 January 2010 - 03:17 PM

Hi,
I'm trying to modify a homework problem. The first problem was to create an array and compare the user input to the array. Now we're supposed to compare the user input to data stored in a file by writing a method to open the file. I tried writing the method, but I'm terribly confused about calling it in main.
In the first program I just parsed the user string input as an int. Now in main I don't know how to pass the array to the isValid method or if I'm opening the file properly.
Sorry for the long explanation. Trying to explain everything I've done. I'm new to Java and am having trouble connecting the dots. My textbook is not good. Thanks!

package chargeaccountmod;
import javax.swing.JOptionPane;
import java.io.*;
import java.util.Scanner;

public class ChargeAccountMod {
//validates whether user entered account numbers are valid compared to file

   private final int SIZE = 5;
   private int[] validNums = new int[SIZE];


   //method to open file

   public void openFile() throws IOException
   {
	  int index = 0;
	   File file = new File("C:/Documents/ValidAcctNums.txt");
	   Scanner inputFile = new Scanner(file);
	   while (inputFile.hasNext() && index < validNums.length)
	   {
		   validNums[index] = inputFile.nextInt();
		   index++;
	   }
	   inputFile.close();
   }

	//method to check whether numbers are valid
	public boolean isValid(int number)
	{
		boolean found = false;
		int index = 0;

		while(!found && index < validNums.length)
		{
			if (validNums[index] == number)
				found = true;
			else
				index++;
		}

		return found;
	}

	public static void main(String[] args) throws IOException {
		// test
		
		int[] acctNum;

		ChargeAccountMod valid = new ChargeAccountMod();

valid.openFile();
		if (valid.isValid(acctNum))
			JOptionPane.showMessageDialog(null, "That's a valid account number.");
		else
			JOptionPane.showMessageDialog(null, "That's an invalid account number.");

		System.exit(0);

	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Open File Method to Validate Account Numbers

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Open File Method to Validate Account Numbers

Posted 26 January 2010 - 03:45 PM

int[] acctNum;


In your isValid method, you have
isValid(int number)
Meaning, it will only accept a single integer, rather than an array.

If you wanted it to accept an array, you could do this
public boolean isValid(int[] number)


If you want to pass a specific element of your array acctNum to the method, you can do this:
valid.isValid(acctNum[0])) //replace 0 with whatever element you want, or, use a loop


Was This Post Helpful? 0
  • +
  • -

#3 0x00hex  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 11-January 10

Re: Open File Method to Validate Account Numbers

Posted 26 January 2010 - 03:55 PM

valid.openFile();

This part of the code should be surrounded by a try catch block since it can throw an exception.

try {
valid.openFile(); 
} catch(IOException e) { System.out.printf("File Not Found"); }



That will deal with the opening of the file properly. Also I might suggest that in the open() you change the file path from "C:/Documents/ValidAcctNums.txt" to "ValidAcctNums.txt" and then move ValidAcctNums.txt into the working directory of the Java program.

You shouldn't need to pass an array into the isValid() method because the scope of validNums array main means it can be accessed by any method within the class.

Hope this helped.

P.S: If you want a good text book Java Foundations by john lewis, Pete J DePasquale and Joseph Chase is good.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1