0

I have file with a list of names in it and next to the names is a series of digits. The digits represent its level of popularity per every 10 years.

The program I have asks the user to input the file path, then it asks for the user to input a name. The program needs to search the file for the name that was entered. Once the name has been located it needs to print out the name and the numbers on separate lines.

Ex.
file has:
Laura 0 0 1 143 443 100 3 43 12 555 0 0
Lisa 900 1 200 22 4 1 1 45 89 0 0 2

User Input:
Enter Name: Lisa
Name found - Lisa
1900: 900
1910: 1
1920: 200
1930: 22
1940: 4
etc, etc.

I can get it to read the file, but it prints the entire file. I need it to just read out the name that was entered (if it exists) and the stats with it.

Here is my code (I have also included the txt file):

What are the best changes to make? I am new to doing this in any code language.

package babynames;

import java.util.*;
import java.io.*;

public class BabyNames
{
    public static void main(String[] args)
    {
        Scanner userInput = new Scanner(System.in);
        System.out.print("Enter file path: ");
        File file = new File(userInput.nextLine());
        
        Scanner userInput2 = new Scanner(System.in);
        System.out.print("Enter name: ");
        String name = userInput2.nextLine();

        StringBuffer contents = new StringBuffer();
        BufferedReader reader = null;

        try
        {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            while ((text = reader.readLine()) != null)
            {
                contents.append(text).append(System.getProperty("line.separator"));

            }
        }
        catch(FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if(reader != null)
                {
                    reader.close();
                }
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }

        System.out.println(contents.toString());

    }//close main
}

Edited by _dragonwolf_: n/a

Attachments
A 83 140 228 286 426 612 486 577 836 0 0
Aaliyah 0 0 0 0 0 0 0 0 0 380 215
Aaron 193 208 218 274 279 232 132 36 32 31 41
Abagail 0 0 0 0 0 0 0 0 0 0 958
Abbey 0 0 0 0 0 0 0 0 537 451 428
Abbie 431 552 742 924 0 0 0 0 752 644 601
Abbigail 0 0 0 0 0 0 0 0 0 953 562
Abby 0 0 0 0 0 906 782 548 233 211 209
Abdiel 0 0 0 0 0 0 0 0 0 925 721
Abdul 0 0 0 0 0 0 0 903 0 0 0
Abdullah 0 0 0 0 0 0 0 0 0 0 863
Abe 248 328 532 764 733 0 0 0 0 0 0
Abel 664 613 626 575 542 491 497 422 381 385 354
Abigail 0 0 0 0 854 654 615 317 150 50 14
Abigale 0 0 0 0 0 0 0 0 0 0 959
Abigayle 0 0 0 0 0 0 0 0 0 0 720
Abner 569 637 930 0 0 0 0 0 977 0 996
Abraham 144 158 261 350 408 410 503 347 274 238 214
Abram 677 735 0 0 0 0 0 0 875 0 802
Abril 0 0 0 0 0 0 0 0 0 0 869
Ada 85 110 154 196 244 331 445 627 962 0 0
Adah 727 860 0 0 0 0 0 0 0 0 0
Adalberto 0 0 0 0 722 773 725 863 891 0 0
Adaline 832 828 0 0 0 0 0 0 0 0 0
Adam 178 200 280 376 444 407 144 38 22 39 46
Adan 0 0 0 985 920 977 936 729 601 516 504
Addie 165 213 249 324 460 686 0 0 0 0 0
Addison 779 759 895 0 0 0 0 0 794 585 323
Adela 444 433 544 623 697 814 0 0 0 0 0
Adelaida 0 0 0 863 897 0 0 0 0 0 0
Adelaide 253 270 386 642 962 0 0 0 0 0 0
Adelard 924 0 0 0 0 0 0 0 0 0 0
Adelbert 767 760 931 0 0 0 0 0 0 0 0
Adele 200 215 233 318 437 465 714 0 0 0 0
Adelia 549 815 909 0 0 0 0 0 0 0 0
Adelina 672 698 727 748 0 0 0 0 0 0 0
Adeline 194 171 201 395 631 0 0 0 0 0 812
Adell 600 544 590 816 0 0 0 0 0 0 0
Adella 698 558 910 857 0 0 0 0 0 0 0
Adelle 795 816 850 0 0 0 0 0 0 0 0
Aden 0 0 0 0 0 0 0 0 0 0 901
Adolf 581 854 0 0 0 0 0 0 0 0 0
Adolfo 822 785 662 551 691 691 648 713 783 609 556
Adolph 156 185 281 406 640 734 827 0 0 0 0
Adolphus 857 797 972 883 0 0 0 0 0 0 0
Adonis 0 0 0 0 0 0 0 0 0 893 752
Adrain 0 0 0 0 0 0 0 831 959 0 0
Adrian 348 379 395 394 401 374 297 153 107 95 77
Adriana 0 0 0 0 0 0 649 385 225 142 116
Adriane 0 0 0 0 0 0 0 892 847 0 0
Adrianna 0 0 0 0 0 0 0 0 557 245 217
Adrianne 0 0 0 0 0 0 939 648 575 0 0
Adriel 0 0 0 0 0 0 0 0 0 0 928
Adrien 0 0 0 0 0 0 0 0 0 845 935
Adrienne 699 730 633 405 432 336 342 201 189 335 529
Afton 0 964 0 0 0 0 0 0 0 0 0
Agatha 383 454 593 850 0 0 0 0 0 0 0
Agnes 37 46 68 134 229 372 612 0 0 0 0
Agustin 553 589 517 437 543 555 678 642 633 659 578
Agustina 0 981 0 0 0 0 0 0 0 0 0
Ahmad 0 0 0 0 0 0 0 787 613 560 448
Ahmed 0 0 0 0 0 0 0 960 735 652 479
Aida 785 661 414 274 255 328 474 657 0 0 0
Aidan 0 0 0 0 0 0 0 0 0 370 115
Aiden 0 0 0 0 0 0 0 0 0 885 325
Aileen 371 327 315 466 612 675 633 748 724 694 525
Aimee 0 0 0 0 0 0 520 151 203 357 555
Aisha 0 0 0 0 0 0 0 486 518 561 682
Aiyana 0 0 0 0 0 0 0 0 0 0 813
Aja 0 0 0 0 0 0 0 0 802 862 948
Akeem 0 0 0 0 0 0 0 0 650 667 0
Al 411 457 537 458 446 399 515 692 0 0 0
Alaina 0 0 0 0 0 0 0 0 508 391 316
Alan 408 366 213 120 56 45 59 112 109 145 162
Alana 0 0 0 0 841 831 822 517 358 304 247
Alanis 0 0 0 0 0 0 0 0 0 0 776
Alanna 0 0 0 0 0 0 0 0 681 518 486
Alayna 0 0 0 0 0 0 0 0 0 787 426
Alba 969 0 0 864 742 0 0 0 0 0 0
Albert 16 18 21 32 48 76 103 123 147 211 282
Alberta 135 120 146 180 264 431 730 0 0 0 0
Albertha 692 604 659 909 0 0 0 0 0 0 0
Albertine 722 929 0 0 0 0 0 0 0 0 0
Alberto 404 488 411 360 341 311 288 236 213 219 262
Albin 554 516 695 986 0 0 0 0 0 0 0
Albina 504 493 724 0 0 0 0 0 0 0 0
Alda 735 582 709 978 0 0 0 0 0 0 0
Alden 485 499 494 536 631 932 983 0 0 975 849
Aldo 0 699 637 0 0 0 876 0 795 762 665
Aleah 0 0 0 0 0 0 0 0 0 954 623
Alec 968 954 0 0 997 0 812 948 524 137 157
Alecia 0 0 0 0 0 0 878 706 602 886 0
Aleen 0 997 0 0 0 0 0 0 0 0 0
Alejandra 0 0 0 0 0 0 0 712 412 160 170
Alejandrin 0 905 0 925 0 0 0 0 0 0 0
Alejandro 688 567 478 410 517 470 413 237 167 111 86
Alek 0 0 0 0 0 0 0 0 0 0 977
Alena 0 0 0 0 0 0 0 0 0 994 727
Alene 723 478 479 598 898 0 0 0 0 0 0
Alesha 0 0 0 0 0 0 0 0 682 863 0
Alesia 0 0 0 0 0 0 543 0 0 0 0
Alessandra 0 0 0 0 0 0 0 0 0 845 663
Alessandro 0 0 0 0 0 0 0 0 0 0 917
Aleta 0 0 0 0 796 824 922 0 0 0 0
Aletha 608 770 728 945 0 0 0 0 0 0 0
Alex 137 132 177 222 231 250 224 177 79 52 63
Alexa 0 0 0 0 0 0 0 0 465 116 87
Alexander 101 99 141 188 199 212 204 102 48 24 20
Alexandra 728 0 0 0 817 778 581 314 85 28 36
Alexandre 0 0 0 0 0 0 0 0 0 846 943
Alexandrea 0 0 0 0 0 0 0 0 0 515 747
Alexandria 0 0 0 0 0 0 0 893 231 81 91
Alexandro 0 0 0 0 0 0 0 0 946 926 794
Alexia 0 0 0 0 0 0 0 0 848 321 167
Alexis 0 0 0 0 716 734 594 355 112 19 6
Alexus 0 0 0 0 0 0 0 0 0 279 255
Alexys 0 0 0 0 0 0 0 0 0 0 857
Alexzander 0 0 0 0 0 0 0 0 0 0 973
Alf 823 0 0 0 0 0 0 0 0 0 0
Alfonso 341 348 310 301 357 357 389 416 392 440 486
Alfonzo 0 0 0 0 0 878 994 846 0 0 0
Alford 757 680 696 690 884 978 0 0 0 0 0
Alfred 33 42 40 61 92 120 164 224 296 434 575
Alfreda 796 565 485 714 698 592 731 0 0 0 0
Alfredo 475 422 330 327 317 302 323 272 264 267 294
Ali 0 0 0 0 0 0 0 658 578 464 381
Alia 0 0 0 0 0 0 0 0 0 0 738
Alice 10 13 14 23 42 87 149 236 319 382 414
Alicia 750 641 384 329 298 240 170 83 45 79 124
Alida 786 829 0 0 0 0 0 0 0 0 0
Alina 0 0 0 0 0 0 0 975 925 583 455
Aline 343 312 371 489 790 0 0 0 0 0 0
Alisa 0 0 0 0 0 669 334 341 451 544 603
Alisha 0 0 0 0 0 0 856 297 143 188 399
Alison 0 0 0 0 689 347 259 134 107 152 231
Alissa 0 0 0 0 0 0 0 459 314 282 280
Alivia 0 0 0 0 0 0 0 0 0 0 570
Aliya 0 0 0 0 0 0 0 0 0 0 730
Aliyah 0 0 0 0 0 0 0 0 0 501 290
Aliza 0 0 0 0 0 0 0 0 0 0 956
Alize 0 0 0 0 0 0 0 0 0 0 806
Allan 287 282 216 152 147 172 229 331 328 432 477
Allen 119 107 94 86 75 84 101 135 129 184 253
Allene 520 422 445 544 855 0 0 0 0 0 0
Allie 208 285 378 468 811 0 0 0 0 492 392
Alline 0 0 952 0 0 0 0 0 0 0 0
Allison 867 973 766 897 0 419 203 96 49 43 44
Ally 0 0 0 0 0 0 0 0 0 0 608
Allyn 0 0 0 0 868 0 0 0 0 0 0
Allyson 0 0 0 0 0 969 600 460 343 209 246
Allyssa 0 0 0 0 0 0 0 0 0 763 808
Alma 57 68 94 130 182 274 353 377 394 430 500
Almeda 615 605 743 946 0 0 0 0 0 0 0
Almeta 0 982 0 0 0 0 0 0 0 0 0
Almira 942 0 0 0 0 0 0 0 0 0 0
Alois 460 634 799 0 0 0 0 0 0 0 0
Alondra 0 0 0 0 0 0 0 0 0 318 148
Alonso 0 0 0 0 0 0 0 0 933 800 640
Alonza 0 0 0 987 0 0 0 0 0 0 0
Alonzo 273 315 337 364 385 363 385 471 505 499 524
Aloysius 381 471 550 691 974 0 0 0 0 0 0
Alpha 358 460 681 836 0 0 0 0 0 0 0
Alphonse 318 316 409 632 657 818 0 0 0 0 0
Alphonso 728 626 568 533 556 451 522 663 848 0 0
Alta 174 224 262 320 546 787 0 0 0 0 0
Altagracia 0 0 0 995 0 0 0 0 0 0 0
Altha 616 708 911 0 0 0 0 0 0 0 0
Althea 465 404 415 577 699 529 812 0 0 0 0
Alton 175 213 202 190 249 292 414 480 609 763 0
Alva 285 373 373 519 606 834 0 0 0 0 0
Alvah 806 0 0 0 0 0 0 0 0 0 0
Alvaro 0 0 0 0 0 0 813 788 557 531 511
Alvena 919 906 0 0 0 0 0 0 0 0 0
Alvera 0 699 835 0 0 0 0 0 0 0 0
Alverta 764 850 0 0 0 0 0 0 0 0 0
Alvie 622 676 730 646 885 0 0 0 0 0 0
Alvin 98 82 72 95 116 142 182 248 287 371 461
Alvina 333 381 553 672 0 0 0 0 0 0 0
Alvis 925 663 704 844 790 835 0 0 0 0 0
Alyce 374 332 332 443 556 849 0 0 0 0 0
Alycia 0 0 0 0 0 0 0 0 699 695 934
Alysa 0 0 0 0 0 0 0 0 0 877 773
Alyse 0 0 0 0 0 0 0 0 849 0 0
Alysha 0 0 0 0 0 0 0 0 726 519 887
Alysia 0 0 0 0 0 0 0 920 816 738 0
Alyson 0 0 0 0 0 0 971 569 396 439 459
Alyssa 0 0 0 0 0 0 923 395 89 23 12
Amado 0 0 0 0 0 879 0 0 0 0 0
Amador 0 0 0 988 0 0 0 0 0 0 0
Amalia 586 717 744 781 975 0 0 0 0 0 0
Amanda 205 269 387 534 477 400 206 17 3 7 32
Amani 0 0 0 0 0 0 0 0 0 983 751
Amara 0 0 0 0 0 0 0 0 0 0 810
Amari 0 0 0 0 0 0 0 0 0 0 513
Amaya 0 0 0 0 0 0 0 0 0 0 198
Amber 970 907 0 0 0 891 446 60 13 18 46
Ambrose 358 394 551 711 869 0 0 0 0 0 0
Amelia 152 168 211 251 293 386 431 411 254 207 210
America 943 0 966 880 0 0 0 0 0 0 731
Americo 946 669 674 0 0 0 0 0 0 0 0
Ami 0 0 0 0 0 0 0 589 915 0 0
Amie 844 945 942 0 0 0 819 313 374 0 0
Amina 0 0 0 0 0 0 0 0 0 0 964
Amir 0 0 0 0 0 0 0 978 683 545 415
Amira 0 0 0 0 0 0 0 0 0 0 836
Amiya 0 0 0 0 0 0 0 0 0 0 939
Amos 218 222 258 323 402 439 586 578 712 860 0
Amparo 0 851 695 741 994 0 0 0 0 0 0
Amy 196 244 284 356 331 136 35 2 16 61 107
Amya 0 0 0 0 0 0 0 0 0 0 571
Ana 359 286 212 153 156 186 201 182 164 131 134
Anabel 0 0 0 0 0 0 0 0 0 905 0
Anahi 0 0 0 0 0 0 0 0 0 0 366
Anais 0 0 0 0 0 0 0 0 0 955 0
Anastacio 947 956 862 959 0 0 0 0 0 0 0
Anastasia 510 568 757 0 0 0 914 641 455 337 312
Anaya 0 0 0 0 0 0 0 0 0 0 902
Anderson 489 573 596 657 654 689 917 933 828 806 771
Andra 0 0 0 0 0 0 918 0 0 0 0
Andre 969 0 875 712 518 270 171 139 135 162 198
Andrea 560 594 539 389 150 140 66 29 33 46 50
Andreas 0 0 0 0 0 0 984 0 0 870 967
Andres 421 458 436 351 379 401 417 322 231 174 161
Andrew 40 46 57 71 86 67 38 28 13 6 7
Andria 0 0 0 0 0 0 0 740 760 0 0
Andy 343 332 396 383 370 289 226 231 262 253 211
Angel 361 288 192 145 128 146 149 129 110 93 61
Angela 212 195 247 246 175 106 21 7 27 73 79
Angelia 0 0 0 0 0 655 325 381 0 0 0
Angelica 0 0 650 668 829 0 704 278 151 99 132
Angelina 218 197 199 284 475 517 541 386 298 269 162
Angeline 249 210 221 342 604 797 972 878 0 0 0
Angelique 0 0 0 0 0 0 623 374 510 468 465
Angelita 920 908 601 530 615 718 797 902 0 0 0
Angelo 207 165 175 243 322 290 315 362 317 348 303
Angie 437 426 499 476 528 501 250 193 387 624 501
Angus 582 798 747 0 998 0 0 0 0 0 0
Anibal 0 0 896 610 471 556 660 763 910 0 0
Anika 0 0 0 0 0 0 0 991 0 943 628
Anissa 0 0 0 0 0 0 879 602 0 681 762
Anita 219 163 145 119 95 91 83 158 295 488 742
Anitra 0 0 0 0 0 0 0 593 0 0 0
Aniya 0 0 0 0 0 0 0 0 0 0 585
Aniyah 0 0 0 0 0 0 0 0 0 0 737
Anjali 0 0 0 0 0 0 0 0 0 0 923
Anjanette 0 0 0 0 0 0 0 796 0 0 0
Anjelica 0 0 0 0 0 0 0 0 0 964 0
Ann 63 45 50 34 38 45 60 102 180 325 495
Anna 4 7 12 33 72 103 101 86 53 40 22
Annabel 632 662 702 0 0 0 0 0 0 0 930
Annabell 899 852 729 0 0 0 0 0 0 0 0
Annabelle 384 307 263 376 665 0 0 0 0 0 462
Annalise 0 0 0 0 0 0 0 0 0 0 822
Annamae 0 909 792 0 0 0 0 0 0 0 0
Annamarie 0 0 0 0 918 0 931 0 0 918 995
Anne 79 57 83 90 86 83 81 123 130 206 299
Annemarie 0 0 0 0 0 0 580 744 0 0 0
Annetta 591 652 696 694 681 921 962 0 0 0 0
Annette 263 236 225 154 152 123 77 173 332 587 749
Annie 28 34 33 41 83 155 291 405 307 348 313
Annika 0 0 0 0 0 0 0 0 0 887 439
Annis 797 840 929 962 0 0 0 0 0 0 0
2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by __avd
1

Use split() method of String.

.....
            String []items=null;
            boolean found=false;
            while (!found && (text = reader.readLine()) != null)
            {
               items=text.split(" ");
               if(items[0].equals(name)) 
                   found=true;

            }
            if(found){
            for(String v:items){
              System.out.println(v); 
             }
            }
          ....
Votes + Comments
This worked great!! Thanks!!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.