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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blknmld69
    New Member
    • Feb 2010
    • 69

    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.

    Code:
    import java.io.BufferedReader; 
       import java.io.FileReader; 
       import java.io.IOException; 
       import java.util.Scanner; 
       import java.util.Vector; 
    
       public class ChargeAccount 
       { 
          static Vector<Integer> validChargeAccountNumbers = new Vector<Integer>(); 
       
       
          public static void main(String[] args) 
          { 
          //load the file
             readMyFile("valid_accounts.txt"); 
          
             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 (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 vector validChargeAccountNumbers 
          public static boolean isValid(int number) 
          { 
             return validChargeAccountNumbers.contains(number); 
          } 
       
          public static void readMyFile(String nameFile) 
          { 
             String record = null; 
             BufferedReader reader = null; 
             FileReader fr = null; 
             int recCount = 0; 
    			
          // Code to read the file and store each account into the vector 
    		// validChargeAccountNumbers 
          
          } 
       }
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    #2
    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
    Code:
    import java.util.*;
    
    public class Test5 {
    public static void main (String[] args) {
    Vector v = new Vector();
    
    List v1 = Arrays.asList(new String[]{"a1", "a2"});
    List v2 = Arrays.asList(new String[]{"b1", "b2"});
    
    v.add(v1);
    v.add(v2);
    
    List v3 = Arrays.asList(new String[]{"b1", "b2"});
    if (v.contains(v3))
    System.out.println("true");
    else
    System.out.println("false");
    }
    }
    Thanks and Regards
    Ramanan Kalirajan

    Comment

    • blknmld69
      New Member
      • Feb 2010
      • 69

      #3
      So you are saying that I should change the validChargeAcco untsNumbers to what? This is my first intro to Java class.

      Comment

      • blknmld69
        New Member
        • Feb 2010
        • 69

        #4
        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:deprecati on for details.

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          ?
          return validChargeAcco untNumbers.cont ains(new Integer(number) );

          Comment

          • RamananKalirajan
            Contributor
            • Mar 2008
            • 608

            #6
            "ERROR:
            Note: readMyFile.java uses or overrides a deprecated API.
            Note: Recompile with -Xlint:deprecati on 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

            Comment

            • blknmld69
              New Member
              • Feb 2010
              • 69

              #7
              unreported exception java.io.FileNot FoundException;

              Now im getting the error:
              ChargeAccount.j ava:51: unreported exception java.io.FileNot FoundException; must be caught or declared to be thrown
              Scanner s = new Scanner(new File("valid_acc ounts.txt"));
              ^

              I dont know how to use the try/catch method
              Code:
              	   import java.io.BufferedReader;  
              	   import java.io.FileReader;  
              	   import java.io.IOException;  
              	   import java.util.Scanner;  
              	   import java.util.Vector;  
              		import java.io.File;
              		
              	  
              	   public class ChargeAccount  
              	   {  
              	      static Vector<Integer> validChargeAccountNumbers = new Vector<Integer>();  
              	  
              	  
              	      public static void main(String[] args)  
              	      {  
              	      //load the file 
              	         readMyFile("valid_accounts.txt");  
              	  
              	         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 (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 vector validChargeAccountNumbers  
              	      public static boolean isValid(Integer number)  
              	      {  
              	         return validChargeAccountNumbers.contains(number);  
              	      }  
              	  
              	      public static void readMyFile(String nameFile) 
              	      {  
              	  
              	          
              	         //code to read the file and store each account into the vector validChargeAccountNumbers  
              	         {  
              	            Scanner s = new Scanner(new File("valid_accounts.txt"));  
              					
              	            while(s.hasNextInt())  
              	               validChargeAccountNumbers.add(s.nextInt());  
              	         }  
              	       }  
              	     }

              Comment

              • jkmyoung
                Recognized Expert Top Contributor
                • Mar 2006
                • 2057

                #8
                Either declare that your function may throw a FileNotFoundExc eption, or add a try-catch clause around the line.

                Comment

                Working...