0

I have a .txt file with content that has the following format:

Car
300
1

Basically, the first line is the Expense object's name, the second line is the Expense object's cost, and the third line is the Expense object's frequency. My text file is repetitions of three lines following that format. I'm able to use a Scanner to store all these lines into a list but for some reason, my code isn't generating the Expense objects correctly. When creating an Expense object, my constructor takes string expenseName, int expenseCost, and int expenseFreq as arguments. However, after debugging I noticed that my output was strange because it appears the Scanner is reading everything from the .txt file as if it's a string even if I want it to be stored as an integer. Here is the code:

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;

    public class readInFile {

        static ArrayList list = new ArrayList();
        static String expenseName;
        static int expenseCost;
        static int expenseFreq;

        public static void main(String[] args){
            File file = new File("C:/Practice/expenses.txt");
            ArrayList fileContents = new ArrayList();
            int count = 0;
            int makeExpenseObject = 0;

            try {
                Scanner input = new Scanner(file);

                while(input.hasNext()){
                    fileContents.add(input.nextLine());
                }

                for (Object o: fileContents){
                    if (makeExpenseObject == 3){
                        Expense e = new Expense(expenseName, expenseCost, expenseFreq);
                        makeExpenseObject = 0;
                        list.add(e);
                    }

                    if (o instanceof String){
                        expenseName = o.toString();
                        makeExpenseObject++;
                    }

                    else if (o instanceof Integer && count == 0){
                        expenseCost = (int)o;
                        count++;
                        makeExpenseObject++;
                    }
                    else if (o instanceof Integer && count != 0){
                        expenseFreq = (int)o;
                        count = 0;
                        makeExpenseObject++;
                    }
                }
            }
            catch (FileNotFoundException e) {
                System.out.println("File not found.");
            }

            for (Object o: list){
                Expense e = (Expense) o;
                System.out.println(e.getName() + ", " + e.getCost() + ", " + e.getFreq());
            }

        }
    }

I was thinking of trying a BufferedReader instead of a Scanner but this should be working as far as I can tell. I tried keeping a counter as the file was being scanned and the lines added to a list. Whenever the counter was zero, I would use Scanner's nextLine method. Whenever the counter was not zero, I'd use Scanner's nextInt method and I'd reset the counter to zero if it was ever equal to 2. This didn't work either - it appears that the Scanner was still seeing that the contents of the file were all strings. Why is this?

The goal of this code is to make a simple expense tracking app for fun. I want to add my expenses to a .txt file and have the code read the file. Before, I was using the code in a console application but it gets tedious typing in my expenses every time I use the app. Right now I want to add the .txt reading functionality and eventually a GUI. Thanks for any suggestions.

HandleThatError
  • 530
  • 7
  • 24
  • Read the javadoc of Scanner for the method 'nextLine()', then you'll know why it reads them as strings – Tom Apr 15 '15 at 16:57
  • as @Tom stated it, go have a look at [Scanner's nextLine()](http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine%28%29) – Anton Apr 15 '15 at 16:58

3 Answers3

0

When you read in the data from the file why don't you just assign it to the correct variables? Then parse the variables using the Integer.parseInt(String s) method.

0

I Am still a student, but i think that you have to change the way u view the problem...try to assign every first line to the string member of ur class and the second to the int and the third to the int. for this u can use a for loop from 1 to 3 and three if statements

Hope I helped you

George_v
  • 83
  • 7
0

The reason you are having issues with scanner.nextLine() is because nextLine() returns a String, more info.

Try this:

public class readInFile {

    public static void main(String[] args){

       ArrayList list = new ArrayList();                

        File file = new File("C:/Practice/expenses.txt");
        ArrayList<String> fileContents = new ArrayList();

        try {
            Scanner input = new Scanner(file);

            while(input.hasNext()){
                fileContents.add(input.nextLine());
            }


          for (int i=0; i< fileContents.size()-3; i++){

      String expenseName = fileContents.get(0);
      int expenseCost = Integer.parseInt(fileContents.get(1)) ;
      int expenseFreq = Integer.parseInt(fileContents.get(2));

      Expense e = new Expense(expenseName, expenseCost,expenseFreq);                             
       list.add(e);

            }
        }
Eduardo Dennis
  • 12,511
  • 11
  • 72
  • 102