Charge Account Modification

how can I get my array from a text file?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

43 Replies - 6239 Views - Last Post: 13 September 2010 - 07:37 PM Rate Topic: -----

#1 blknmld69  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 102
  • Joined: 28-November 09

Charge Account Modification

Posted 09 September 2010 - 08:14 AM

How can I modify the validation class so it reads the list of valid charge account numbers from a text file (valid_accounts) that i have save in notepad?

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; 
      } 
   }

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Charge Account Modification

#2 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Charge Account Modification

Posted 09 September 2010 - 08:31 AM

You can use a BufferReader in order to read each linea in the file:
String ln;
try
{
   BufferedReader inFile = new BufferedReader(new FileReader(file));
   while ((ln = inFile.readLine()) != null){
          //your code here. Save each account in an array or vector
   }

}finally{
   if (inFile != null)
   {
	try
	{
   	 inFile.close();
	}
	catch (Exception e)
	{
           
	}
   }
}


This post has been edited by guidojavier: 09 September 2010 - 08:31 AM

Was This Post Helpful? 0
  • +
  • -

#3 blknmld69  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 102
  • Joined: 28-November 09

Re: Charge Account Modification

Posted 09 September 2010 - 09:43 AM

Ok Thanks will work on that today!
Was This Post Helpful? 0
  • +
  • -

#4 blknmld69  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 102
  • Joined: 28-November 09

Re: Charge Account Modification

Posted 13 September 2010 - 08:24 AM

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

reads from array
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
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) 
                  {
                  }
            }
         }
      }
   }


Was This Post Helpful? 0
  • +
  • -

#5 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Charge Account Modification

Posted 13 September 2010 - 08:41 AM

Use the readMyFile method for to read each account in the file like you are doing it in the while loop
 while ( (record=dis.readLine()) != null ) 


Store each account in a Vector object. Then when the user enter an account you can validate if the account exists using the get method from the Vector object:

vAccount.get(inputAccount);

If the the get method return null the account does not exist. Otherwise it will return the String object which represents the account.

I made a mistake about the get method it is intended just for get objects by index. The method you need to use is 'contains'. So your ChargeAccount class it would be:
import java.util.Scanner; 
import java.util.Vector;

   public class ChargeAccount 
   { 
     
      static Vector<String> vAccounts = new Vector<String>();
      
      public static void main(String[] args) 
      { 
    	  
    	  FileReadTest fr = new FileReadTest();
    	  //load all the accounts
    	  vAccounts.addAll(fr.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 (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)       
      { 
          String account = Integer.toString(number);
          
    	  return vAccounts.contains(account);
      } 
   }



And the readMyFile method would be:
      public Vector readMyFile(String nameFile) 
      {
         DataInputStream dis = null;
         String record = null;
         int recCount = 0;
         Vector vAccounts = new Vector
         try 
         {
            File f = new File(nameFile);
            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("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) 
                  {
                  }
            }
         }

         return vAccounts ;
      }


This post has been edited by guidojavier: 13 September 2010 - 09:07 AM

Was This Post Helpful? 0
  • +
  • -

#6 blknmld69  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 102
  • Joined: 28-November 09

Re: Charge Account Modification

Posted 13 September 2010 - 09:39 AM

so basically i would just compile the readMyFile then run the program?
Was This Post Helpful? 0
  • +
  • -

#7 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Charge Account Modification

Posted 13 September 2010 - 09:47 AM

You should change both class files as the examples I gave you above. Then run the app and test it.
Was This Post Helpful? 0
  • +
  • -

#8 blknmld69  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 102
  • Joined: 28-November 09

Re: Charge Account Modification

Posted 13 September 2010 - 09:58 AM

ok i did that and got these errors for the read me file:

jGRASP exec: javac -g readMyFile.java

readMyFile.java:7: class, interface, or enum expected
public Vector readMyFile(String nameFile)
^
readMyFile.java:10: class, interface, or enum expected
String record = null;
^
readMyFile.java:11: class, interface, or enum expected
int recCount = 0;
^
readMyFile.java:12: class, interface, or enum expected
Vector vAccounts = new Vector;
^
readMyFile.java:13: class, interface, or enum expected
try
^
readMyFile.java:16: class, interface, or enum expected
FileInputStream fis = new FileInputStream(f);
^
readMyFile.java:17: class, interface, or enum expected
BufferedInputStream bis = new BufferedInputStream(fis);
^
readMyFile.java:18: class, interface, or enum expected
dis = new DataInputStream(bis);
^
readMyFile.java:20: class, interface, or enum expected
while ( (record=dis.readLine()) != null )
^
readMyFile.java:23: class, interface, or enum expected
recCount++;
^
readMyFile.java:24: class, interface, or enum expected
System.out.println(recCount + ": " + record);
^
readMyFile.java:25: class, interface, or enum expected
}
^
readMyFile.java:32: class, interface, or enum expected
}
^
readMyFile.java:43: class, interface, or enum expected
}
^
readMyFile.java:51: class, interface, or enum expected
}
Was This Post Helpful? 0
  • +
  • -

