473,421 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

My program gives the same result whether i input the correct account or not

My program checks a valid_accounts.txt to see if the input account number is valid. When i run the code with either a correct or incorrect code it outputs "the code is invalid" Can anyone help with this? I do believe it is something simple. I have been working all day on this program.

Expand|Select|Wrap|Line Numbers
  1. import java.io.BufferedReader; 
  2.    import java.io.FileReader; 
  3.    import java.io.IOException; 
  4.    import java.util.Scanner; 
  5.    import java.util.Vector; 
  6.  
  7.    public class ChargeAccount 
  8.    { 
  9.       static Vector<Integer> validChargeAccountNumbers = new Vector<Integer>(); 
  10.  
  11.  
  12.       public static void main(String[] args) 
  13.       { 
  14.       //load the file
  15.          readMyFile("valid_accounts.txt"); 
  16.  
  17.          Scanner in = new Scanner(System.in); 
  18.  
  19.       // Ask the user for an account number 
  20.          System.out.print("Please enter an account number: "); 
  21.  
  22.       // Get the number from the user 
  23.          int number = in.nextInt(); 
  24.  
  25.       // Check to see if the number is valid 
  26.          if (isValid(number) == true) 
  27.          { 
  28.             System.out.println("That account number is valid."); 
  29.          } 
  30.  
  31.          else 
  32.          { 
  33.             System.out.println("You did not enter a valid account number."); 
  34.          } 
  35.       } 
  36.  
  37.    // Check to see if an account number is valid by comparing it to the 
  38.     // entries in the vector validChargeAccountNumbers 
  39.       public static boolean isValid(int number) 
  40.       { 
  41.          return validChargeAccountNumbers.contains(number); 
  42.       } 
  43.  
  44.       public static void readMyFile(String nameFile) 
  45.       { 
  46.          String record = null; 
  47.          BufferedReader reader = null; 
  48.          FileReader fr = null; 
  49.          int recCount = 0; 
  50.  
  51.       // Code to read the file and store each account into the vector 
  52.         // validChargeAccountNumbers 
  53.  
  54.       } 
  55.    }
  56.  
Sep 14 '10 #1
7 1831
RamananKalirajan
608 512MB
The problem in your program may be you are pasing a primitive datatype.But it should have an object as parameter. Just change that to Object and check.

This is a sample pogram from Net
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. public class Test5 {
  4. public static void main (String[] args) {
  5. Vector v = new Vector();
  6.  
  7. List v1 = Arrays.asList(new String[]{"a1", "a2"});
  8. List v2 = Arrays.asList(new String[]{"b1", "b2"});
  9.  
  10. v.add(v1);
  11. v.add(v2);
  12.  
  13. List v3 = Arrays.asList(new String[]{"b1", "b2"});
  14. if (v.contains(v3))
  15. System.out.println("true");
  16. else
  17. System.out.println("false");
  18. }
  19. }
Thanks and Regards
Ramanan Kalirajan
Sep 14 '10 #2
So you are saying that I should change the validChargeAccountsNumbers to what? This is my first intro to Java class.
Sep 14 '10 #3
Im thinking that it has something to do with the error that i receive when I compile my readMyfile. It show the error but it still runs!

ERROR:
Note: readMyFile.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Sep 14 '10 #4
jkmyoung
2,057 Expert 2GB
?
return validChargeAccountNumbers.contains(new Integer(number));
Sep 14 '10 #5
RamananKalirajan
608 512MB
"ERROR:
Note: readMyFile.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details. "

This is not an error. It is warning. You have used some methods in your file, those are deprecated. As jmkYoung said send an object as method parameter. It will work.

Thanks and Regards
Ramanan Kalirajan
Sep 15 '10 #6
Now im getting the error:
ChargeAccount.java:51: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
Scanner s = new Scanner(new File("valid_accounts.txt"));
^

