Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Difference between nextBoolean and hasNextBoolean?

  1. #1
    Junior Member
    Join Date
    Jun 2017
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Difference between nextBoolean and hasNextBoolean?

    My program works fine and I'll post it here for you to see. But I'm trying to understand the difference between hasNext and hasNextBoolean. In the part of my program where I have answer = myScanner.hasNextBoolean I noticed that if I try to type myScanner.nextBoolean eclipse automatically changes it myScanner.hasnextBoolean.

    So what is the difference between the two methods?

    My program is this:

    import java.util.Scanner;
     
    public class TriviaQuestion
    {
    	public static void main(String args[])
    	{
    		Scanner myScanner = new Scanner(System.in);
    		boolean answer;
     
    		System.out.print("True or False: The first televised presidential debate was between Richard Nixon and John Kennedy: ");
     
    		answer = myScanner.hasNextBoolean();
     
    		if(myScanner.nextBoolean() == answer)
    			System.out.print("Correct!");
     
    		else
    			System.out.print("Sorry. You got it wrong.");				
    	}
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Difference between nextBoolean and hasNextBoolean?

    what is the difference between the two methods?
    Please read the API documentation for those two methods. If you have questions about what is said there, copy the text and paste it here with your questions.
    The API doc: http://docs.oracle.com/javase/8/docs/api/index.html


    I have no idea why your IDE wants to change the code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2017
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Difference between nextBoolean and hasNextBoolean?

    Quote Originally Posted by Norm View Post
    Please read the API documentation for those two methods. If you have questions about what is said there, copy the text and paste it here with your questions.
    The API doc: http://docs.oracle.com/javase/8/docs/api/index.html


    I have no idea why your IDE wants to change the code.
    I do not see methods in the API documentation. So I will tell you what I have turned up. hasNextBoolean is a method used to check if the next token can be interpreted as a boolean value. nextBoolean is a method which is used to scan the next token of the input into a boolean value and returns that value.

    I'm not sure I understand the difference.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Difference between nextBoolean and hasNextBoolean?

    I do not see methods in the API documentation.
    Where did you look with that link?
    There two ways to get to a method's doc:
    1)Click on INDEX in the top blue Navigation bar
    Select first letter of the method
    A list of all the methods with that first letter will be displayed.
    Find the method in the class that you are interested in.
    2) Find the class in the lower lefthand frame
    Click on the name of the class
    All the documentation for that class will be shown in the main frame.
    Read the various sections for that class

    hasNextBoolean is a method used to check if the next token can be interpreted as a boolean value.
    nextBoolean is a method which is used to scan the next token of the input into a boolean value and returns that value.
    In other words:
    has... checks if the next token is a valid value: returns True or false
    next.. reads and returns the next value: returns the value
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. DIFFERENCE
    By JIJESHJEANS in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 2nd, 2014, 09:37 AM
  2. Difference between ++i and i++
    By azya in forum Java Theory & Questions
    Replies: 4
    Last Post: November 29th, 2013, 09:09 AM
  3. What is the difference between these two?
    By Java Trainer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 7th, 2013, 02:19 AM
  4. Difference ++j and J++
    By rayan2004 in forum Loops & Control Statements
    Replies: 4
    Last Post: December 4th, 2012, 01:42 AM
  5. Need to know this difference
    By arvind in forum Java Theory & Questions
    Replies: 2
    Last Post: January 19th, 2010, 06:24 AM