86.8k views
3 votes
In Pseudo code:

A text file holding financial information contains lines with the following structure:

o ID (5 characters)

o Rate (2 digits)

o Taxes (2 digits)

o Each item is separated by a colon":"

Let the user enter an ID to search for and output the rate of the ID, if not found output a message​

User Ndemoreau
by
8.0k points

1 Answer

0 votes

Answer:

Step-by-step explanation:

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.util.Scanner;

public class FinancialInfoSearch {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter ID to search: ");

String searchID = scanner.nextLine();

scanner.close();

try (BufferedReader br = new BufferedReader(new FileReader("financial_info.txt"))) {

String line;

while ((line = br.readLine()) != null) {

String[] data = line.split(":");

if (data[0].equals(searchID)) {

System.out.println("Rate for " + searchID + ": " + data[1]);

return;

}

}

System.out.println("ID not found.");

} catch (IOException e) {

System.err.println("Error reading file: " + e.getMessage());

}

}

}

In this code, the user is prompted to enter the ID to search for. The BufferedReader class is used to read the lines of the file. For each line, the split method is used to split the line into an array of strings using the colon character as a delimiter. The first element of the array is compared to the search ID, and if they match, the rate is outputted. If the end of the file is reached and no match is found, a message is outputted saying that the ID was not found.

thank you

User Jamadri
by
7.0k points