I dont know how to use the try/catch method
Expand|Select|Wrap|Line Numbers
  1.        import java.io.BufferedReader;  
  2.        import java.io.FileReader;  
  3.        import java.io.IOException;  
  4.        import java.util.Scanner;  
  5.        import java.util.Vector;  
  6.         import java.io.File;
  7.  
  8.  
  9.        public class ChargeAccount  
  10.        {  
  11.           static Vector<Integer> validChargeAccountNumbers = new Vector<Integer>();  
  12.  
  13.  
  14.           public static void main(String[] args)  
  15.           {  
  16.           //load the file 
  17.              readMyFile("valid_accounts.txt");  
  18.  
  19.              Scanner in = new Scanner(System.in);  
  20.  
  21.           // Ask the user for an account number  
  22.              System.out.print("Please enter an account number: ");  
  23.  
  24.           // Get the number from the user  
  25.              int number = in.nextInt();  
  26.  
  27.           // Check to see if the number is valid  
  28.              if (isValid(number) == true)  
  29.              {  
  30.                 System.out.println("That account number is valid.");  
  31.              }  
  32.  
  33.              else  
  34.              {  
  35.                 System.out.println("You did not enter a valid account number.");  
  36.              }  
  37.           }  
  38.  
  39.        // Check to see if an account number is valid by comparing it to the entries in the vector validChargeAccountNumbers  
  40.           public static boolean isValid(Integer number)  
  41.           {  
  42.              return validChargeAccountNumbers.contains(number);  
  43.           }  
  44.  
  45.           public static void readMyFile(String nameFile) 
  46.           {  
  47.  
  48.  
  49.              //code to read the file and store each account into the vector validChargeAccountNumbers  
  50.              {  
  51.                 Scanner s = new Scanner(new File("valid_accounts.txt"));  
  52.  
  53.                 while(s.hasNextInt())  
  54.                    validChargeAccountNumbers.add(s.nextInt());  
  55.              }  
  56.            }  
  57.          } 
  58.  
Sep 15 '10 #7
jkmyoung
2,057 Expert 2GB
Either declare that your function may throw a FileNotFoundException, or add a try-catch clause around the line.
Sep 17 '10 #8

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

Similar topics

10
by: KENNY L. CHEN | last post by:
Dear experts, I have two tables in my Oracle 8i database: TEST (COL1,COl2,REC_NO) and TEST1 (COL1,COL2,REC_NO). Both tables are unique-indexed on (COL1,COL2,REC_NO). I think the following...
2
by: swhite76 | last post by:
I have a section of code that adds some double values and gives an incorrect result. This occurs with data that isn't really waht I would call high precision. An example is the following code...
2
by: Morten Snedker | last post by:
I have an Access application where my forms have to different colors. These I wish to use in my vb.net app as well. I copy screen and paste into Photoshop and use the color picker to get the...
0
by: Mark Sapiro | last post by:
email.Utils.parseaddr('Real Name ((comment)) <address@example.com>') returns ('comment <address@example.com>', 'Real') Granted the string above is invalid as RFC 2822 does not allow...
0
by: gguan123 | last post by:
Recommend an excellent ASP program gives everyone:Webmaster club news system v5.09 demo:http://www.caifuw.com/en/new/ download:http://dow.caifuw.com/new5.09freeEn.rar Categories:asp(news system)...
1
by: Syl | last post by:
Hello! Can someone verify this for me please. Should these 2 statments return the same result ? The 2nd one is the original, the first one is my re-write. Thanks : SELECT orders_status,...
2
by: gngui | last post by:
i have a query that subtracts the date of birth (dob) from the date, i.e. age_d: Date()- This is OK as it gives me the difference in days e.g. 700 days. i would like the result to show me the age...
10
by: manojsingh | last post by:
hi, This is my program and it is resluting same message "checked" everytime, either i checed it or unchecked the given checkbox. Please have a look . <html> <head> <title>Customer...
3
by: Deano | last post by:
I have a nice report that uses sql to report employee absences. I grab the employee and related absence data using the employee id field which is unique. I use the employee id as a header on...
1
by: poonamt | last post by:
Hello Friends, I created web application in .net using C# where i created parameterized store procedure for join query but its showing me same result for different parameter in .net but when i...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
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
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,...

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.