-3

In the below code :

import java.io.*;
import java.util.*;
class GFG {
    public static void main (String[] args) {
        Scanner scan = new Scanner(System.in);
        if(scan.hasNext("Teddy ")){ // or if change it here to "Bear"
            System.out.println("Yes its true");
        }
        else {
            System.out.println("false");
        }
    }
}

As per the definition hasNext(String pattern):

Returns true if the next token matches the pattern constructed from the specified string. The scanner does not advance past any input.

  • I'm not able to get an idea of what are tokens for java and if whitespaces are included in tokens

What's the point I'm not able to get for hasNext(String pattern)

Input:

Teddy Bear

output:

False
Community
  • 1
  • 1
Abhishek
  • 47
  • 9
  • 2
    could you format your code so that it is readable? – Albert Sep 05 '19 at 07:28
  • 2
    Yes, I'm agree with @Albert. Also you can refer the [Scanner#hasNext(java.lang.String)](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNext(java.lang.String)) – Dushyant Tankariya Sep 05 '19 at 07:30
  • of course that does not work because you read "Teddy Bear" and look for only "Teddy" or "Bear". Change scan.find("Teddy Bear") and it will work – ItFreak Sep 05 '19 at 07:36
  • Please see it again i rectified my code. – Abhishek Sep 05 '19 at 07:44
  • @DushyantTankariya : I just want to know why whitespace have been eliminated for hasNext("string ") . I haven't found any suitable resource to explain it. – Abhishek Sep 05 '19 at 07:49
  • 2
    @LuciFiEr When you talk about tokens, then you're implicitly talking about patterns and delimiters, since they are the elements that help the tokenizer to know where to split the input. The Scanner class uses spaces as delimiters so don't expect them to be present in any token, but it gives you the opportunity to change the behavior by specifying the pattern using the method: Scanner useDelimiter(String pattern) – XBlueCode Sep 05 '19 at 07:53
  • @Albert : Can you please explain my doubt – Abhishek Sep 05 '19 at 07:55
  • For more information about how to use delimiters pattern, you can refer to the following post: [How do I use a delimiter in Java Scanner?](https://stackoverflow.com/q/28766377/11699772) – XBlueCode Sep 05 '19 at 08:09

2 Answers2

1

The default white-space delimiter used by a scanner is as recognized by Character.isWhitespace(). So token is basically a word which is recognized on the basis of spaces by default. e.g I am a developer (Each word is a token here)

If you want to match word including white-spaces then you can use pattern, like -

String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input);
s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
MatchResult result = s.match();
for (int i=1; i<=result.groupCount(); i++)
System.out.println(result.group(i));
s.close();
-1

The hasNext() is a method of Java Scanner class which returns true if this scanner has another token in its input.

refer= https://www.javatpoint.com/post/java-scanner-hasnext-method

Vishal
  • 65
  • 6
  • Then you didn't read it very well. Try some of the examples. And...What is your Scanner object working against? What string? – DevilsHnd Sep 05 '19 at 07:47
  • @DevilsHnd : you didnt understand my question, it is nowhere written that token include whitespaces or not. it could have been better if you can simply explain why whitespaces and pattern after whitespace cannot process. – Abhishek Sep 05 '19 at 07:52
  • @DevilsHnd my Scanner object is string object as "Teddy " in program and my input string is "Teddy Bear" – Abhishek Sep 05 '19 at 07:54
  • Tokens are determined by the delimiter supplied to the Scanner object. By default the delimiter used are White-spaces. So, if you have a string line **"Teddy Bear"** then **Teddy** is one token and **Bear** is another. These tokens can be individually extracted from the string with the use of a loop utilizing the **Scanner#hasNext()** method in conjunction with the **Scanner#next()** method. Read the tutorial provided to you within this answer. – DevilsHnd Sep 05 '19 at 07:59
  • @DevilsHnd : what about whitespace will it be counted as a token ? – Abhishek Sep 05 '19 at 08:59