0

How can i make the program to tell the user what letter of the alphabet it is:
ex: " k is the 11th letter in the alphabet"
here is the code that i got so far

import javax.swing.*;
public class Ncom{
String Inputnum;
char num;
public static void main(String[] args){
new Ncom();
}
public Ncom(){
char []upper = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char []lower = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
   
Inputnum=JOptionPane.showInputDialog("Enter a letter");
num= Inputnum.charAt(0);

if (Character.isUpperCase(num))

System.out.println(num+" is Upper case");
else
System.out.println(num+" is Lower case");


  

}
}

Thank you

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by mckrm
0

Arrays.binarySearch(array,char) gives the index

try this

if (Character.isUpperCase(num)){
System.out.println(num+" is Upper case");
System.out.println("Index is = "+(Arrays.binarySearch(upper, num)+1));
}
else{
System.out.println(num+" is Lower case");
System.out.println("Index is = "+(Arrays.binarySearch(lower, num)+1));
}

Edited by Nick Evan: added code tags

0

you can use the ascii

int num = Char_input ; 
if (num >= 65 && num <= 90 ){
S.O.P("is upper case" + " \nindex " +num-64 );
}

and the same for lower case
this algorithm is more efficient than yours no search in it

Edited by Nick Evan: Added code-tags

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.