Scanner object works when running but not when debugging
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!
Please sign in to leave a comment.
Does it help if you disable this option?
Unfortunately not
I can't reproduce it, the output is the same in Run and in Debug with the breakpoint inside the while loop:
I slightly modified that code to remove irrelevant details. Sorry, I should have just shown you the actual code:
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;
}
}
This code produces NPE:
Is the NPE related to the issue? Here is output when run:
When debugging:
I have some old code from before it threw NPE that I can post in a second. Same issue.
I get the same output when debugging with a breakpoint:
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?
No, it's likely some debugger setting that is different.