how can i incorporate my reader file to my program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blknmld69
    New Member
    • Feb 2010
    • 69

    how can i incorporate my reader file to my program

    My program works reading from values that I have stored in an array for valid account numbers. I also have a file that reads the valid account numbers from a text file. I need to read from the text file and not the array.

    reads from array
    Code:
    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; 
          } 
       }
    file reader
    Code:
    mport java.io.*;
    
    	
       class FileReadTest 
       {
          public static void main (String[] args) 
          {
             FileReadTest f = new FileReadTest();
             f.readMyFile();
          }
       
          void readMyFile() 
          {
             DataInputStream dis = null;
             String record = null;
             int recCount = 0;
          
             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 ) 
                {
                   recCount++;
                   System.out.println(recCount + ": " + record);
                }
             } 
    			
    			// catch io errors from FileInputStream or readLine()
                catch (IOException e) 
                {
                   System.out.println("Uh oh, got an IOException error!" + e.getMessage());
                } 
             
             finally 
             {
             
              // if the file opened okay, make sure we close it
                if (dis != null) 
                {
                   try 
                   {
                      dis.close();
                   } 
                      catch (IOException ioe) 
                      {
                      }
                }
             }
          }
       }
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    Try building a method similar to ReadMyFile(), which returns a collection of valid accounts. The type of collection would, of course, depend on how you are planning to use the account numbers.

    Code:
    import java.util.Set;
    import java.util.HashSet;
    
    public Set<Integer> ReadMyFile
    {
      HashSet<Integer> result = new HashSet<Integer>;
    
      ...
    
      while( !eof )
      {
        ...
        result.add(new Integer(validAccount));
      }
    
      return result;
    }
    Hopefully this helps a little.

    Comment

    • blknmld69
      New Member
      • Feb 2010
      • 69

      #3
      I just want it to basically check from the validAccounts.t xt file to see if the users input is a valid account.

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        Do you want to reload the file every time for the check, or just read the thing once?

        Right now all you have are miscellaneous code bits that sort of solve your problem.

        I take it that your assignment is to write a one-shot test to see if the account number is valid?

        If you don't like the approach of returning a collection, then think procedurally.

        You might use your current file read routine to test while reading - if no match is found by end-of file, then there is no match. (BTW, I notice that you are reading lines, not integer values. Were you planning on doing a comparison of strings or integers?)

        You might hoist the file-read code to your isValid() method. Or even into the main routine.

        If you need help converting text strings into integers, look up the documentation on Integer.decode( String).

        Luck!

        Comment

        • blknmld69
          New Member
          • Feb 2010
          • 69

          #5
          I have a two part assignment.
          Code:
          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; 
                } 
             }
          this works great. Now the second part is to read the valid accounts stored in the array above from a text file(valid_acco unts.txt)

          Comment

          • Oralloy
            Recognized Expert Contributor
            • Jun 2010
            • 988

            #6
            Well, you've made a good start at your reader code. Don't throw that away.

            You already know that you need to replace the static instantiation of the account number array.

            First, have you got a strategy? Will you look at the file until found, or will you read the entire file and then see if the value is there? This determines how you will write your program.

            Second, have you looked at using any of the java.util collection classes to manage the account numbers? Or hasn't your instructor introduced you to template classes, yet?

            Third, if you are lost, try pseudo-coding your answer - solve the problem with words first. Then you can write Java code to implement your solution.

            Lastly, has your instructor given you any out-of-band information, like maximum file size, or some such? This is important, because the kinds of considerations in production software are a little tougher than are usually applied to students' assignments.

            Cheers!

            Comment

            • blknmld69
              New Member
              • Feb 2010
              • 69

              #7
              Ok i have it reading from the text file now but it is only outputing "the account you entered is invalid even when I input the correct account number.

              Code:
              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 
                    
                    } 
                 }

              Comment

              • Oralloy
                Recognized Expert Contributor
                • Jun 2010
                • 988

                #8
                Excellent.

                So now all you have to do is read from the file into your Vector, right?

                The code is essentially the same as that you had earlier.

                You're using a Scanner to read from the System.in stream, right?

                What's wrong with using a Scanner to read from your account numbers file?

                The only trick is detecting EOF, right? Scanner.nextInt () throws a java.util.NoSuc hElementExcepti on exception on end-of-input. The page is here.

                So the control code will look something like:
                Code:
                try
                {
                  while(true)
                  {
                    value = myScanner.nextInt();
                    validChargeAccountNumbers.add(value);
                  }
                }
                catch (NoSuchElementException exception)
                {
                  // EOF is legitimate; all others will pass up the stack
                }
                Do you think you can do the rest?

                Comment

                • blknmld69
                  New Member
                  • Feb 2010
                  • 69

                  #9
                  actually no, I am late with this project. I have been working on it and have fallen behind in my other classes.

                  Comment

                  • blknmld69
                    New Member
                    • Feb 2010
                    • 69

                    #10
                    you mean something like this:

                    Code:
                       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) 
                                      {
                                   }
                                }
                             }
                          }
                       }

                    Comment

                    • Oralloy
                      Recognized Expert Contributor
                      • Jun 2010
                      • 988

                      #11
                      Ouch. Then you are one frustrated kid.

                      Still, your code looks good. All you need is the read-file bit and you're done.

                      Did you ever bother to read that link I sent?

                      This is a simplified bit of code that might work for you, but it'll never fly in a production environment where people seem to put all kinds of junk in files. The problem is, of course, in error detection and reporting:
                      Code:
                      try {
                        Scanner s = new Scanner(new File(fileName));
                          	
                        while(s.hasNextInt())
                          validChargeAccountNumbers.add(s.nextInt());
                      } catch (FileNotFoundException e) {
                        e.printStackTrace();
                      }

                      Comment

                      • blknmld69
                        New Member
                        • Feb 2010
                        • 69

                        #12
                        lol very confused old man (44). I will try that.

                        Comment

                        • Oralloy
                          Recognized Expert Contributor
                          • Jun 2010
                          • 988

                          #13
                          Eeeep, that makes me ancient (48).

                          Comment

                          • blknmld69
                            New Member
                            • Feb 2010
                            • 69

                            #14
                            where would that go?

                            Comment

                            • Oralloy
                              Recognized Expert Contributor
                              • Jun 2010
                              • 988

                              #15
                              Do you understand what the code example does?

                              Comment

                              Working...