473,474 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how can i incorporate my reader file to my program

69 New Member
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
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner; 
  2.  
  3.    public class ChargeAccount 
  4.    { 
  5.       static int[] validChargeAccountNumbers = 
  6.       { 
  7.          5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 8080152, 
  8.          4562555, 5552012, 5050552, 7825877, 1250255, 1005231, 6545231, 
  9.          3852085, 7576651, 7881200, 4581002 
  10.          }; 
  11.  
  12.       public static void main(String[] args) 
  13.       { 
  14.          Scanner in = new Scanner(System.in); 
  15.  
  16.       // Ask the user for an account number 
  17.          System.out.print("Please enter an account number: "); 
  18.  
  19.       // Get the number from the user 
  20.          int number = in.nextInt(); 
  21.  
  22.       // Check to see if the number is valid 
  23.          if (ChargeAccount.isValid(number) == true) 
  24.          { 
  25.             System.out.println("That account number is valid."); 
  26.          } 
  27.  
  28.          else 
  29.          { 
  30.             System.out.println("You did not enter a valid account number."); 
  31.          } 
  32.       } 
  33.  
  34.        // Check to see if an account number is valid by comparing it to the entries in the array of valid numbers 
  35.       public static boolean isValid(int number)       
  36.       { 
  37.  
  38.       // Perform sequential search through list of valid account numbers 
  39.          for (int i = 0; i < validChargeAccountNumbers.length; i++) 
  40.          { 
  41.  
  42.       // Check to see if the number we were given is at the ith position in the list 
  43.          if (validChargeAccountNumbers[i] == number) 
  44.          { 
  45.             return true; 
  46.          } 
  47.       } 
  48.  
  49.       // If we get down here, then we never found it in the list 
  50.              return false; 
  51.       } 
  52.    }
  53.  
file reader
Expand|Select|Wrap|Line Numbers
  1. mport java.io.*;
  2.  
  3.  
  4.    class FileReadTest 
  5.    {
  6.       public static void main (String[] args) 
  7.       {
  8.          FileReadTest f = new FileReadTest();
  9.          f.readMyFile();
  10.       }
  11.  
  12.       void readMyFile() 
  13.       {
  14.          DataInputStream dis = null;
  15.          String record = null;
  16.          int recCount = 0;
  17.  
  18.          try 
  19.          {
  20.             File f = new File("valid_accounts.txt");
  21.             FileInputStream fis = new FileInputStream(f);
  22.             BufferedInputStream bis = new BufferedInputStream(fis);
  23.             dis = new DataInputStream(bis);
  24.  
  25.             while ( (record=dis.readLine()) != null ) 
  26.             {
  27.                recCount++;
  28.                System.out.println(recCount + ": " + record);
  29.             }
  30.          } 
  31.  
  32.             // catch io errors from FileInputStream or readLine()
  33.             catch (IOException e) 
  34.             {
  35.                System.out.println("Uh oh, got an IOException error!" + e.getMessage());
  36.             } 
  37.  
  38.          finally 
  39.          {
  40.  
  41.           // if the file opened okay, make sure we close it
  42.             if (dis != null) 
  43.             {
  44.                try 
  45.                {
  46.                   dis.close();
  47.                } 
  48.                   catch (IOException ioe) 
  49.                   {
  50.                   }
  51.             }
  52.          }
  53.       }
  54.    }
  55.  
Sep 13 '10
58 6318
blknmld69
69 New Member
ok let me try that
Sep 15 '10 #51
blknmld69
69 New Member
lol, now it gives this error:

ChargeAccount.java:17: unreported exception java.lang.Exception; must be caught or declared to be thrown
readMyFile("valid_accounts.txt");
^
1 error
Sep 15 '10 #52
blknmld69
69 New Member
you there?
Sep 15 '10 #53
blknmld69
69 New Member
ok I have some errands to run. Will be back on later. Hopefully this can give you an understanding of what im trying to do.