#9 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Charge Account Modification

Posted 13 September 2010 - 10:07 AM

Why do you have readMyFile.java ?. If the name of your class is FileReadTest your class file must be FileReadTest.java. You should have:
import java.io.*;

	
class FileReadTest 
{
     
    public Vector readMyFile(String nameFile) 
    {
      DataInputStream dis = null;
      String record = null;
      int recCount = 0;
      Vector vAccounts = new Vector
      try 
      {
         File f = new File(nameFile);
         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("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) 
               {
               }
         }
      }

      return vAccounts ;
   }

}


This post has been edited by guidojavier: 13 September 2010 - 10:10 AM

Was This Post Helpful? 0
  • +
  • -

#10 blknmld69  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 102
  • Joined: 28-November 09

Re: Charge Account Modification

Posted 13 September 2010 - 10:23 AM

lol, ok did that and got this

--jGRASP exec: javac -g readMyFile.java

readMyFile.java:13: '(' or '[' expected
try
^
readMyFile.java:29: 'catch' without 'try'
catch (IOException e)
^
readMyFile.java:29: ')' expected
catch (IOException e)
^
readMyFile.java:29: not a statement
catch (IOException e)
^
readMyFile.java:29: ';' expected
catch (IOException e)
^
readMyFile.java:34: 'finally' without 'try'
finally
^
6 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Was This Post Helpful? 0
  • +
  • -

#11 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Charge Account Modification

Posted 13 September 2010 - 11:02 AM

I still can see that the name of your file is readMyFile.java:
--jGRASP exec: javac -g readMyFile.java


Please post the code that you have in that file.

This post has been edited by guidojavier: 13 September 2010 - 11:04 AM

Was This Post Helpful? 0
  • +
  • -

#12 blknmld69  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 102
  • Joined: 28-November 09

Re: Charge Account Modification

Posted 13 September 2010 - 11:22 AM

im illiterate when it comes to this Java language. The file that I am trying to read from is valid_accounts.txt which i have attached. I am thinking that the readMyFile is what I need to read the txt file.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#13 blknmld69  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 102
  • Joined: 28-November 09

Re: Charge Account Modification

Posted 13 September 2010 - 11:28 AM

import java.io.*;

class FileReadTest 
{
     
    public Vector readMyFile(String nameFile) 
    {
      DataInputStream dis = null;
      String record = null;
      int recCount = 0;
      Vector vAccounts = new Vector
      try 
      {
         File f = new File(nameFile);
         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("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) 
               {
               }
         }
      }

      return vAccounts ;
   }

}


Was This Post Helpful? 0
  • +
  • -

#14 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Charge Account Modification

Posted 13 September 2010 - 12:22 PM

Ok there is a mistake here in line 11. It must be:
Vector vAccounts = new Vector();


This post has been edited by guidojavier: 13 September 2010 - 12:22 PM

Was This Post Helpful? 0
  • +
  • -

#15 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,020
  • Joined: 15-July 08

Re: Charge Account Modification

Posted 13 September 2010 - 03:56 PM

You have a typo:
Vector vAccounts = new Vector



In Java 1.5+ you also need Generics to work well:

Vector<String> vAccounts = new Vector<String>();



This will make the Vector so it only accepts Strings
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3