473,597 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ 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 6342
Oralloy
988 Recognized Expert Contributor
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:
Expand|Select|Wrap|Line Numbers
  1. try {
  2.   Scanner s = new Scanner(new File(fileName));
  3.  
  4.   while(s.hasNextInt())
  5.     validChargeAccountNumbers.add(s.nextInt());
  6. } catch (FileNotFoundException e) {
  7.   e.printStackTrace();
  8. }
Sep 15 '10 #11
blknmld69
69 New Member
lol very confused old man (44). I will try that.
Sep 15 '10 #12
Oralloy
988 Recognized Expert Contributor
Eeeep, that makes me ancient (48).
Sep 15 '10 #13
blknmld69
69 New Member
where would that go?
Sep 15 '10 #14
Oralloy
988 Recognized Expert Contributor
Do you understand what the code example does?
Sep 15 '10 #15
blknmld69
69 New Member
it looks like it reading from the file validChargeAcco untNumber
Sep 15 '10 #16
blknmld69
69 New Member
ok so it should be in the readMyFile method?
Sep 15 '10 #17
Oralloy
988 Recognized Expert Contributor
Yep.

So that's probably the core of your method "ReadMyFile ()".

What is important is how you incorporate it.

Since you're a student, I assume that you don't have any strong idea of exception handling, and how to use it. That's why the example (chicken-sh*t) exception handler.

At some level you'll have to deal with I/O errors in your program. You might just be able to declare all your methods as "throws Exception", and that's it. I really don't know what your instructor's exception handling requirements are.

Hopefully you can test and you'll be done in a couple minutes. Your post #7 looks pretty good, otherwise.
Sep 15 '10 #18
blknmld69
69 New Member
i get the error when i do it like that:
ChargeAccount.j ava:54: cannot find symbol
symbol : class File
location: class ChargeAccount
Scanner s = new Scanner(new File());
^
ChargeAccount.j ava:58: cannot find symbol
symbol : class FileNotFoundExc eption
location: class ChargeAccount
catch (FileNotFoundEx ception e)
^
2 errors
Sep 15 '10 #19
Oralloy
988 Recognized Expert Contributor
Ok, good start.

Did you remember to import the requisite classes. Either with the blanket imports:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
or explicitly:
Expand|Select|Wrap|Line Numbers
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;
BTW, is this your first or second Java program? *friendly-laugh*
Sep 15 '10 #20

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

Similar topics

3
1862
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 an illegal operation." I am running this on Windows 98 platform. {If not, I'll post this issue to a Borland newsgroup.} #include <iostream> #include <fstream>
0
1842
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 Sharepoint portal Server B whose domain is stood alone. However, the program is unable to upload file over 2MB, when the file size exceeds, timeout will occur! I have tried to make a change in the web.config and machine.config of sharepoint portal...
5
1804
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 Answer4
1
1364
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 CInternetFile::CInternetFile. My call //pServer = session.GetHttpConnection(strServerName, nPort); pServer = session.GetHttpConnection(strServerName, nPort, pstrUserName, pstrPassword); lpContent = szContent;
0
1835
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 if asked to !!).. 3]want an output file,say OUT, where X and Y are interchanged i.e above 'X' values in'Y' and 'Y' values in 'X'..but 'Z' remain same( and display if asked to!!) 4]get average of values(sum of numbers/number of items) in column Z...
4
2509
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 where my mistake is thanks. This is what I have so far. In a vendlib.h file I have // include file for project 3 //converts input to product_price int product_price(int);
1
1526
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 dimes and pennies print out a long string of numbers. I can't figure out were these numbers are coming from. If anyone can help I will be appreciated. This is my main.cpp code for the body of my program. #include<iostream> #include"vendlib.h"...
1
2191
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
1389
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
2646
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 istaller and everything. i wonder why it would do so?? please help out code to run the adobe reader: ProcessStartInfo startInfo = new ProcessStartInfo(AcrobatReader, Filename)
0
7959
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8021
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8254
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5421
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3876
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3917
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2393
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 we have to send another system

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.