2

I am writing a program for homework where I am to modify a string using a menu. The rest of the code works fine except for one part that has me in a bind. I am using a method in order to find a word and all its occurrences in the String. Whenever I execute this method outside of a loop, I get the result I need, but whenever I use it inside of a while or switch statement, the program does not give me anything back. The method needs to return an int for the number of occurrences. This is the excerpt of that code:

    import java.util.Scanner;

    public class test {
    public static Scanner scnr = new Scanner(System.in);

    public static int findWord(String text, String userText) {
        int occurance = 0;
        int index = 0;

        while (index != -1) {
            index = userText.indexOf(text, index);
            if (index != -1) {
                occurance++;
                index = index + text.length();
            }
        }

        return occurance;
    }

    public static void main(String[] args) {

        System.out.println("Enter a text: ");
        String userText = scnr.nextLine();

        System.out.println("Enter a menu option");
        char menuOption = scnr.next().charAt(0);

        switch (menuOption) {
        case 'f':
            System.out.println("Enter a phrase from text: ");
            String text = scnr.nextLine();

            int occurance = (findWord(text, userText));

            System.out.println("" + text + " occurances : " + occurance + "");
            break;
        default:
            System.out.println("Goodbye");
        }

        return;
    }
}

Now a couple things I have noticed. If I prompt the user while inside the method, i do get back my integer, but not the text that i was looking for in order to complete my println inside the switch statement. And whenever i prompt the user for the word inside the switch statement, i get nothing back. If anyone has any solutions for me, i would greatly appreciate it, since i haven't a clue what i could be overlooking or missing.

rici
  • 201,785
  • 23
  • 193
  • 283
James
  • 33
  • 4
  • Please learn how to debug your code. So you can see what is happening. For some reason the text is read as empty string, so your loop never finishes (the string "" is found at index 0 in each loop!). – Heri Nov 25 '16 at 18:11
  • The reason could be because you are using `nextLine()` after `next()` in a loop and as the latter one does not consume the last newline, this issue must be occurring. Did you check this thread ?http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo – Shashank Kadne Nov 25 '16 at 18:16

2 Answers2

0

You need to change char menuOption = scnr.next().charAt(0); to char menuOption = scnr.nextLine().charAt(0);.

Raghav
  • 4,360
  • 1
  • 20
  • 31
0

The problem is with your Scanner method, which you are reading the continuously with scnr.next(), but should be changed to scnr.nextLine()` as shown below:

public static void main(String[] args) {
        Scanner scnr = null;
        try {
            scnr = new Scanner(System.in);

        System.out.println("Enter a text: ");
        String userText = scnr.nextLine();

        System.out.println("Enter a menu option");
        char menuOption = scnr.nextLine().charAt(0);

        switch (menuOption) {
        case 'f':
            System.out.println("Enter a phrase from text: ");
            String text = scnr.nextLine();

            int occurance = (findWord(text, userText));

            System.out.println("" + text + " occurances : " + occurance + "");
            break;
        default:
            System.out.println("Goodbye");
        }
        return;
        } finally {
            if(scnr != null)
                scnr.close();
        }
    }

Also, ensure that you are closing the scanner object properly in thefinally block.

developer
  • 19,553
  • 8
  • 40
  • 57