Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am studying for a test and I am not clear how the substring method works. Plea

ID: 641843 • Letter: I

Question

I am studying for a test and I am not clear how the substring method works. Please kindly explain to me the function of the substring found at the end of the bellow code. If possible please provide me with small additional samples codes using susbstring so I can better comprehend it. Thank you in advance for your assistance!

package test1;

import java.util.Scanner;

/**
*
*
*/
public class NumberToWords {

    public static void main(String[] args) {

        // TODO code application logic here
        String numberInWords = "";
        String tensPart = "";

        Scanner kb = new Scanner(System.in);

        System.out.println("Enter an integer");

        int n = kb.nextInt();

        // seperating n's digits
        int % 10;
        int tens = n / 10;

        //n is between 10 and 19
        if (tens == 1) {
            if (ones == 0) {
                numberInWords = "ten";
            } else if (ones == 1) {
                numberInWords = "eleven";
            } else if (ones == 2) {
                numberInWords = "twelve";
            } else if (ones == 3) {
                numberInWords = "thirteen";
            } else if (ones == 4) {
                numberInWords = "fourteen";
            } else if (ones == 5) {
                numberInWords = "fifteen";
            } else if (ones == 6) {
                numberInWords = "sixteen";
            } else if (ones == 7) {
                numberInWords = "seventeen";
            } else if (ones == 8) {
                numberInWords = "eighteen";
            } else if (ones == 9) {
                numberInWords = "ninety";
            }
        } else {    // if tens is < 1
            //   first convert ones to words
            if (ones == 0) {
                numberInWords = "zero";
            } else if (ones == 1) {
                numberInWords = "one";
            } else if (ones == 2) {
                numberInWords = "two";
            } else if (ones == 3) {
                numberInWords = "three";
            } else if (ones == 4) {
                numberInWords = "four";
            } else if (ones == 5) {
                numberInWords = "five";
            } else if (ones == 6) {
                numberInWords = "six";
            } else if (ones == 7) {
                numberInWords = "seven";
            } else if (ones == 8) {
                numberInWords = "eight";
            } else if (ones == 9) {
                numberInWords = "nine";
            }

            // convert tens to words
            if (tens == 2) {
                tensPart = "twenty";
            } else if (tens == 3) {
                tensPart = "thirty";
            } else if (tens == 4) {
                tensPart = "forty";
            } else if (tens == 5) {
                tensPart = "fifty";
            } else if (tens == 6) {
                tensPart = "sixty";
            } else if (tens == 7) {
                tensPart = "seventy";
            } else if (tens == 8) {
                tensPart = "eighty";
            } else if (tens == 9) {
                tensPart = "ninety";
            }

        }
        // Forming the complete number

        if (tens > 1) // if tens == 0 or tens == 1we already have the complete word
        {
            if (ones == 0) // avoinding strings like twenty zero
            {
                numberInWords = tensPart;
            } else // need to concatenate tens and ones
            {
                numberInWords = tensPart + " " + numberInWords;
            }
        }

        numberInWords = Character.toUpperCase(numberInWords.charAt(0))
                + numberInWords.substring(1);   // I am not sure how the substring works ???????????

        System.out.println(numberInWords);

    }

}

Explanation / Answer

output

Return Value : thework
Return Value : the
As shown

Parameters:

Here is the detail of parameters:

beginIndex -- the begin index, inclusive.

endIndex -- the end index, exclusive.

Return Value:

The specified substring.


In given code numberInWords.substring(1) ,prints the string from index 1 to end of numberInWords string

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote