0

I'm a CS210 student and I am having trouble checking a text file for multiple names to pull data from it. I'm given the following data to store in a text file. It is a name, followed by 11 integers to read data from:

-Sally 0 0 0 0 0 0 0 0 0 0 886 

-Sam 58 69 99 131 168 236 278 380 467 408 466

-Samantha 0 0 0 0 0 0 272 107 26 5 7

-Samir 0 0 0 0 0 0 0 0 920 0 798

The following code works well for the first name in the group (Sally) and runs correctly. But for the program doesn't return anything to the console for Sam, Samantha or Samir. I'm not sure why it will work correctly for one, but not work for the others.

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

public class TestSpace1 {
    public static void main(String[] args) throws FileNotFoundException{

    Scanner fileInput = new Scanner(new File("names.txt"));
    String nameUsed = nameSearch();
    dataScan(fileInput, nameUsed);

}

public static void dataScan(Scanner file, String name) {
    String nameCheck = file.next();

        try{
            // While there are still tokens to be read in the file
            while(!name.equals(file.next())) {
                while(file.hasNext()) {
                // If the name is equal to the next String token in the file
                    if (nameCheck.equals(name)) {

                        // Initialize variable to contain start year
                        int year = 1910;

                        // Run through for loop to add up the integers and display them in value pairs for the year
                        for (int i = 0; i < 11; i++) {
                            int ranking = file.nextInt();
                            System.out.println(year + ": " + ranking);
                            year += 10;

                        }

                      // If the file doesn't read the name, loop to the end of the line, start at top of loop
                    } else {

                        file.nextLine();


                    }

                }
            }
            // If there is a line out of bounds, catch exception
        } catch (Exception e) {
            System.out.println("All lines have been read");
        }

}

public static String nameSearch() {
    Scanner input = new Scanner(System.in);


    System.out.println("This program allows you to search through the");
    System.out.println("data from the Social Security Administration");
    System.out.println("to see how popular a particular name has been");
    System.out.println("since 1900.");

    System.out.print("What name would you like to search? ");
    String name = input.nextLine();

    return name;
}

}
tk3
  • 990
  • 1
  • 13
  • 18
dat_cube
  • 13
  • 1
  • You don't have a `break` or `continue` statement, so when do you think the `while(file.hasNext())` loop ends? So once it does end, i.e. `file.hasNext()` returned false, what do you think happens when the outer loop condition calls `file.next()`? If you think it fails with `NoSuchElementException`, you'd be right, so outer loop will either skip immediately, or it will iterate once and then die! You'd have known this if you hadn't discarded the exception. **Don't discard exceptions**. – Andreas May 19 '18 at 21:45
  • Hi there, thank you for the quick response! So if I'm understanding this correctly, while(file.hasNext()) returns a boolean. So once it searches through the file once, it returns false at the end and breaks the loop? Say I enter "Sam" as my input, shouldn't the scanner read the first line, see it doesn't equal sally, and read line by line until it finds "Sam"? I guess I'm confused as to why it won't check the beginning point (or how can I check the first Token in each line) to see if my input is equal to that token. Thanks so much! – dat_cube May 19 '18 at 21:49

1 Answers1

1

The problem is nameCheck is not updating to be the next token in the file. You need to move nameCheck inside the while loop so it changes every loop. Also you don't need the while(!name.equals(file.next()))) because then if the name match is file.next the loop would just terminate instead of calculating the sum of the integers. The code works for the first one because it is the first one in the list so nameCheck doesn’t need to update and file.next() will be the next one not the first one. Your code should look something like this:

try{
    while(file.hasNext()){
        String nameCheck = file.next();
            if (nameCheck.equals(name)){
                //code for calculating sum
             }
    }
 } 
sher222
  • 135
  • 1
  • 8
  • Oh my goodness, moving nameCheck inside the loop so that it updates worked like a charm. Thanks for the valuable insight -- I can't believe it came down to something that simple! – dat_cube May 20 '18 at 00:07