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 2 of 2

Thread: hasNext() - next() and substring() Methods

  1. #1
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default hasNext() - next() and substring() Methods

    Can someone explain to me what is going on between lines 8 to 11 of this code: https://www.w3resource.com/java-exer...xercise-58.php
    On line, I do not get what they replace in the Scanner initialization new Scanner (System.in) with new Scanner(line).
    In the other lines through 11, it is about what each method is doing.
    (The code is about capitalizing the first letter of each word in a sentence)
    Been reading the documentation but want it to be clear in my head.

  2. #2
    Junior Member BosnianKingdom's Avatar
    Join Date
    Mar 2022
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: hasNext() - next() and substring() Methods

    As far as I can tell, the 2 Scanner objects have different purposes in this case. The first Scanner object reads your input from the console. Since the objective of the code is to capitalize the first letter of every word, you need to somehow break down your input into pieces and then capitalize the first letter of each piece. The second Scanner object is there so that you can take advantage of it's hasNext and next methods to achieve just that. Now you may ask why you can't use the first Scanner object to do this? Well if you take a closer look, the first Scanner object just reads the input and stores it as a String value (line 6). The first Scanner object does not store your input.

    The condition in the while loop basically checks if there is another piece in the lineScan variable which contains your input broken down into pieces. In line 10, you go to the next piece of your input and store that value in the word variable. In line 11 there are a few things happening. You take the first character of your current piece and capitalize it and then concatenate the rest of the word to the capitalized character. Then you take capitalized piece and concatenate it to the upper case line variable.

    The a += b is just a shorter way of doing the same as a = a +b

  3. The Following User Says Thank You to BosnianKingdom For This Useful Post:

    siid14 (March 9th, 2022)

Similar Threads

  1. [SOLVED] Using inputFile.hasNext ?
    By deejaypanininini in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 15th, 2014, 05:54 PM
  2. what is this error in hasnext in webgraph
    By javakar in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 7th, 2014, 04:48 PM
  3. Big problems with my hasNext method and outer loop! Please help!
    By Eclecstatic in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 26th, 2012, 08:55 AM
  4. substring
    By frozen java in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 26th, 2012, 10:10 PM
  5. Need some help with substring please
    By iijakeh in forum Java SE APIs
    Replies: 18
    Last Post: April 7th, 2012, 08:08 AM

Tags for this Thread