简体   繁体   中英

How do I display a whole category from a text file?

I managed to display 1 record of the asked category but what i need is for the program to display everything from that category. If it's too vague the code might help. Thanks in advance

public static void SearchCatRecord() throws Exception
{
    LoadFile();

    System.out.println("\t\t\t*******************************");
    System.out.println("\n\t\t\t---------SEARCH CATEGORIZED ITEM--------");
    System.out.println("\n\t\t\t*******************************");
    System.out.print("\t\t\tEnter Category: ");
    String searchnum = br.readLine();

    boolean found = false;
        for(int i=0;i<row;i++)
        {
            String record[] = list.get(i).split(",");
            String num = record[1];
            if(searchnum.equals(num))
            {
                found = true;
                System.out.println("\t\t\t*******************************");
                System.out.println("\n\t\t\t---------RECORD FOUND----------");
                System.out.println("\n\t\t\tProduct Number      : "+record[0]);
                System.out.println("\t\t\tCategory              : "+record[1]);
                System.out.println("\t\t\tProduct Name          : "+record[2]);
                System.out.println("\t\t\tPrice                 : "+record[3]);
                System.out.println("\t\t\tQuantity              : "+record[4]);
                System.out.println("\n\t\t\t*******************************");
                Thread.sleep(2000);
                found = true;
                System.out.println("\n\n\t\t\tSearch Completed");   
                exiting();


            }

        }
        if(found == false)
        {
            System.out.println("\t\t\tNo Record Found");
            System.out.println("\t\t\t*******************************");
            exiting();
        }
        MainMenu();
}

The following code asks the user which category should the program display. then it displays the asked category but it only displays one record.

This is because you call exiting(); when you found the first record. You should remove it in your loop.

for example:

for(int i=0;i<row;i++)
{
    String record[] = list.get(i).split(",");
    String num = record[1];
    if(searchnum.equals(num))
    {
            found = true;
            System.out.println("\t\t\t*******************************");
            System.out.println("\n\t\t\t---------RECORD FOUND----------");
            System.out.println("\n\t\t\tProduct Number      : "+record[0]);
            System.out.println("\t\t\tCategory              : "+record[1]);
            System.out.println("\t\t\tProduct Name          : "+record[2]);
            System.out.println("\t\t\tPrice                 : "+record[3]);
            System.out.println("\t\t\tQuantity              : "+record[4]);
            System.out.println("\n\t\t\t*******************************");
            Thread.sleep(2000);
    }

}

System.out.println("\n\n\t\t\tSearch Completed");
if(found == false)
{
        System.out.println("\t\t\tNo Record Found");
        System.out.println("\t\t\t*******************************");
}
exiting();

If you want to find all the records then you should not breakout after finding one record that meets your criteria. I believe your method invocation exiting() is not needed in loop.

On a side note why are you setting found=true twice in the loop? Also what is the need of Thread.sleep(2000) in the code?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM