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 6318
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
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 -
import java.util.Scanner;
-
-
public class ChargeAccount1
-
{
-
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 (ChargeAccount1.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;
-
}
-
}
-
return false;
-
}
-
}
-
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)
Oralloy 988
Recognized Expert Contributor
Glad to help.
Good luck with the rest of your class.
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!!!!!!!!!!!!
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!
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! :)
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.
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |