1

Recently, I've been trying to build a program that does 4 things:

1) Enter a word from the keyboard.

2) Check the context of this word with the context of a string that contains the letters of the alphabet.

3) Compare the letters of the given word with the letters of the alphabet string and whenever there is match, it will return the position of that letter in the alphabet string +1. (ex. word='a' position=1 since 'a' is the first letter)

4) Get the total of all of these positions.(ex. word='abc' total=6)

Now let me show you what I've written in terms of code.


//Part 1 Entering word from keyboard

package IntroductionJava;

import java.util.Scanner;

public class Numerology {

public static void main(String[] args) 
{

    int m=0,n=0,sum=0;
    int j,k;


    Scanner user_input=new Scanner(System.in);

    String word;
    System.out.print("Give a word: ");
    word=user_input.next();

String word1 = "\u03b1\u03b2\u03b3\u03b4\u03b5\u03b6\u03b7\u03b8\u03b9\u03ba\u03bb\u03bc\u03bd\u03be\u03bf\u03c0\u03c1\u03c3\u03c2\u03c4\u03c5\u03c6\u03c7\u03c8\u03c9";

//Part 2 check word for(int i=0; i<word.length(); i++) { if(word.charAt(i)>=word1.charAt(0) && word.charAt(i)<=word1.charAt(word1.length()-1)) { System.out.println("Your word '"+word+"' is valid."); break; } else { System.out.println("Your word '"+word+"' is invalid."); } break; //show System.out.print(word.charAt(i)); } //Part 3 Compare letters for(j=0; j<word.length(); j++) { for(k=0; k<word1.length(); k++) { if(word.charAt(j)==word1.charAt(k))//??? { m=k+1; } } } }

Now, Part 1 and 2 are working fine.

My problem lies when I try to compare the letters of the word that I'm entering with the String of letters in unicode format(the letters in unicode format are from the Greek alphabet). I've tried many variations, I've also consulted some of the articles here but I couldn't find a solution.

To make things a bit more clear let's say that I'm entering the word: "hello". I want to check whether 'h' is inside the alphabet string, and if it is I want to get it's position as an integer which in our case it's the number '8' (position in the string +1) and so on.

And the last part of my program is to get all those numbers from the given word and get it's total ('hello'=8+5+12+12+20=57).

Thank you very much in advance.

4
  • 1
    "I've tried many variations, I've also consulted some of the articles here but I couldn't find a solution." That is not a question. We will not write your code for you. Commented Nov 1, 2016 at 17:09
  • 1
    I'm confused. Why would you expect the value 8 for the letter 'h', if the alphabet is the greek alphabet? The greek alphabet doesn't have the letter 'h', does it? Commented Nov 1, 2016 at 17:09
  • 1
    I'm afraid I don't see how this could possibly work. Your 'hello' example is assigning a score to each letter based on its position in the English alphabet. Then you talk about Unicode characters and the Greek alphabet. How are you planning on assigning a score to those characters? Commented Nov 1, 2016 at 17:09
  • Also, you're supposed to compute a sum, but there is no + in your code. Commented Nov 1, 2016 at 17:16

1 Answer 1

0

How about this:

String alphabet = "abcdefgh";
String input = "abc";
int value = input.chars()
    .map(alphabet::indexOf)
    .map(i -> i + 1)
    .sum()

System.out.println(value);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.