Reputation: 3
I am working on a project where I need to scan one line at a time and pass that into another method that does more scanning. currently I have
public static void inputStats(Scanner scan)throws FileNotFoundException {
while (scan.hasNextLine()) {
String text = scan.nextLine();
printDuplicates(text);// my other method
}
}
currently java tells me :String cannot be converted to Scanner my other method simply gets a scanner passed in, the start of it is: printDuplicates(Scanner scan){ Im not sure how to pass the correct thing, or force it.
alternate solution for me would be having scanner.next stop at the end of a line.
I have tried passing a scanner in by creating a new scanner and passing that in instead of a string. I have tried forcing the inputs with (). I have tried every way of not using scanner twice that I know of.
Upvotes: 0
Views: 51
Reputation: 6266
"... currently java tells me :String cannot be converted to Scanner ..."
This is because the printDuplicates parameter is a Scanner object.
"... I have tried passing a scanner in by creating a new scanner and passing that in instead of a string ..."
This is correct, try it again.
"... the start of it is: printDuplicates(Scanner scan){ Im not sure how to pass the correct thing, or force it. ..."
Either will work, just adjust the printDuplicates code.
void printDuplicates(Scanner scan) {
scan.close();
}
void printDuplicates(String text) {
try (Scanner s = new Scanner(text)) {
}
}
Upvotes: 0
Reputation: 273540
You can create a Scanner
that scans a String
by just passing the string to its constructor that takes a string.
printDuplicates(new Scanner(text));
Also consider making printDuplicates
directly take a String
, and create the scanner inside printDuplicates
, if it needs to scan tokens.
void printDuplicates(String text) {
Scanner scan = new Scanner(text);
// ...
}
Upvotes: 1