Scanner object works when running but not when debugging

Answered

Hello,

I have a scanner object which reads from a string.

import java.io.File;
import java.util.Scanner;

public class SeaShellTest {

public static void main(String[] args) {
String test = "She sells sea shells by the sea shore";
String test1 = "The quick brown fox jumps over the lazy dog";
try {
Scanner input = new Scanner(test);
String soFar = "";
while (input.hasNext()) {
soFar += " " + input.next();
System.out.println(soFar);
}
}
catch(Exception e) {System.out.println(e.getCause());}
}
}

This code works as expected when run and prints this:

She
She sells
She sells sea
She sells sea shells
She sells sea shells by
She sells sea shells by the
She sells sea shells by the sea
She sells sea shells by the sea shore

However, when I run in debug mode and put a break point in the while loop, the output is this:

She
She by
She by the
She by the sea
She by the sea shore

Similarly, when debugging with test1, the output is (imagine this builds iteratively like above): "The brown fox jumps over the lazy dog"

The code is in a try catch block because I also tried to scan from a .txt file that had "She sells sea shells by the sea shore" in it. Same problem.

I'm stumped as to why the debugger would behave like this. Any help is greatly appreciated!

1
9 comments

Does it help if you disable this option?

0
Avatar
Permanently deleted user

Unfortunately not

0

I can't reproduce it, the output is the same in Run and in Debug with the breakpoint inside the while loop:

0
Avatar
Permanently deleted user

I slightly modified that code to remove irrelevant details. Sorry, I should have just shown you the actual code:

import java.util.Scanner;

public class SeaShellTest {

public static void main(String[] args) {
DLB trie = new DLB();

String test = "She sells seashells by the seashore";
String test1 = "The quick brown fox jumps over the lazy dog";
Scanner input = new Scanner(test);
String nextWord;
String soFar = "";
while (input.hasNext()) {
nextWord = input.next();
soFar += " " + nextWord;
System.out.println(soFar);
trie.addWord(nextWord);
}
}
}

Here's DLB.java:

public class DLB {
Node root;

class Node {
Node peer;
Node child;
char data;

public Node() {
peer = null;
child = null;
}
public Node(char data) {
this.data = data;
peer = null;
child = null;
}

public returnObj addPeer(char letter) {
//boolean isLastPeer = false;
//if(peer == null) isLastPeer = true;

if (data == '\u0000') {
data = letter;
return new returnObj(this, true);
}
else if(data == letter) return new returnObj(this, false);
else if(peer == null) {
peer = new Node(letter);
return new returnObj(peer, true);
}
else peer.addPeer(letter);
return new returnObj(peer.peer, true);
}

public returnObj addChild(char letter) {
if(child == null) {
child = new Node(letter);
return new returnObj(child, true);
}
if(child.data == letter) return new returnObj(child, false);
else return child.addPeer(letter);
}

public Node getPeer(char c) {
Node current = this;
while(peer != null) {
if(data == c) return current;
else current = current.peer;
}
return null;
}

public Node getChild(char c) {
return child.getPeer(c);
}
}

class returnObj {
Node node;
boolean success;

public returnObj(Node node, boolean success) {
this.node = node;
this.success = success;
}
}

public DLB() {
root = new Node();
}

public void addWord(String word) {
Node current = root;
root.addPeer(word.charAt(0));
for(int i = 1; i < word.length(); i++) {
returnObj r = current.addChild(word.charAt(i));
current = r.node; //se-L-l is current when adding sea so sel-L is current.child. Current pointer should be moved to se-A. Possible fix, return values for addChild().
}
current.child = new Node('.');
}

public int search(String key) {
char c = key.charAt(0);
Node current = root;
current = current.getPeer(c);

for(int i = 1; i < key.length(); i++) {
current = current.getChild(c);
if(current == null) return 0;
}

Node stop = current.getChild('.');
if(stop == null) {
return 1;
}
else if(stop.peer != null) {
return 2;
}
else return 3;
}
}
0

This code produces NPE:

 She
She sells
She sells seashells
Exception in thread "main" java.lang.NullPointerException
at r360.ClassObjects.DLB.addWord(DLB.java:79)
at r360.ClassObjects.SeaShellTest.main(SeaShellTest.java:19)
0
Avatar
Permanently deleted user

Is the NPE related to the issue? Here is output when run:

She
She sells
She sells seashells
Exception in thread "main" java.lang.NullPointerException
at DLB.addWord(DLB.java:78)
at SeaShellTest.main(SeaShellTest.java:18)

When debugging:

 sells
sells seashells
sells seashells by
sells seashells by the
sells seashells by the seashore

Exception in thread "main" java.lang.NullPointerException
at DLB.addWord(DLB.java:78)
at SeaShellTest.main(SeaShellTest.java:18)
Disconnected from the target VM, address: '127.0.0.1:60303', transport: 'socket'

I have some old code from before it threw NPE that I can post in a second. Same issue.

0

I get the same output when debugging with a breakpoint:

0
Avatar
Permanently deleted user

I'm using git for this project. I copied the source files into a new project without VCS and am not running into the issue anymore. Could this have something to do with it?

0

No, it's likely some debugger setting that is different.

0

Please sign in to leave a comment.