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 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.
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
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”
2. Create ACCESSOR FUNCTIONS in JAVA a) String moneyToString(int[] money); // Returns a nice looking string....
2. Create ACCESSOR FUNCTIONS in JAVA a) String moneyToString(int[] money); // Returns a nice looking string. Ex, "$6.25", "$0.21", "$4.01", "$2.00". MAKE SURE TO CONSIDER ALL EXAMPLES! b) *String moneyToText(int[] money); // Returns the Money as words. Ex,{123,51} => "one hundred and twenty three dollars and fifty one cents." YOU MAY ASSUME money <$1000. context: this is what I have so far package com.company; import java.util.*; public class Main { public static void main(String[]args) { int money[] = createMoney(12, 34);...
Ø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}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT