Home Ask Login Register

Developers Planet

Your answer is one click away!

Beatz February 2016

Get letter from user and print out letter with its position in the alphabet

I want to have the user input a letter, and then print out the letter and its position in the alphabet. If the input isn't a letter, I want the program to print: "Not valid".

How would I do that?

EDIT - Sorry for my unresponsiveness, I actually figured this out a few days ago. Heres the actual code though :

import java.util.Scanner;

public class AlphaPos {
   public static void main(String[] args) {
   java.util.Scanner input = new java.util.Scanner(System.in);
      System.out.println(" Please enter an upper or lowercase letter. ");
           String letter = input.nextLine();

           String alpha = " abcdefghijklmnopqrstuvwxyz";
           String beta = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";

         if((!alpha.contains(letter))&&(!beta.contains(letter))) 
              System.out.println(" You entered " +letter+ ", and you obviously can't follow directions."); 

        boolean hasUppercase = !letter.equals(letter.toLowerCase()); {
           if (hasUppercase) 
              System.out.println("Your letter is " +letter+ ", Uppercase, and its number is "+ beta.indexOf(letter));

        boolean hasLowercase = !letter.equals(letter.toUpperCase());
           if (hasLowercase)
                 System.out.println("Your letter is " +letter+ " ,lowercase , and its number is "+ alpha.indexOf(letter));
                                                                      }   

   }
}

School assignment, so this code has no purpose. And thanks for all the responses anyways!

Answers


Mleach February 2016

This is mine idea on first thought:

char c = ...
if(!Character.isLetter(c)) {
   return "Not valid!";
} else {
    System.out.println("Letter:"+c+", position: "+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(c));
}


cricket_007 February 2016

My simple testing verifies this works by taking advantage of the fact that char values are also evaluated to ASCII int values, so you can do some simple math as shown.

There are 26 possible positions for an English letter, so the if condition checks for out-of-bounds on that.

char c = 'd'; // Replace with user input like Scanner#next() or something

// output: 'A' = 1, 'Z' = 26
int position = (Character.toUpperCase(c) - 'A' + 1);

if (position < 1 || position > 26) {
    System.out.println("Not valid");
} else {
    System.out.println(String.format("Character: %s\nPosition: %d", c, position));
}

Output

Character: d
Position: 4


user1803551 February 2016

Probably not the fastest way but robust and easy to understand:

public class Example {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        while (true) {
            String input = sc.nextLine();
            if (input.matches("[a-zA-Z]")) {
                System.out.println(input);
                char c = input.toLowerCase(Locale.ENGLISH).charAt(0);
                System.out.println(c - 96);
            }
            else
                System.out.println("Not valid");
        }
    }
}

I use a regex to check if the input is a single character in the English alphabet, either lowercase or uppercase. If so, it is printed. To get the position in the alphabet, I converted it to lowercase (because both uppercase and lowercase have the same position) and subtracted its int value so to normalize a to 1.

The loop is just there for easier testing.

Thanks to cricket_007 and Tunaki for improving the answer (see revisions for details).

Post Status

Asked in February 2016
Viewed 3,239 times
Voted 6
Answered 3 times

Search




Leave an answer


Quote of the day: live life