0

The following code is not correct. When I enter the string, "The rain in Spain" the output is 0 when it should be 2. Or when I put "in in in" the output is 1 when it should be 3. So please help me out and show me how to change this to make it work. Thanks!

import java.util.Scanner;

public class Assignment5b {

public static void main(String[] args){

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter the string: ");

String str = keyboard.next();

String findStr = "in";
int lastIndex = 0;
int count =0;

while(lastIndex != -1){

   lastIndex = str.indexOf(findStr,lastIndex);

   if( lastIndex != -1){
         count ++;
         lastIndex+=findStr.length();
  }
}

System.out.print("Pattern matches: " + count);

}}
Tynamo
  • 149
  • 10

5 Answers5

2

Replace String str = keyboard.next(); with String str = keyboard.nextLine();

.next() method only scans for the next token. While nextLine() scans the entire line being input till you hit enter.

Adarsh
  • 3,613
  • 2
  • 21
  • 37
1

Another way to find the occurrence is simply:

System.out.print("Enter the string: ");
String str = keyboard.nextLine();
String findStr = "in";   
System.out.println(str.split(findStr, -1).length); 
Baby
  • 5,062
  • 3
  • 30
  • 52
0

In your cases. keyboard.next() is taking word by word. So the expression keyboard.next() will give you the first word - "The". So your whole of code will run for String str = "The".

Change your keyboard.next() to keyboard.nextLine()

or

put your code in a while condition like

while(keyboard.hasNext()) {

 }
Arjit
  • 3,290
  • 1
  • 17
  • 18
0
import java.util.Scanner;


public class Temp {

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

    System.out.print("Enter the string: ");

    String str = keyboard.nextLine();

    String findStr = "in";
    int lastIndex = 0;
    int count =0;

    while(lastIndex != -1){

       lastIndex = str.indexOf(findStr,lastIndex);

       if( lastIndex != -1){
             count ++;
             lastIndex+=findStr.length();
      }
    }

    System.out.print("Pattern matches: " + count);
}

}

NextLine Documentation says:

public String nextLine()

Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.

Returns: the line that was skipped Throws: NoSuchElementException - if no line was found IllegalStateException - if this scanner is closed

GyaniPundit
  • 133
  • 1
  • 8
0

import java.util.Scanner;

public class Assignment5b {

    public static void main(String[] args){

        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter the string: ");

        String str = keyboard.nextLine();

        String findStr = "in";
        int lastIndex = 0;
        int count =0;

        while(lastIndex != -1){

            lastIndex = str.indexOf(findStr);

            if( lastIndex != -1){
                str = str.substring(lastIndex+findStr.length(),str.length());
                count ++;
            }
        }

        System.out.print("Pattern matches: " + count);

    }}

Couple of the things to be noted -

  1. You need to use keyboard.nextLine(); if you need the entire line. Else it will just get the first word.
  2. If you input "in" in the input statement "The rain in Spain". Program will output 3, there is "in" in "rain", "in" and "Spain"
hellboy
  • 2,222
  • 1
  • 14
  • 19