51.7k views
3 votes
Write code to print the location of any alphabetic character in the 2-character string passCode. Each alphabetic character detected should print a separate statement followed by a newline. Ex: If passCode is "9a", output is:Alphabetic at 1import java.util.Scanner; public class FindAlpha { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); String passCode; passCode = scnr.next(); /* Your solution goes here */ } }

1 Answer

4 votes

Answer:

The code to this question can be described as follows:

Code:

char c1,c2; //defining char variable

c1=passcode.charAt(0); // hold the value of first character

c2=passcode.charAt(1); // hold the value of first character

//defining conditional statement to check value

if(Character.isLetter(c1))// check character is alphabet

{

System.out.println("Alphabetic is available in 0 position:"); //print message

}

else if(Character.isLetter(c2))

{

System.out.println("Alphabetic is available in 1 position:"); //print message

}

Step-by-step explanation:

Description of the code:

In the above code, two char variable "c1 and c2" is declared, in which these variable holds the first and second character value.

In the next step, if the conditional statement is used that check the input char is a letter or not.

To check these values "isLetter" method is used, that checks value is a letter or prints its location.

User Uthark
by
6.6k points