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 -
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)
-
{
-
}
-
}
-
}
-
}
-
}
-
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: - try {
-
Scanner s = new Scanner(new File(fileName));
-
-
while(s.hasNextInt())
-
validChargeAccountNumbers.add(s.nextInt());
-
} catch (FileNotFoundException e) {
-
e.printStackTrace();
-
}
lol very confused old man (44). I will try that.
Oralloy 988
Recognized Expert Contributor
Eeeep, that makes me ancient (48).
Oralloy 988
Recognized Expert Contributor
Do you understand what the code example does?
it looks like it reading from the file validChargeAcco untNumber
ok so it should be in the readMyFile method?
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.
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
Oralloy 988
Recognized Expert Contributor
Ok, good start.
Did you remember to import the requisite classes. Either with the blanket imports: - import java.io.*;
-
import java.util.*;
or explicitly: - import java.io.File;
-
import java.io.FileNotFoundException;
-
import java.util.Scanner;
BTW, is this your first or second Java program? *friendly-laugh*
Sign in to post your reply or Sign up for a free account.
Similar topics |
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>
|
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...
|
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
|
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;
|
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...
| |
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);
|
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"...
|
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;
|
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 .
|
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)
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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();...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |