如何解决如何搜索字符串并显示txt文件中存储的特定字符串?
我正在从事的项目是一个收入和支出系统,将有以下三种选择: 加上一个月的收入和支出 按月搜索收入和支出 显示一年的报告 对于第一个,我要求用户输入不同类别的收入和支出并写入文本文件。 对于第二个,我无法根据文本文件中的月份进行搜索。你能给我一些建议吗?如何将它们存储在数组中?
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class IncomeExpenseSystem {
public static void main (String[]args) throws IOException{
File summaryTxt = new File("summary.txt");
// To create the menu
Scanner input = new Scanner(system.in);
int choice = 0;
while(choice!= -1) {
System.out.println("Please choose an choice you would like to proceed with from the list below(type -1 to quit;");
System.out.println("1. Add income and expense of a month");
System.out.println("2. Search income and expense by month");
System.out.println("3. display the report of a year");
choice = input.nextInt();
if (choice == 1) {
//ask month
System.out.println(" Add the month:");
String month = input.next();
input.nextLine();
// input the income
System.out.println("Enter the Freelance Salary:");
double freelance_salary = input.nextDouble();
System.out.println("Enter the Monthly Job Salary:");
double monthly_salary = input.nextDouble();
System.out.println("Enter the Business Income:");
double business_salary = input.nextDouble();
System.out.println("Enter the Rental Income:");
double rental_salary = input.nextDouble();
double total_income = freelance_salary + monthly_salary + business_salary + rental_salary;
//input the expenses
System.out.println("Enter the expenses on food:");
double food_expense = input.nextDouble();
System.out.println("Enter the expenses on transportation:");
double trans_expense = input.nextDouble();
System.out.println("Enter the expenses on Entertainment:");
double enter_expense = input.nextDouble();
System.out.println("Enter the expenses on Rent:");
double rent_expense = input.nextDouble();
System.out.println("Enter the expenses on Tax:");
double tax_expense = input.nextDouble();
System.out.println("Enter the expenses on Miscellaneous:");
double miscell_expense = input.nextDouble();
double total_expenses = food_expense + trans_expense + enter_expense + rent_expense + tax_expense+ miscell_expense;
double balance1 = total_income - total_expenses;
double balance =Math.abs(balance1); //to remove negative sign
//To check whether the expenditure is above or below limit with extra expense and saving respectively
if(balance <0) {
System.out.println("The expenditure is above limit with extra expense of " + balance);
}
else {
System.out.println("The expenditure is below limit with saving of " + balance);
}
createData( month,freelance_salary,monthly_salary,business_salary,rental_salary,total_income,food_expense,trans_expense,enter_expense,rent_expense,tax_expense,miscell_expense,total_expenses,balance,summaryTxt);
}
if (choice == 2) {
//Search the income and expenditure by month
System.out.println("Enter the month that you want to search:");
String search_month = input.next();
FileReader fr=new FileReader(summaryTxt); //Creation of File Reader object
BufferedReader br=new BufferedReader(fr); //Creation of BufferedReader object
String s=null;
while((s=br.readLine())!=null) //Reading the content line by line
{
System.out.println(s);
}
fr.close();
}
}
}
static void createData(String month,double freelance_salary,double monthly_salary,double business_salary,double rental_salary,double total_income,double food_expense,double trans_expense,double enter_expense,double rent_expense,double tax_expense,double miscell_expense,double total_expenses,double balance,File summaryTxt)
{
try {
PrintStream ps = new PrintStream(new FileOutputStream(summaryTxt,true));
ps.print(month + " ");
ps.print(freelance_salary + " ");
ps.print( monthly_salary+ " ");
ps.print( business_salary+ " ");
ps.print( rental_salary+ " ");
ps.print(total_income + " ");
ps.print(food_expense + " ");
ps.print(trans_expense + " ");
ps.print(enter_expense + " ");
ps.print(rent_expense+ " ");
ps.print(tax_expense + " ");
ps.print(miscell_expense + " ");
ps.print(total_expenses + " ");
ps.print(balance + " ");
ps.print(" \n");
ps.close();
} catch (FileNotFoundException e) {
System.err.println(e);
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。