• Post Reply Bookmark Topic Watch Topic
  • New Topic

Another Scanner question  RSS feed

 
Higgledy Smith
Ranch Hand
Posts: 193
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did a search on "scanner" but did not see anything that fit my problem.

I am trying to run a very simple scanner code, below, in a servlet. My trouble is at line #7. I am debugging with BEA WorkShop (Eclipse) I place a Watch on "scanner.next()" before executing code at line 7, scanner.next() is at the right value I want assigned to pos in line 8. But after line 7 is executed, scanner.next()== the description in the text file, making me mis-assign values to pos. This only happens on the first loop, in the second loop everything aligns perfectly. What am I doing wrong? Thank you.

[CODE]
String pos = "";
String dep = "";
String des = "";
try {
Scanner scanner = new Scanner(new FileReader(path + Departments.txt"));
scanner.useDelimiter("-");
while (scanner.hasNext()) {
pos = scanner.next();
dep = scanner.next();
des = scanner.nextLine();
if (pos.equals(position)) {
department = new Department(-1, dep, des);
break;
}
}
} catch (FileNotFoundException exception) { }

[ October 09, 2007: Message edited by: Higgledy Smith ]
[ October 11, 2007: Message edited by: Higgledy Smith ]
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you did not post any sample input, it will be hard for anyone to answer your question. Please post some sample input (at least two or three "departments" worth).
 
Higgledy Smith
Ranch Hand
Posts: 193
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the omission of data. Don't laugh at my departments, but here are two entries:
Auror-Department of Magical Law Enforcement-captures wizarding criminals.
Unspeakable-Department of Mysteries-Researches the mysteries of magic.

[ October 10, 2007: Message edited by: Higgledy Smith ]
 
Oggi Olli
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the first line of your departments file? Those two example input lines seem to work with me.
 
Higgledy Smith
Ranch Hand
Posts: 193
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those are the first two lines of my txt file. But it is working right now but I did not change a thing. Must have been to much caffeine. Thank you anyway.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!