tyrese
tyrese

Reputation: 11

Item Counter program that will display the number of names that are stored on a file

Can someone help me with this java program that I've been puzzled on for a while. I'll post the question alone with the code but its a Files in java that I've recently started and I'm having trouble adding a loop counter variable to hold and display the number of names stored in the file.

For better understanding here the question I'm working on:

Assume that a file containing a series of names (as strings) is named names.dat and exists on the computers disk. Design a program that displays the number of names that are stored in the file. (Hint: Open the file and read every string stored in it. Each time you read a string, increment a counter variable. When you've read all the strings from the file, the counter variable will contain the number of names stored in the file.)

I don't know how much trouble it will be to aid assist being that this question is file related in java.

Here is my java code so far for better understanding:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

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

        int numLines = 0;
        int sum = 0;

        File namesFile = new File("C:\\Users\\Tyrese\\JAVA 2\\Chapter 10\\names.dat");
        Scanner bot = new Scanner(namesFile);

        System.out.println(bot.nextLine());

        while (bot.hasNext()) {
            numLines = numLines + 1;
            numLines = bot.nextInt();
        }
        System.out.println("The file has" + numLines);
    }
} 

Feel free to replace my file path and name that is a simple notepad document containing a few names, with your own if necessary.

Upvotes: 1

Views: 126

Answers (2)

Lajos Arpad
Lajos Arpad

Reputation: 76434

This is the problematic part of your code:

    System.out.println(bot.nextLine());

    while (bot.hasNext()) {
        numLines = numLines + 1;
        numLines = bot.nextInt();
    }

Your println types a line being read and therefore that line will be ignored later. You need hasNextLine in the while loop and nextLine instead of nextInt.

Fix:

    while (bot.hasNextLine()) {
        numLines = numLines + 1;
        bot.nextLine();
    }

You can compute the sum of integers inside a file similarly. However, the actual solution depends on the structure of the file. If the file contains the integers as lines, without white characters beside the newline, such as

1
5
4
7
2
7

then the solution is:

int sum = 0;
while (bot.hasNextLine()) {
    sum += 1;
    bot.nextLine();
}

however, if the file is a list of numbers, separated by space, such as

3 7 8 2 8 3 6 9

then you read the content of the file via

String content = bot.nextLine();
String[] splited = str.split("\\s+");
int sum = 0;
for (int index = 0; index < splited.length; index++) {
    sum += Integer.parseInt(splited[index])
}

Upvotes: 0

ChaosQK
ChaosQK

Reputation: 49

You have some errors in your code, the way Scanner works for file reading is simple, you open the file the way you did like this : Scanner bot = new Scanner(namesFile); but in order to go to the next line you need to do bot.nextLine() so if you do it before you are in the while or do it twice it will go on 2 lines, so you just need to do like this :

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

        int numLines = 0; // initialise variable

        File namesFile = new File("C:\\Users\\Tyrese\\JAVA 2\\Chapter 10\\names.dat"); // fetch the file
        Scanner bot = new Scanner(namesFile); // open the file

        while (bot.hasNextLine()) { // while the file has a next line -> go on
            numLines++; // add +1 to the variable numLines
            numLines = bot.nextLine(); // go to the next line (if it has one)
        }
        System.out.println("The file has" + numLines); // print the result
    }

You can ask me if you have any further questions

Upvotes: 1

Related Questions