My program compiles and runs but its not outputting correctly. The program should check the file valid_accounts.txt and compare it to the users input. If the account is found int the valid_accounts.txt the output should display "That account number is valid.". I get "You did not enter a valid account number." whether the input is correct or not. Any help would be greatly appreciated.

import java.io.BufferedReader; 
   import java.io.FileReader; 
   import java.io.IOException; 
   import java.util.Scanner; 
   import java.util.Vector;
	 

   public class ChargeAccount 
   { 
      static Vector<Integer> validChargeAccountNumbers = new Vector<Integer>(); 
   
   
      public static void main(String[] args) 
      { 
      //load the file
         readMyFile("valid_accounts.txt"); 
      
         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 (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 vector validChargeAccountNumbers 
      public static boolean isValid(int number) 
      { 
         return validChargeAccountNumbers.contains(number); 
      } 
   
      public static void readMyFile(String nameFile) 
      { 
         String record = null; 
         BufferedReader reader = null; 
         FileReader fr = null; 
         int recCount = 0; 
			
      // Code to read the file and store each account into the vector 
		// validChargeAccountNumbers 
      
      } 
   }

Recommended Answers

All 14 Replies

and this surprises you?
you do realize, you're never actually reading the data that is stored in the valid_accounts.txt file, do you?

There is nothing in the readMyFile method.

There is nothing in the readMyFile method.

This is my readMyFile:

import java.io.*;
   import java.util.Vector;

   public class readMyFile 
   {
      public static void main(String[] args) 
      {
         DataInputStream dis = null;
         String record = null;
         int recCount = 0;
         Vector<String> vAccounts = new Vector<String>();
      
         try 
         {
            File f = new File("valid_accounts.txt");
            FileInputStream fis = new FileInputStream(f);
            BufferedInputStream bis = new BufferedInputStream(fis);
            dis = new DataInputStream(bis);
         
            while ( (record=dis.readLine()) != null ) 
            {
               vAccounts.add(record);
               recCount++;
               System.out.println(recCount + ": " + record);
            }
         } 
         
         // catch io errors from FileInputStream or readLine()
            catch (IOException e) 
            {
               System.out.println("IOException error!" + e.getMessage());
            } 
         
         finally 
         {
         
         // if the file opened okay, make sure we close it
            if (dis != null) 
            {
               try 
               {
                  dis.close();
               } 
                  catch (IOException ioe) 
                  {
               }
            }
         }
      }
   }

Does it work? Are you getting all the records from the file into the vAccounts Vector?

yah, but the readMyFile in the previous code you've shown us, does not read your file, and it is impossible for it to use this readMyFile class, so whether this class works or not, will not change anything about the original program

The previous code (ChargeAccount) uses the readMyFile to read the valid account numbers in the valid_accounts.txt. They both compile but my concern is that I get the invalid account output even when I put in the valid account. HELP!

>>The previous code (ChargeAccount) uses the readMyFile to read the valid account numbers in the valid_accounts.txt.

Uhm, If you can read, we told you no it does not.
It seems you cannot distinguish between classes and methods.

You guys are very sarcastic! You would think this is a place to help and not sarcasm. I am new to this Java stuff!

You guys are very sarcastic! You would think this is a place to help and not sarcasm. I am new to this Java stuff!

Are you kidding me? First of all, Nobody was even remotely sarcastic. Secondly, If you had actually read your replies carefully, you would realise where your problem was. I simply stated(correctly), that you must not be able to read, if you still think: >>The previous code (ChargeAccount) uses the readMyFile to read the valid account numbers in the valid_accounts.txt.- statement is true after all the replies.

One of the OPs problems is the ambiguity of using the same name for two different things:

public static void readMyFile(String nameFile)
public class readMyFile

Java has a naming convention that would help prevent this. The first letter of a class name should begin with an uppercase letter.

Um I can read. I just dont understand Java like you Junior Poster in training. And that still did help me solve my problem!!!!!!!!!!!

One of the OPs problems is the ambiguity of using the same name for two different things:

public static void readMyFile(String nameFile)
public class readMyFile

Java has a naming convention that would help prevent this. The first letter of a class name should begin with an uppercase letter.

I am certain he thinks they are the same thing or that calling the function 'calls' the class.

@blknmld69, so what changes did you make? Have you made the readMyFile method actually useful?
Also, I don't know what you are implying with
>>I just dont understand Java like you Junior Poster in training
But my alias is Akill10.

@OP You need to change your code to be a method that reads in data from the file and return the data it has read in some structure like the Vector you are using.

@OP You need to change your code to be a method that reads in data from the file and return the data it has read in some structure like the Vector you are using.

what he means to say is: you need to put the functionality that you've put in the main method of your readMyFile class, into your readMyFile method in your other class.

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.