1

I am creating a program like Mint.

right now, I am getting information from a text file, splitting it by the spaces, then going to pass it to the Constructor of another class to create objects. I am having a bit of trouble getting that done properly.

I don't know how to get the information I actually need from the text file with all the extra stuff that's in it.

I need to have an array of objects that has 100 spots. The Constructor is

public Expense (int cN, String desc, SimpleDateFormat dt, double amt, boolean repeat)

The file comes as :

(0,"Cell Phone Plan", new SimpleDateFormat("08/15/2015"), 85.22, true);
(0,"Car Insurance", new SimpleDateFormat("08/05/2015"), 45.22, true);
(0,"Office Depot - filing cabinet", new SimpleDateFormat("08/31/2015"), 185.22, false);
(0,"Gateway - oil change", new SimpleDateFormat("08/29/2015"), 35.42, false);

Below is my code for the main:

Expense expense[]  = new Expense[100];
    Expense e = new Expense();
    int catNum;
    String name;
    SimpleDateFormat date = new SimpleDateFormat("01/01/2015");
    double price;
    boolean monthly; 

    try {
        File file = new File("expenses.txt");
        Scanner scanner = new Scanner(file);

        while (scanner.hasNextLine()) {                
            String line = scanner.nextLine();
            String array[] = line.split(",");

            expenses[i] = new Expense(catNum, name, date, price, monthly);


        }
        scanner.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
6
  • 2
    where did you get the file? the new SimpleDateFormat("01/01/2015") is probably wrong, should be new SimpleDateFormat("dd-MM-yyyy"). Commented Sep 28, 2015 at 14:58
  • Why is your file contains new SimpleDateFormat("08/15/2015") one of the field ? should it not just the date string like "08/15/2015". And offcourse the new Expense(catNum....) , where the catNum should have been array[0] , array[1] for name etc Commented Sep 28, 2015 at 14:59
  • cleanest solution would be matching a regex pattern, capturing the interesting parts in groups, then simply use Integer.parseInt(String) etc to convert from strings to the arguments of your constructor Commented Sep 28, 2015 at 15:03
  • 1
    I highly doubt that is the file. And Expense should probably take a Date rather than SimpleDateFormat. If this is an assignment, I suggest you either reread or post it here verbatim. If the file posted above and the Expense class (constructor) was created by the person who gave the assignment, I would boycott the assignment. Commented Sep 28, 2015 at 15:03
  • That is how the file comes in. I only took off the expense.txt that was at the beginning of the file. The programming assignment is in a PDF file. I could post all of it if that would make you happy peeskillet Commented Sep 28, 2015 at 15:07

1 Answer 1

3

Step by step:

//(0,"Cell Phone Plan", new SimpleDateFormat("08/15/2015"), 85.22, true);
String array[] = line.split(",");

Will produce this array

[0] (0
[1] "Cell Phone Plan"
[2] new SimpleDateFormat("08/15/2015")
[3] 85.22
[4] true);

So

expenses[i] = new Expense(catNum, name, date, price, monthly);

Wont work because it expects another data in almost each parameter:

In order to fix this:

  • you must ignore ( and ); when splitting line
  • be careful with " in the given string, you must scape this characters or ignore them
  • you wont be able to use: new SimpleDateFormat("08/15/2015") you must create the object by yourself
  • this is not a correct date format "08/15/2015"!!!!

SOLUTION: if you are creating the file to parse, I would recommend to change it's format to:

//(0,"Cell Phone Plan", new SimpleDateFormat("08/15/2015"), 85.22, true);
0,Cell Phone Plan,MM/dd/yyyy,85.22,true

Then:

String array[] = line.split(",");

Will produce

[0] 0
[1] Cell Phone Plan
[2] MM/dd/yyyy
[3] 85.22
[4] true

Then you can simply parse non string values with:


UPDATE

Check here a working demo that you must adapt to make it work.

OUTPUT:

public Expense (0, Cell Phone Plan, 08/15/2015, 85.22, false  );
public Expense (0, Car Insurance, 08/05/2015, 45.22, false  );
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much for your answer. I do have a question though...Is there a way to clean up the line as you have done above? Or do you mean you want me to do this myself with the file?
Who created this file?? I should know more about the SimpleDateFormat because data in your example is not correct. About changing the file, yes sure, you better create a snippet that modify data, don't do it manually
My professor created the file. That is what he gave us. Is there a way to just have the line read in and ignore certain parts of the line or do I have to read in the line then somehow have to edit it a few times to get the information needed?
Sure, check String API, you can use replace, substring, indexof and other built in methods to manipulate the String easily, abot DateFormat, use it like in my example and the insert the file date, dont try to create a SimpleDateFormat with file content, just use the date (08/09/2015)
@isabella please check the link in my edit, hope it guides you enough to make it work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.