-2

Input file has data one line at a time in this manner: Line 1: string, Lines 2-4: ints, Line 5: string etc. I need to count the total number of strings and ignore the ints. How could I do this? Here's my code:

import java.util.Scanner;
import java.io.*;
public class Project5 {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the file name: ");
    String fileName = in.nextLine();
    int stringCounter = 0;
    try {
        File file = new File(fileName);
        Scanner inputFile = new Scanner(file);
        String SnumStudents = inputFile.nextLine();
        int numStudents = Integer.parseInt(SnumStudents);
        Student [] studentList = new Student[numStudents];
        for (int i = 0; i < numStudents; i++)
        {
            String line = inputFile.nextLine();
            String score1 = inputFile.nextLine();
            String score2 = inputFile.nextLine();
            String score3 = inputFile.nextLine();
            studentList [i] = new Student(line, Integer.parseInt(score1), Integer.parseInt(score2), Integer.parseInt(score3));
        }
        System.out.println("Name\t\tScore1\tScore2\tScore3\tTotal");
        System.out.println("---------------------------------------------");
        for (int i=0; i< studentList.length;i++){
            System.out.println(studentList[i].getName() + "\t" + studentList[i].getScore1() + "\t" + studentList[i].getScore2() + "\t" + studentList[i].getScore3() + "\t" + studentList[i].getTotal());
        }
        System.out.println("---------------------------------------------");
        Integer i = Integer.valueOf(SnumStudents);
        stringCounter++;
        System.out.println(stringCounter);
        inputFile.close();
    } catch (IOException e) {
        System.out.println("There was a problem reading from " + fileName);
    }
    finally {
    }
    in.close();
}
}

Input file:

9
Andy Borders
200
250
400
John Smith
120
220
330
Alvin Smith
225
300
278
Seumas Frew
  • 25
  • 2
  • 9

1 Answers1

0

A Possible way (Pseudo-code)

  1. Initialize a counter (to keep track of the number of strings)

    int stringCounter = 0;
    
  2. Use a Reader or a Scanner

    Scanner scan = new Scanner(file);
    
  3. Loop over each line and Determine if the line is a number by trying to parse the value

    while(scan.hasNext()) {
        String currentLine = scan.nextLine();   
        try {
            Integer i = Integer.valueOf(currentLine);
        } catch (NumberFormatException nfe) {
            stringCounter++;
        }
    }
    
  4. Once you exit the loop your counter will have the answer

Community
  • 1
  • 1
gtgaxiola
  • 8,840
  • 5
  • 41
  • 60
  • I updated it with my attempt. I can't get the two nameTotal variables linked though. – Seumas Frew Dec 05 '14 at 16:36
  • Please provide an example of the text file.... Are you reading actual numbers or the word "int"? – gtgaxiola Dec 05 '14 at 16:43
  • Added the input file. – Seumas Frew Dec 05 '14 at 16:45
  • currentLine would be whatever variable is holding the String reading your current line... and Read the Documentation on (Integer.valueOf)[https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#valueOf%28java.lang.String%29] – gtgaxiola Dec 05 '14 at 18:45
  • I'm not familiar with Integer.valueOf(currentLine). What does that do? When I tried the code in Eclipse it just says currentLine can't be resolved to a variable. Does it need to be currentLine? Should it be the variable for the input file? – Seumas Frew Dec 05 '14 at 18:45
  • OK, I changed I changed currentLine and got it to work, however the counter is outputting a value of 0 which isn't correct. – Seumas Frew Dec 05 '14 at 18:47
  • Don't use Scanner.nextInt() you need.. Scanner.nextLine() – gtgaxiola Dec 05 '14 at 18:48
  • @SeumasFrew You are not correctly looping on your File... Updated my answer once more.. – gtgaxiola Dec 05 '14 at 19:02