Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8714711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:39:49+00:00 2026-06-13T05:39:49+00:00

I understand how to search through a txt file searching for a value (or

  • 0

I understand how to search through a txt file searching for a value (or an id in this case) however usually this returns the first option it finds.

How would I work this so that it finds the last value in the file related to the id and uses that one? (in this case, the card ID of 17 has a balance of 813.

txt file example

17,721.0
17,744.0
17,767.0
17,790.0
17,813.0

my current search code.

public static double readBalance(int cardNumber)
{
  double balance;  
  String line;

  try {
        File CbFile = new File("cardBalance.txt");
        BufferedReader br = new BufferedReader(
                            new InputStreamReader(
                            new FileInputStream(CbFile)));

        while((line = br.readLine()) != null) {
            if(line.indexOf(cardNumber) != -1)
                break;
        }
        String[] s = line.split("\\,");

        balance = Double.parseDouble(s[1]);
        System.out.println("Blalance of " + cardNumber +
                           " is: " + s[1]);
        br.close();
    } catch(IOException e) {
        System.out.println("read error: " + e.getMessage());
    }


  return  balance;

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:39:51+00:00Added an answer on June 13, 2026 at 5:39 am

    lastIndexOf(String) will give you the last occurrence of a String, if you can read the entire file into memory at once.

    If not, you’ll want something like:

    String cardInfo;
    String cardNumberString = cardNumber + ","; // makes sure that you don't get your
                                                // id in the middle of a value
    while((line = br.readLine()) != null) {
        if(line.indexOf(cardNumberString) != -1)
            cardInfo = line;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, of all, I already search about this but i still can't I understand
I've search through about 10 pages, can't find this question here. I'm trying to
Can someone please help me understand why this code is not working? $('#quick-search-header.widget-title').css('background-image', 'url(dd_includes/images/icons/sliding-menu-arrow-right.gif)');
I want to get the line of a file, search through that line for
I am using the Twitter Search API and I can't understand the id field
OK so now I can understand that SO's search system is primarily based around
I have a problem with my search on my site I don't understand why
I understand this is an easy question but for some reason this just isn't
I understand I can create an enum like this: public enum MyEnum { ONE(1),
I have been going through the algorithm of uniform-cost search and even though I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.