Write a java program to find nth number word from the string

Write a java program to find nth number word from the string. Read a string that contains comma-separated fruit names and also a number N. Pick the nth fruit and return it. If the total number of elements is less than the number specified in N, then return the last element.

Input and Output Format

  • Input consists of a string and integer.
  • Output consists of a string.

Refer sample output for formatting specifications

Sample Input 1:
Apple, Banana, Orange
2

Sample Output 1:
Banana

Sample Input 2:
Apple, Banana, Orange
4

Sample Output 2:
Orange

Java program to find nth number word from the string

Following are the steps to find the nth word from the string:

  • Input a string that contains comma-separated fruit names and also a number N.
  • Pass them to findFruitName() method.
  • Inside the method, declare and initialize string ‘h’ and an integer ‘i’ value to null and 0.
  • First, break the string by using ‘,’ as a delimiter. Using String tokenizer get the count of tokens. Next, create an array of strings of the size of the count.
  • Iterate over the string and add each token to the array. If the input number N is greater than the count value, add the value of the string at a [i-1] position to the newly created string else get the value of the string at the [n-1] position i.e the last element in an array.
  • At last return the newly created string.
import java.util.Scanner;
import java.util.StringTokenizer;
 
public class Main
{
public static void main(String args[])
{
 
String str=new String();
Scanner sc=new Scanner(System.in);
str=sc.nextLine();
int n=sc.nextInt();
String k=findFruitName(str, n);
System.out.println(k);
 
}
 
 
public static String findFruitName(String m,int n)
{
int i=0;
String h=null;
StringTokenizer st=new StringTokenizer(m,",");
 
int max=st.countTokens();
String[] ss=new String[max];
while(st.hasMoreElements())
{
ss[i++]=st.nextToken();
}
if(n>max)
h=ss[i-1];
else
h=ss[n-1];
return h;
}

Output

Find Total Number of Words in a String

Write a java program to find the total number of words from the string. Read a string that contains comma-separated fruit names. Count the total number of words.

Input and Output Format

  • Input consists of a string and integer.
  • Output consists of an integer value.

Refer sample output for formatting specifications

Sample Input 1:
Apple, Banana, Orange

Sample Output 1:
Total number of words 3

Following are the steps to count words:

  • Input comma-separated string from the user. Pass them to findFruitName() method.
  • Inside the method, use the StringTokenizer instance and separate the string using ‘,’ as a delimiter.
  • Get all the counts of the token using the countToken() method and store the count in a variable max. At last, print the max value.
package com.demo;

import java.util.Scanner;
import java.util.StringTokenizer;

public class TestJava2 {
    public static void main(String[] args) {
        String str = new String();
        Scanner sc = new Scanner(System.in);
        str = sc.nextLine();
        findFruitName(str);
    }

    public static void findFruitName(String m) {
        int i = 0;
        String h = null;
        StringTokenizer st = new StringTokenizer(m, ",");
        int max = st.countTokens();
        System.out.println("Total number of words "+max);

    }
}

Output

Thus, in this way, we learn how to find the nth word in the string in Java. Also, learned how to count a total number of words in a comma-separeted string.