1

Trying to create a method findNewLineWithChar() which returns a String and accepts a Scanner scn and a char c. It returns the line of the scanner which contains c.

I've tried using a string to store the value of currentLine, but it seemed to convolute my code and not provide any answers anyways. I would love to use a String[], but I get a NullPointerException. What am I exactly doing wrong?

public static String findLineWithChar(Scanner scn, char c) {
     /*
      * findLineWithChar returns line in scanner if line has char c
      * @param scanner
      * @param c
      */

        String result = "";
        String[] stringArray = new String[10]; //accepts max 10 lines

        int counter = 0; //populate string array from scn
        while(scn.hasNextLine()) {
            stringArray[counter] = scn.nextLine();
            counter++;
        }

        //iterate through each element in string array
        for (int i = 0; i < 10; i++) {
            if (stringArray[i].contains(c + "")) {
                result = stringArray[i];
                System.out.println(stringArray[i]);
                break;
            } else {
                result = "";
            }
        }

        return result;
    }

It works if I feed it a true statement, findLineWithChar(new Scanner("Look here!\nLook there!\nExtra extra!"), 'x') returns Extra extra!, but findLineWithChar(new Scanner("Look here!\nLook there!\nExtra extra!"), 'q') returns a NullPointerException. How do I deal with this?

nrsmac
  • 101
  • 7
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – George Z. Sep 18 '19 at 22:26
  • 1
    Your for loop assumes that the array has strings in of its ten positions, even if you didn't fill it all the way up. – khelwood Sep 18 '19 at 22:27

2 Answers2

1

First, Javadoc comments go before the method (and start with /**). Second, you don't need an array here; just keep a count to make sure you don't consume more than ten lines (assuming that's necessary). Instead of setting a return variable and breaking the loop, I would return when a matching line is found. I would also prefer String.indexOf(int) to building one character String(s) for comparison. And, add a default return value for when nothing matches. Like,

/**
 * findLineWithChar returns line in scanner if line has char c
 * 
 * @param scanner
 * 
 * @param c
 */
public static String findLineWithChar(Scanner scn, char c) {
    int counter = 0;
    while (scn.hasNextLine() && counter < 10) {
        String line = scn.nextLine();
        if (line.indexOf(c) > -1) {
            return line;
        }
        counter++;
    }
    return "";
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

You almost got it! Just change condition of your while loop.

from this:

while(scn.hasNextLine()) {

to this:

while(scn.hasNextLine() && counter < 10) {
Alan Sereb
  • 2,358
  • 2
  • 17
  • 31