Java 如何搜索字符串并显示存储在txt文件中的特定字符串?

Java 如何搜索字符串并显示存储在txt文件中的特定字符串?,java,arrays,bufferedreader,file-handling,Java,Arrays,Bufferedreader,File Handling,我正在从事的项目是一个收入和支出系统,有三种选择: 加上一个月的收入和费用 按月搜索收入和费用 显示年度报告 对于第一个,我要求用户输入不同类别的收入和支出,并写入文本文件。 对于第二个,我无法根据文本文件中的月份进行搜索。你能给我一些建议吗?如何将它们存储在阵列中 import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io

我正在从事的项目是一个收入和支出系统,有三种选择: 加上一个月的收入和费用 按月搜索收入和费用 显示年度报告 对于第一个,我要求用户输入不同类别的收入和支出,并写入文本文件。 对于第二个,我无法根据文本文件中的月份进行搜索。你能给我一些建议吗?如何将它们存储在阵列中

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);
                }
            }
        }
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.FileReader;
导入java.io.IOException;
导入java.io.PrintStream;
导入java.nio.file.Files;
导入java.nio.file.path;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Scanner;
导入java.util.stream.collector;
导入java.util.stream.stream;
公共类IncomeExpenseSystem{
公共静态void main(字符串[]args)引发IOException{
File summaryTxt=新文件(“summary.txt”);
//创建菜单的步骤
扫描仪输入=新扫描仪(System.in);
int-choice=0;
while(选项!=-1){
System.out.println(“请从下面的列表中选择一个要继续的选项(键入-1退出;”);
System.out.println(“1.加上一个月的收入和费用”);
System.out.println(“2.按月搜索收入和费用”);
System.out.println(“3.显示一年的报告”);
choice=input.nextInt();
如果(选项==1){
//询问月份
System.out.println(“添加月份:”);
字符串month=input.next();
input.nextLine();
//投入收入
System.out.println(“输入自由职业者工资:”);
双自由职业者工资=输入.nextDouble();
System.out.println(“输入每月工作工资:”);
双倍月薪=输入.nextDouble();
System.out.println(“输入业务收入:”);
double business_salary=input.nextDouble();
System.out.println(“输入租金收入:”);
双倍租金=输入.nextDouble();
双倍总收入=自由职业者工资+月薪+商务工资+租金工资;
//输入费用
System.out.println(“输入食品费用:”);
双倍食物费用=输入.nextDouble();
System.out.println(“输入运输费用:”);
双重交易费用=输入.nextDouble();
System.out.println(“输入娱乐费用:”);
双击输入_expense=input.nextDouble();
System.out.println(“输入租金支出:”);
双倍租金费用=input.nextDouble();
System.out.println(“输入税务费用:”);
双重税费=输入.nextDouble();
System.out.println(“在杂项中输入费用:”);
double miscell_expense=input.nextDouble();
双倍总费用=食品费用+运输费用+录入费用+租金费用+税费费用+杂项费用;
双倍余额1=总收入-总费用;
double balance=Math.abs(balance1);//删除负号
//分别用额外费用和储蓄检查支出是否高于或低于限额

如果(这不是一个答案,而是考虑将你的CREATEATA()方法设为一个类。一个方法应该不超过3-4个参数。这个提示会使你的生活更容易。同时,尽量保持你的方法尽可能短(4-5行最大,如果是一个非常重的方法,可能是10)。。谢谢@IvanDimitrov,我只是一个初学者,我对如何做感到困惑。我还没有学习OOP。你能给我更多的提示吗?通过阅读Joshua Bloch的《有效Java》一书,熟悉如何使用和创建类、接口和枚举。这是400页的提示。在开始学习之前,试着理解你正在阅读的内容前进。请通过java教程(),如果你想要一个好的OOP介绍。谢谢Youu@ IvanDimitrov。我会考虑你的建议: