|
-
January 14th, 2018, 09:44 PM
#1
Reading an array to find the middle char from two given chars
I am trying to refresh my java memory from when I was in college so i decided to do over some practice exercises i had. I am stuck one this question.
Implement a method named letter that will ask the user to enter a word of 2 letters. The first letter should be lower than the second (alphabetical order) The method will print to the screen the letter (of the alphabet) that is midway between the first and the last letters.
If the word is ‘am’ then the output should be: g
so far i have this but I'm not sure if an array should be used at all or should i use a switch statement to go through the whole alphabet. but then if i use a switch statement i need 26 cases.
What i want to do with the array is sort through it find the letters that match char at 0 and 1 find the length divide by 2 and then print the letter that in the middle.
[/CODE] public void letter(){
String word;
char firstLetter;
char secLetter;
do{
System.out.println("Enter a two letter word (word must be in alphabetical order).");
word = scan.nextLine();
word.toLowerCase();
firstLetter = word.charAt(0);
secLetter = word.charAt(1);
}while(firstLetter > secLetter);
char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|