205k views
5 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 1 Hint: Use two if statements to check each of the two characters in the string, using Character.isLetter().

User Herin
by
5.8k points

1 Answer

5 votes

Answer:

// here is code in java.

import java.util.*;

//class defintion

class Main

{

// main method of class

public static void main (String[] args) throws java.lang.Exception

{

try{

// scanner object to read input

Scanner s=new Scanner(System.in);

String passcode;

System.out.print("please enter the passcode:");

// read string from user

passcode=s.nextLine();

// find first character

char ch=passcode.charAt(0);

// find second character

char ch1=passcode.charAt(1);

// if first character is alphabet

if(Character.isLetter(ch))

System.out.println("Alphabetic at 0:");

// if second character is alphabet

else if(Character.isLetter(ch1))

System.out.println("Alphabetic at 1:");

}catch(Exception ex){

return;}

}

}

Step-by-step explanation:

Create a scanner class object to read input from user.Read 2-character string

from user.Find the each character and check if it is a alphabet or not.If First

character is alphabet then print "Alphabetic at 0:" else if second character

is alphabet then print "Alphabetic at 1:".

Output:

please enter the passcode:b2

Alphabetic at 0:

User EyeQ Tech
by
5.2k points