0

I feel like I should give an idea what the code is for. I have this problem where I need to write two functions, one is find() that returns a boolean value if it finds the index of a substring in a given string. The function is supposed to 'save the index values in the index array'. And another method, replace() that replaces all the searched words with the new word, and out put the new string. The index number of the matched words need to be output.

import java.util.Scanner;

public class findAndReplace {

    public static void main(String[] args) {
        String text;
        String find;
        String replace = null;
        String result;
        int index[];

        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the line: ");
        text = scanner.nextLine();
        System.out.print("Enter the string to find: ");
        find = scanner.nextLine();
        System.out.print("Enter the string to replace with: ");
        find = scanner.nextLine();

        find(text, find);
        String result1 = replace(text, find, replace); //!!NUllPointerException             
        System.out.print(result1);
    }

    public static boolean find(String text, String find){ //method return a Boolean value if it finds the index of the word that we are trying to find. Saves the index value in the index array.
        boolean b = false;
        int index_int;
        int index[] = null;

        if (text.indexOf(find)>=0){
            b = true;
            int i = 0;
            do{ 
                index_int = text.indexOf(find);
                index[i]=index_int;
                i++;
                text.replace(text.charAt(index_int), '0');
                System.out.print(index_int);
            } while (text.indexOf(find)>=0);
        }
        return b;
    }

    public static String replace(String text, String find, String replace){ //method replaces all search words with new word and prints.
        String result;
        result = text.replace(find, replace); //!!NUllPointerException
        return result;
    } 
}

Now the problem is, I am finding nullpointer exception at the commented lines. Can you tell me where the problem is? I searched the error to no avail.

femtoRgon
  • 32,893
  • 7
  • 60
  • 87
mrahh
  • 281
  • 1
  • 4
  • 13
  • What is this line in the `find` method supposed to do: `text.replace(text.charAt(index_int), '0');`? The `replace` method of `String` replaces things in your string and returns the resulting string, but you're just throwing the result away. – ajb Mar 12 '14 at 20:02
  • It's supposed to change the string so that in the next iteration the index chosen with the given substring is different. this is not working though. – mrahh Mar 12 '14 at 20:04
  • What is `replace`in `main`: a `String` or a `Method`? – PM 77-1 Mar 12 '14 at 20:31
  • @user3332884 If your last comment is a response to me, it will not work. `s.replace(...)` **never** changes `s`. It returns a string with whatever replacements have been made. You have to do something with the result (which could be `text = text.replace(...)`, but note that this will change just the local copy of the `text` parameter to `find`; it won't modify whatever string you pass to `find`). – ajb Mar 13 '14 at 04:12

5 Answers5

2

your replace is null; you assign to find twice.

XSen
  • 188
  • 1
  • 2
  • 9
1

You made a little mistake, when you ask for the replace string, you store the input in the String find, and not in replace. Replace it with this:

System.out.print("Enter the string to replace with: ");
replace = scanner.nextLine();
0

Your replace variable is null. You need to fix that

Raghvendra Singh
  • 1,775
  • 4
  • 26
  • 53
0

Change this:

System.out.print("Enter the string to find: ");
find = scanner.nextLine();
System.out.print("Enter the string to replace with: ");
find = scanner.nextLine();

To this:

System.out.print("Enter the string to find: ");
find = scanner.nextLine();
System.out.print("Enter the string to replace with: ");
replace = scanner.nextLine();
Mike B
  • 5,390
  • 2
  • 23
  • 45
0

Its because your replace variable is null, that you are passing as an argument.

As per Java docs

" passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown"
Arjit
  • 3,290
  • 1
  • 17
  • 18