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

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
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 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
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.
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...
Given a string, write a method called removeRepthat returns another string where adjacent characters that are...
Given a string, write a method called removeRepthat returns another string where adjacent characters that are the same have been reduced to a single character. Test the program by calling the method from the main method. For example: removeRep(“yyzzza”) à “yza” removeRep(“aabbbccd”) à “abcd” removeRep(“122333”) à “123”
ØWrite a method that takes a string representation of time in hours:minutes:seconds and returns the total...
ØWrite a method that takes a string representation of time in hours:minutes:seconds and returns the total number of seconds •For example, if the input is “0:02:20” it would return 140 ØIf the input string is missing the hours component it should still work •For example, input of “10:45” would return 645 ØWrite an overloaded form of the method that takes in only hours and minutes and returns the total number of seconds •It should leverage the first method you wrote...
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
Create a method named stringOperations () that accepts a string variable. This method must perform the...
Create a method named stringOperations () that accepts a string variable. This method must perform the following operations:                Split the sentence into an array                Display each element in the array on a seprate line using “ForEach”                Print the first element in the array, using the array syntax                Tell the user where the second word starts                Display the first 3 characters of the second word In your man method, ask the user to enter a sentence...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT