Gary Liton
Gary Liton

Reputation: 41

Scanner to String in Java

Scanner x = new Scanner(web.openStream());
System.out.print(web.toString());

I want to change my scanner object to a string, but when I print it, I get something like this

java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]/# 

Why is that?

Upvotes: 0

Views: 1110

Answers (3)

Writwick Bhattacharya
Writwick Bhattacharya

Reputation: 11

Scanner class is used to tokenize the strings. The input you gave as a parameter inside scanner constructor will work as a delimiter.

If you want to change the scanner object to a string and print it....i think the code will be :

Scanner x = new Scanner(web.openStream());
System.out.println(x.toString());

This will print the delimiter string to the console.

If you want to print the string after delimiter, then the code should be:

Scanner x = new Scanner(web.openStream());
System.out.println(x.next());

Upvotes: 1

Neitsch
Neitsch

Reputation: 81

You got to use the Scanner itself. Depending on what you want to read it's gotta be x.next() or x.nextLine() Have a look at this tutorial. Only the part of the tutorial when the Scanner is used matter for you. http://www.mkyong.com/java/how-to-read-input-from-console-java/

Upvotes: 1

Rishi Saraf
Rishi Saraf

Reputation: 1812

toString() only represent string representation Scanner object not actual input. If you want to read data use next() function like below

System.out.print( scanner.next());

Upvotes: 1

Related Questions