Orginal Program
Expand|Select|Wrap|Line Numbers
  1.  import java.util.Scanner; 
  2.  
  3.    public class ChargeAccount1 
  4.    { 
  5.       static int[] validChargeAccountNumbers = 
  6.       { 
  7.          5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 8080152, 
  8.          4562555, 5552012, 5050552, 7825877, 1250255, 1005231, 6545231, 
  9.          3852085, 7576651, 7881200, 4581002 
  10.          }; 
  11.       public static void main(String[] args) 
  12.       { 
  13.          Scanner in = new Scanner(System.in); 
  14.  
  15.       // Ask the user for an account number 
  16.          System.out.print("Please enter an account number: "); 
  17.  
  18.        // Get the number from the user 
  19.          int number = in.nextInt(); 
  20.  
  21.       // Check to see if the number is valid 
  22.          if (ChargeAccount1.isValid(number) == true) 
  23.          { 
  24.             System.out.println("That account number is valid."); 
  25.          } 
  26.  
  27.          else 
  28.          { 
  29.             System.out.println("You did not enter a valid account number."); 
  30.          } 
  31.       } 
  32.  
  33.    // Check to see if an account number is valid by comparing it to the entries in the array of valid numbers 
  34.       public static boolean isValid(int number) 
  35.       { 
  36.  
  37.       // Perform sequential search through list of valid account numbers 
  38.          for (int i = 0; i < validChargeAccountNumbers.length; i++) 
  39.          { 
  40.  
  41.          // Check to see if the number we were given is at the ith position in the list 
  42.             if (validChargeAccountNumbers[i] == number) 
  43.             { 
  44.                return true; 
  45.             } 
  46.          } 
  47.         return false; 
  48.       } 
  49.    } 
  50.  
This is my assignment:

Modify the charge account validation class that is above so it reads the list of valid charge account numbers from a file(valid_accounts.txt)
Sep 15 '10 #54
Oralloy
988 Recognized Expert Contributor
Glad to help.

Good luck with the rest of your class.
Sep 15 '10 #55
blknmld69
69 New Member
Finally got it to work Oralloy. Just wanted to let you know I appreciate all of your help. I do have another project that is due I will post tomorrow and hopefully you can help me with that.

Thanks a million!!!!!!!!!!!!
Sep 16 '10 #56
Oralloy
988 Recognized Expert Contributor
No worries. We'll do our best to help you learn this stuff. That means that we won't write the answers for you, but we're going to push you towards them, give hints, and generally try to guide your learning.

Can you give us a hint about what your main hang-ups are? Is it lack of study time? Lack of background? Just not understanding how to read manuals? Something else? We may be able to give you some pointers to help you past your hurdle.

BTW, Do you know how to get the Java class library manual on-line?

It's available here. Please add it to your browser's favorites list.

Lastly, are you taking a Computer Science or Information Technology degree, or something else?

Cheers!
Sep 16 '10 #57
blknmld69
69 New Member
Well for starters the class is only offered as a web class.(not displined) My degree is computer programming specialist. Go figure right. I picked up Visual basics and Unix/linux pretty good. Its just this Java thats giving me hangups. Added online manuels to my favorites.
Class ends on monday! :)
Sep 16 '10 #58
Oralloy
988 Recognized Expert Contributor
Good luck, Bro.

I'm in the office tomorrow until late afternoon, then gone until Monday morning; so if you need help, ask early.
Sep 16 '10 #59

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Thomas Matthews | last post by:
Is there anything technically wrong with the code below? I compiled it with g++ 3.3.1 and it executes with no errors. I compiled it with bcc32 5.6 and it executes with "This program has performed...
0
by: cherry | last post by:
Dear All, I have written a program in sharepoint, in which will call a Web Service of another Sharepoint portal server so that documents from sharepoint portal server A can push document to...
5
by: ComicCaper | last post by:
Hi all, I use a quiz program that accepts a text file for questions and answers in this format: Question Answer1 <----is the correct answer. Quiz randomizes answers. Answer2 Answer3...
1
by: Glenn Graham | last post by:
My web download program stopped working when IE7 beta 1-3 was installed. Error when calling OpenRequest in CHttpConnection. The Error is Created by ASSERT(hFile != NULL) in mehtod...
0
by: jisha | last post by:
i want to get code for a program in visual c++ where : 1]take the name of directory say DIR 2]get a file ,say IN from that directory which has 3 column (say X, Y and Z) and many rows( display this...
4
by: gdarian216 | last post by:
I am doing a multi file program and I got it to work correctly in a single file but when I split it up it isn't working properly. I keep getting errors when i compile. Can anyone help me figure out...
1
by: gdarian216 | last post by:
I am writing a multifile program and it worked when it was all in one file. I have gotten all of the errors out of the program except when I go to output the change the quarters are right but the...
1
by: sadanand | last post by:
Hi, I am a beginer to perl progmig. When I run the following program.. use File::Find; use File::Stat; use Time::Local;
2
by: rajak20pradeep | last post by:
Hello, i cannot solve this program. Q. Write a program to find and count article(a.an ,the) in a text file .
1
by: sunny | last post by:
Hi all We have a pdf user manual which we open when client clicks on help button. some of our clients have the issue that it does not open the file and it says file not found. checked with...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.