1

I'm new to Java. I'm trying to do a database on text file (Don't ask me why). The thing is I want to do is to read specific lines of a text file.

Text file:

Salary
3,400
21/12/2015
Tax Refund
3
22/12/2015
Tax Refund
320
23/12/2015
Savings Deposit
1,230
23/12/2015
Bonus
343
23/12/2015

Every 3 lines there is a new entry. For example Salary is the category, 3,400 is the amount and 21/12/2015 is a date. What i want to do is to read only the amounts (e.g. 3,400 3 320 1,230 etc.). The only thing I succeed to do is to print a the lines by matching them on true print statement, with this code:

while(opnEsoda.hasNext()) { // diabazei kathe seira sto txt
    // diabazei kathe seira kai emfanizei to value tis kathe mias ana 3.
    System.out.print("You got money from: " + opnEsoda.nextLine()+ ", "); 

    System.out.print("Amount: " + opnEsoda.nextLine()+", ");
    System.out.print("Date: " + opnEsoda.nextLine()+". ");               
    System.out.println(); 
} 

opnEsoda is the Scanner and prints:

You got money from: Salary, Amount: 3,400, Date: 21/12/2015. 
You got money from: Tax Refund, Amount: 3, Date: 22/12/2015. 
You got money from: Tax Refund, Amount: 320, Date: 23/12/2015. 
You got money from: Savings Deposit, Amount: 1,230, Date: 23/12/2015. 
You got money from: Bonus, Amount: 343, Date: 23/12/2015.
Kevin Andrid
  • 1,963
  • 1
  • 15
  • 26
Alex Andreadis
  • 347
  • 1
  • 4
  • 17

3 Answers3

3

Just skip the other lines :-)

while(opnEsoda.hasNext()) {
    // skip category
    opnEsoda.nextLine(); 

    System.out.print("Amount: " + opnEsoda.nextLine()+", ");

    // skip date
    opnEsoda.nextLine();

    System.out.println(); 
} 
cadrian
  • 7,332
  • 2
  • 33
  • 42
2

You can use below JAVA8 code and get file in format of list of String. It will be easy to retrieve line number 'n' for list.

        int n=2;
        List<String> fileLines = Files.readAllLines(Paths.get("c:\\abc.txt"));
        while(n<fileLines.size()){
            fileLines.get(n);
            n+=3;
        }
Abhijeet Kale
  • 1,656
  • 1
  • 16
  • 34
2

This in my opinion is a bit cleaner than just skipping lines and it allows you to have access to the skipped elements incase you need them later :)

do
{
    String[] entry = new String[3];

    for (int i = 0; i < 3; i++)
    {
        if (opnEsoda.hasNextLine())
        {
            // Trim removes leading or trailing whitespace.
            entry[i] = opnEsoda.nextLine().trim(); 
        }
    }

    System.out.println(entry[2]);
}
while (opnEsoda.hasNextLine)
Hatefiend
  • 3,416
  • 6
  • 33
  • 74
  • Thank you. That worked. The next thing i want to do is to sum this numbers, take the date of each amount and print week, month and year stats. – Alex Andreadis Dec 23 '15 at 09:29
  • You can use Integer.parseInt( ) to convert the String values into integer values. The rest should be pretty easy with some well placed math operators. – Hatefiend Dec 23 '15 at 09:33
  • @AlexAndreadis see [that question](http://stackoverflow.com/questions/999172/how-to-parse-a-date) – cadrian Dec 23 '15 at 09:40