charge account validator modification

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 749 Views - Last Post: 13 May 2014 - 02:41 PM Rate Topic: -----

#1 Comrade9178  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 23-April 14

charge account validator modification

Posted 12 May 2014 - 01:49 PM

Hey d.i.c
So last week I had to write a charge account validator program that compared the number you entered to a list of numbers using the array method. Now I have to modify the program to do the same thing but from a .txt file instead.
I understand it is saying that AccountNumbers.txt does not exist, but I am positive that it does.
Here is my error message:

run:
Exception in thread "main" java.io.FileNotFoundException: AccountNumber.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.util.Scanner.<init>(Scanner.java:656)
at validatormodtest.ValidatorMod.<init>(ValidatorMod.java:24)
at validatormodtest.ValidatorModTest.main(ValidatorModTest.java:24)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)


Here is my code:

ValidatorModTest.java
package validatormodtest;
import java.util.Scanner;
import java.io.*;
/**
 *
 * @author Joe
 */
public class ValidatorModTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
    
    Scanner in = new Scanner (System.in);
    
    ValidatorMod vm = new ValidatorMod("AccountNumber.txt");
        
        System.out.println("youName  May 12, 2014");
        System.out.println("");
        
        System.out.print("Enter your charge account number: ");
        int number = in.nextInt();
        
        if (ValidatorMod.isValid(number) == true)    
        {
        System.out.println("That's a valid account number.");
        }
        else
        {
        System.out.println("That's an INVALID account number.");
        }
    
    }
    
}




ValidatorMod.java
package validatormodtest;
import java.io.*;
import java.util.Scanner;
/**
 *
 * @author Joe
 */
public class ValidatorMod {
    
    private int valid[];
    
    public ValidatorMod(String filename) throws IOException{
    
        String str;
    
    File file = new File(filename);
    
    Scanner inputFile = new Scanner(file);
    
    while (inputFile.hasNext())
    {
        double number = inputFile.nextDouble();
    }
    
    inputFile.close();
    }
    
    public static boolean isValid(int number)
    {
        return true;
    }
   
    
   
    }


Attached File(s)


This post has been edited by Comrade9178: 12 May 2014 - 01:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: charge account validator modification

#2 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 704
  • View blog
  • Posts: 3,026
  • Joined: 03-December 12

Re: charge account validator modification

Posted 12 May 2014 - 01:53 PM

Where is the file? Are you including it from the same directory the program is is? Are you using an IDE? Have you added it as per you IDE requirements?
Was This Post Helpful? 0
  • +
  • -

#3 Comrade9178  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 23-April 14

Re: charge account validator modification

Posted 12 May 2014 - 02:34 PM

I am using netbeans, and I have the file saved in the project folder. What do you mean by add it as per IDE requirements?
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 704
  • View blog
  • Posts: 3,026
  • Joined: 03-December 12

Re: charge account validator modification

Posted 12 May 2014 - 02:39 PM

You need to get the file from resource then,


class().getResource("/path/to/file/AccountNumber.txt") 

Was This Post Helpful? 0
  • +
  • -

#5 Comrade9178  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 23-April 14

Re: charge account validator modification

Posted 12 May 2014 - 02:49 PM

Not really sure how to make use of that. Is there a way to just move the file to the appropriate location?


View Postastonecipher, on 12 May 2014 - 02:39 PM, said:

You need to get the file from resource then,


class().getResource("/path/to/file/AccountNumber.txt") 

Was This Post Helpful? 0
  • +
  • -

#6 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 704
  • View blog
  • Posts: 3,026
  • Joined: 03-December 12

Re: charge account validator modification

Posted 12 May 2014 - 03:48 PM

Then you need to include the file path to the text file.

./folder/filename.txt

Was This Post Helpful? 0
  • +
  • -

#7 Comrade9178  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 23-April 14

Re: charge account validator modification

Posted 12 May 2014 - 05:38 PM

I included the file path and I am positive it is correct, but I am still getting the same error message.


View Postastonecipher, on 12 May 2014 - 03:48 PM, said:

Then you need to include the file path to the text file.

./folder/filename.txt

Was This Post Helpful? 0
  • +
  • -

#8 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 704
  • View blog
  • Posts: 3,026
  • Joined: 03-December 12

Re: charge account validator modification

Posted 13 May 2014 - 12:10 AM

Which filename are you using? I just noticed the file you included was AccountNumbers.txt NOT AccountNumber.txt
Was This Post Helpful? 1
  • +
  • -

#9 Comrade9178  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 23-April 14

Re: charge account validator modification

Posted 13 May 2014 - 08:09 AM

