Home > Software engineering >  Finding every other integer token using a Scanner for the file
Finding every other integer token using a Scanner for the file

Time:11-27

So I'm using the scanner for a file example that's got 4 boys and 3 girls. After each name there is an integer (Mike 24) like so and it starts with a boy then girl then boy then girl etc. In total there are 4 boys and 3 girls and I'm supposed to count the number of boys and girls and then add up each boys numbers for the sum then the same for girls. Also, when I assigned boys the console.nextInt() does that take the number from the file and then assign to the boys variable? Also, does console.hasNext() have an index like if it reads token #1 then I can say console.hasNext() == 1;?Thank you very much.

Sample data:

Erik 3 Rita 7 Tanner 14 Jillyn 13 Curtis 4 Stefanie 12 Ben 6

Code:

import java.util.*;
import java.io.*;
public class Lecture07 {

  public static void main(String[] args)  throws FileNotFoundException{
    System.out.println();
    System.out.println("Hello, world!");
   
    // EXERCISES:

    // Put your answer for #1 here:
    // You will need to add the method in above main(), but then call it here
    Scanner console = new Scanner(new File("mydata.txt"));
    boyGirl(console);
  }


  public static void boyGirl(Scanner console) { 
    int boysCount = 0;
    int girlsCount = 0;

    while (console.hasNext()) {
          if (console.hasNextInt()) {
              int boys = console.nextInt();
              int girls = console.nextInt();
                  
          }
          else {
            console.next();
          }
    }

  } 
}

CodePudding user response:

the hasNext() will only return true or false. first you shouldn't do int boys = console.nextInt(); inside a loop, since it will create new variable each time and the data will be lost. what you need to do is assign int boys = 0; just bellow your other 2 variables int boysCount and int girlsCount, same goes for int girls = 0

next you will need something like this :

    public static void boyGirl(Scanner console) {
    int boysCount = 0; // here we asigning the variables that we gonna be using
    int girlsCount = 0;
    int boys = 0;
    int girls = 0;

    while (console.hasNext()) { // check if there is next element, it must be the name
        console.next(); // consume the name, we do not want it. or maybe you do up to you
        boys  = console.nextInt(); // now get to the number and add it to boys
        boysCount  ; // increment the count by 1 to use later, since we found a boy

        if (console.hasNext()) { // if statement to see if the boy above, is followed by a girl
            console.next(); // do same thing we did to the boy and consume the name
            girls  = console.nextInt(); // add the number
            girlsCount  ; // increment girl
        }
    }

now after your while loop you do what you want with the variables, such as print them or something. hope i could be of help.

  • Related