Can you explain how scanner works? Does it print everything?

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

I was trying to understand how scanner worked. So, I tied to scan in a text document and iterate only through certain words but it didn't work, it just printed everything. So, I replaced it with a loop that just printed numbers but it never even printed it and it printed all the words in the text document even though I didn't write a print statement for that. Does scanner just automatically print everything? How come my for loop is being skipped too?

Scanner read = new Scanner(new File("Test.txt"));
read.close();
for(int i = 0; i < 5; i++) {
    System.out.println(i);
}
profilepic.png
manpreet 2 years ago

 

Scas://forum.tuteehub.com/tag/n">ns://forum.tuteehub.com/tag/n">ner is not such a nice class, as in reading one has to determine what kind of token one wants to read, and preferably handle all errors.

It reads sequentially a file as tokens, separated default by spaces, and a token can be Stris://forum.tuteehub.com/tag/n">ng/s://forum.tuteehub.com/tag/is://forum.tuteehub.com/tag/n">nt">is://forum.tuteehub.com/tag/n">nt/....

Scas://forum.tuteehub.com/tag/n">ns://forum.tuteehub.com/tag/n">ner is://forum.tuteehub.com/tag/n">n = s://forum.tuteehub.com/tag/n">new Scas://forum.tuteehub.com/tag/n">ns://forum.tuteehub.com/tag/n">ner(s://forum.tuteehub.com/tag/n">new File("Test.txt"));
if (is://forum.tuteehub.com/tag/n">n.hasNextLis://forum.tuteehub.com/tag/n">ne()) {
    Stris://forum.tuteehub.com/tag/n">ng lis://forum.tuteehub.com/tag/n">ne = is://forum.tuteehub.com/tag/n">n.s://forum.tuteehub.com/tag/n">nextLis://forum.tuteehub.com/tag/n">ne();
}
if (is://forum.tuteehub.com/tag/n">n.hasNextIs://forum.tuteehub.com/tag/n">nt()) {
    s://forum.tuteehub.com/tag/is://forum.tuteehub.com/tag/n">nt">is://forum.tuteehub.com/tag/n">nt s://forum.tuteehub.com/tag/n">n = is://forum.tuteehub.com/tag/n">n.s://forum.tuteehub.com/tag/n">nextIs://forum.tuteehub.com/tag/n">nt();
    if (is://forum.tuteehub.com/tag/n">n.hasNext()) {
        Stris://forum.tuteehub.com/tag/n">ng s = is://forum.tuteehub.com/tag/n">n.s://forum.tuteehub.com/tag/n">next();
    }
    if (is://forum.tuteehub.com/tag/n">n.hasNextLis://forum.tuteehub.com/tag/n">ne()) {
        is://forum.tuteehub.com/tag/n">n.s://forum.tuteehub.com/tag/n">nextLis://forum.tuteehub.com/tag/n">ne();
    }
}
s://forum.tuteehub.com/tag/is://forum.tuteehub.com/tag/n">nt">is://forum.tuteehub.com/tag/n">nt a = is://forum.tuteehub.com/tag/n">n.s://forum.tuteehub.com/tag/n">nextIs://forum.tuteehub.com/tag/n">nt(); // Us://forum.tuteehub.com/tag/n">nsafe - are there digits here?
s://forum.tuteehub.com/tag/is://forum.tuteehub.com/tag/n">nt">is://forum.tuteehub.com/tag/n">nt b = is://forum.tuteehub.com/tag/n">n.s://forum.tuteehub.com/tag/n">nextIs://forum.tuteehub.com/tag/n">nt();
is://forum.tuteehub.com/tag/n">n.close();

For a file:

My header lis://forum.tuteehub.com/tag/n">ne
42 s://forum.tuteehub.com/tag/fis://forum.tuteehub.com/tag/n">ne">fis://forum.tuteehub.com/tag/n">ne secret
97 98

Os://forum.tuteehub.com/tag/n">ne cas://forum.tuteehub.com/tag/n">n easily make a mistake.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.