So that fixed the file problem. Now I am getting this error message:

Enter your charge account number: 7881200
Exception in thread "main" java.lang.NullPointerException
at validatormodtest.ValidatorMod.isValid(ValidatorMod.java:32)
at validatormodtest.ValidatorModTest.main(ValidatorModTest.java:34)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

Here is the code I am using now:

package validatormodtest;
import java.io.*;
import java.util.Scanner;
/**
 *
 * @author Joe
 */
public class ValidatorMod {
    
    private static int valid[];
    
    public ValidatorMod(String filename) throws IOException{
    
        String str;
    
    File file = new File(filename);
    
    Scanner inputFile = new Scanner(file);
    
    while (inputFile.hasNext())
    {
        double number = inputFile.nextDouble();
    }
    
    inputFile.close();
    }
    
    public static boolean isValid(int number)
    {
       for (int i = 0; i < valid.length; i++)
        {
            if (valid[i] == number)
            {    
        return true;
            }
   
   
    }
        return false;
    }
}



package validatormodtest;
import java.util.Scanner;
import java.io.*;
/**
 *
 * @author Joe
 */
public class ValidatorModTest {

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
    
    Scanner in = new Scanner (System.in);
    
    ValidatorMod vm = new ValidatorMod("AccountNumbers.txt");
  
        
        System.out.println("youName  May 5, 2014");
        System.out.println("");
        
        System.out.print("Enter your charge account number: ");
        int number = in.nextInt();
        
        if (ValidatorMod.isValid(number) == true)    
        {
        System.out.println("That's a valid account number.");
        }
        else
        {
        System.out.println("That's an INVALID account number.");
        }
    
    }
    
}




View Postastonecipher, on 13 May 2014 - 12:10 AM, said:

Which filename are you using? I just noticed the file you included was AccountNumbers.txt NOT AccountNumber.txt

Was This Post Helpful? 0
  • +
  • -

#10 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 704
  • View blog
  • Posts: 3,026
  • Joined: 03-December 12

Re: charge account validator modification

Posted 13 May 2014 - 08:46 AM

What is the purpose of the is Valid function? parseInt() can be used to determine if the string can be parsed as a number.
Was This Post Helpful? 0
  • +
  • -

#11 Comrade9178  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 23-April 14

Re: charge account validator modification

Posted 13 May 2014 - 08:55 AM

I am not entirely sure, but I think I have to use it because it was included in the uml for my class. It seemed to work fine back when I was using the array list. So there must be a way to make it work. That does seem to be the root of the current issue though. I am not familiar with parseInt()
)

View Postastonecipher, on 13 May 2014 - 08:46 AM, said:

What is the purpose of the is Valid function? parseInt() can be used to determine if the string can be parsed as a number.

Was This Post Helpful? 0
  • +
  • -

#12 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 164
  • View blog
  • Posts: 629
  • Joined: 24-April 10

Re: charge account validator modification

Posted 13 May 2014 - 09:03 AM

10    private static int valid[];

Right now this is null, you need to initialise it, with valid = new int[some size here],
Was This Post Helpful? 1
  • +
  • -

#13 Comrade9178  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 23-April 14

Re: charge account validator modification

Posted 13 May 2014 - 09:32 AM

Ok, well that seemed to help. Its running but just saying that every number is invalid. I am confused about "some size here". I just set it as 0

View Postmike73, on 13 May 2014 - 09:03 AM, said:

10    private static int valid[];

Right now this is null, you need to initialise it, with valid = new int[some size here],

Was This Post Helpful? 0
  • +
  • -

#14 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 704
  • View blog
  • Posts: 3,026
  • Joined: 03-December 12

Re: charge account validator modification

Posted 13 May 2014 - 09:43 AM

isValid is going to evaluate the number passed to it and compare it to, 0- to the size of the valid array. That's why I asked the purpose. What are you tryi.g to evaluate? If it is a number ? If it is a specific number?
Was This Post Helpful? 0
  • +
  • -

#15 Comrade9178  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 23-April 14

Re: charge account validator modification

Posted 13 May 2014 - 10:17 AM

Ok, well it needs to be compared to the list of numbers in the txt file. I don't want to use an array list. So your saying, there is no need for isValid if I am trying to run through the txt file? Whats another method to set what is valid and what is not from the file?

View Postastonecipher, on 13 May 2014 - 09:43 AM, said:

isValid is going to evaluate the number passed to it and compare it to, 0- to the size of the valid array. That's why I asked the purpose. What are you tryi.g to evaluate? If it is a number ? If it is a specific number?

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3