I ALREADY SOLVED THE QUESTION. NO POINTS WILL BE AWARDED you ask the user for a
ID: 642740 • Letter: I
Question
I ALREADY SOLVED THE QUESTION. NO POINTS WILL BE AWARDED
you ask the user for a 3 letter word. If the first letter is uppercase or lowercase A, B, or C print out "The first letter is part of ABC and is [FIRST_LETTER]". Also do this for the second and third letters. For example if the second letter is uppercase or lowercase A, B or C print out "The second letter is part of ABC and is [SECOND_LETTER]".
please fix my code. it prints and looks only for the first word and not rest. here's my code:
package letterWords;
import java.util.Scanner;
public class letterWords {
public static void main(String[] args) {
String userLetter = "";
int wordCount;
char firstLetter = 'a';
char secondLetter = 'b';
char thirdLetter = 'c';
Scanner scan = new Scanner(System.in);
wordCount = userLetter.length();
System.out.println("Enter three letter words: ");
userLetter = scan.next();
switch(userLetter.charAt(0)) {
case 'A':
case 'a':
System.out.println("First letter is " + userLetter.charAt(0));
break;
case 'b':
case 'B':
System.out.println("Second letter is " + userLetter.charAt(1));
break;
case 'c':
case 'C':
System.out.println("Third letter is " + userLetter.charAt(2));
break;
default:
System.out.println("First letter is not a, b, or c");
}
System.out.println("Size: " + userLetter.length()); //if user enters less than 3 letters show error
}
}
Explanation / Answer
package letterWords;
import java.util.Scanner;
public class letterWords {
public static void main(String[] args) {
String userLetter = "";
int wordCount;
char firstLetter = 'a';
char secondLetter = 'b';
char thirdLetter = 'c';
Scanner scan = new Scanner(System.in);
wordCount = userLetter.length();
System.out.println("Enter three letter words: ");
userLetter = scan.next();
int i = 0;
String place;
if(wordCount <3)
System.out.println("Length should be atleast 3 Exitting the program");
for(i=0;i<3;i++)
{
if(i==0)
place = "FIRST";
else if(i==1)
place = "SECOND";
else
place = "THIRD";
switch(userLetter.charAt(i)) {
case 'A':
case 'a':
System.out.println("The " + place +" letter is part of ABC and is " + userLetter.charAt(i));
break;
case 'b':
case 'B':
System.out.println("The " + place +" letter is part of ABC and is " + userLetter.charAt(i));
break;
case 'c':
case 'C':
System.out.println("The " + place +" letter is part of ABC and is " + userLetter.charAt(i));
break;
default:
System.out.println(place +" letter is not a, b, or c");
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.