1

So I am making a program that reads from my file and allows the user to enter a word to see the occurrence of the word. It runs properly but doesn't ask the user to enter a word and I'm stuck on why, here's the code:

import java.util.*;
import java.io.*;
public class WordOccurence {
    public static void main(String[] args) throws IOException {
        int wordCount=0;
        int word =0;
        Scanner scan=new Scanner(System.in);
        System.out.println("Enter file name");
        String fileName=scan.next().trim();
        System.out.println("Enter the word you want to scan: ");
        Scanner scr = new Scanner(new File(fileName));
        // your code goes here ...
        while(scr.hasNextInt()){
            Scanner word2 = new Scanner(System.in);
            String word1 = word2.next();
            if (word1.equals(word2)){
                word++;
            }
        }
        System.out.println("Total words = " + word);
    }
}
user6904265
  • 1,938
  • 1
  • 16
  • 21
ant c
  • 81
  • 1
  • 11
  • 2
    Can you please indicate which line of code you thought was taking input about what word to scan from the user? – takendarkk Nov 16 '16 at 22:36
  • Well, is the next token to be read an Integer? Have you checked the `while` loop condition is ever true? Also you only need one `Scanner` for `System.in`, just use `scan`. – d.j.brown Nov 16 '16 at 22:39

2 Answers2

1

There are few errors in your code, this version should be work as you expected (I inserted comments trying to explain how to fix)

public static void main(String[] args) throws IOException {
{
    int word =0;
    Scanner scan=new Scanner(System.in);
    System.out.println("Enter file name");
    String fileName=scan.next().trim();
    System.out.println("Enter the word you want to scan: ");
    //You have to insert this line in order to ask the user to input the word to find
    String wordTofind=scan.next().trim();
    Scanner scr = new Scanner(new File(fileName));
    // You should fix also here: scan the file and look if the word is present
    while(scr.hasNext()){
        String word1 = scr.next();
        if (word1.equals(wordTofind)){
            word++;
        }
    }
    System.out.println("Total words = " + word);
    }
}

Don't forget to close the scanners:

scan.close();
scr.close();
user6904265
  • 1,938
  • 1
  • 16
  • 21
  • I would generally opt not to close the standard input stream `System.in` since it cannot be reopened. It is okay if you don't plan on using it again! – d.j.brown Nov 16 '16 at 22:55
  • yeah sorry about that, It was updated from my previous project which was to print the amount that the file has. but thank you for clearing it up. – ant c Nov 16 '16 at 22:57
  • I can see the problem clearly. thank you so much sir – ant c Nov 16 '16 at 22:57
  • @d.j.brown you are right but in this simple "main" I always close the scanner at the end, just only for not see the warning "scanner is never close" ;) – user6904265 Nov 16 '16 at 23:02
  • @antc I'm glad to have helped you in some way. – user6904265 Nov 16 '16 at 23:02
0

If you want it to ask the user, like in a popup box, you can do:

String input = JOptionPane.showInputDialog("Your message");

Edit: You don't need a scanner unless you're iterating over the input.

MacStation
  • 411
  • 4
  • 20
  • 1
    Unless you wanted to iterate over it in some way, why would you 'Scan' the `String` returned by `showInputDialog()`? – d.j.brown Nov 16 '16 at 22:52