Question

In: Computer Science

Create a method that returns the third character from the String word. Be sure to use...

Create a method that returns the third character from the String word. Be sure to use a try catch block and use the appropriate exception to deal with indexes out of a String’s bound: StringIndexOutOfBoundsException. Return the character 'x' (lowercase) when this exception is caught. Do not use if statements and do not use the general exception handler Exception.

Examples:

thirdLetter("test") -> 's'
public char thirdLetter(String word) 

{

}

​should return the char 'r'

Solutions

Expert Solution

Coding

import java.util.Scanner; //it is for user input
public class Simple
{
public static char thirdLetter(String words)//our static function which will get array string from main method
{
try//try catch block if any error will come it will redirect to catch
{
return(words.charAt(2));
}
catch(StringIndexOutOfBoundsException siobe)
{
System.out.println("invalid input No characher on third position");//output message if
return (' ');//it will return null value in that case
}

}

   public static void main(String[] args)
   {
   Scanner sc = new Scanner(System.in);
   System.out.print("Please Enter the String ::");//output message for print
   String word = sc.nextLine();//get string value from word
   System.out.println("Third Letter is ::"+thirdLetter(word));//here third letter
   }
}

output:

if you still have any Problem regarding this question please comment and if you like my code please appreciate me by thumbs up thank you.........


Related Solutions

Create a Java method that takes a String as input value and returns the number of...
Create a Java method that takes a String as input value and returns the number of vowels contained in that string.
Please use Python to create a method for a linked list that returns the index of...
Please use Python to create a method for a linked list that returns the index of a lookup value within the linked lust
Create a SPIM program that asks the user for a character string and a number. Repeatedly...
Create a SPIM program that asks the user for a character string and a number. Repeatedly display the string on the console for the number of times specified by the user. Format your assembly program to make it easier to read by including appropriate comments and formatting using appropriate white space. When ending your program call system call code 10. Example Output: Please enter a string: I will not repeat myself Please enter the integer number of times to repeat:...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
Write a method that returns the 200tth most frequent word and its frequency from a text...
Write a method that returns the 200tth most frequent word and its frequency from a text file. Use the best text reading method with a merge sort. and/or other techniques. The method signature: public static Integer countFAST(String fileName) throws Exception {...} The function takes the text file name.
Use a switch statement to write a function that returns TRUE if a character is a...
Use a switch statement to write a function that returns TRUE if a character is a consonant and returns FALSE otherwise.
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
no dictionaries please Returns a new string which is lowercase version of the given word with...
no dictionaries please Returns a new string which is lowercase version of the given word with special characters and digits removed The returned word should not have any of the following characters: ! . ? : , ' " - _ \ ( ) [ ] { } % 0 1 2 3 4 5 6 7 8 9 tab character and new-line character >>> clean_word("co-operate.") 'cooperate' >>> clean_word("Anti-viral drug remdesivir has little to no effect on Covid patients' chances...
Create and initialize a string array, write and use 3 functions.Write a program. Create a string...
Create and initialize a string array, write and use 3 functions.Write a program. Create a string array in the main(),called firstNameArray, initialize with7 first namesJim, Tuyet, Ann, Roberto, Crystal, Valla, Mathilda Write a first function, named searchArray, that passes two arguments: a single person’s name and the array reference, into a function,This function should then search the array to see if the name is in the firstNameArray. If found, it will return the array index where the name is found,...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns an ArrayList in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the ArrayList and then reverse the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT