-2

"Write and test the method that returns a letter of the alphabet from a given word, it the position is given. (Hint: use the method that begins with static char getLetter (String txt, int n)."

I've been staring at this question for 20 minutes, can't seem to understand what it wants me to do.

This is what I have so far:

// The "Divide_raminAmiri" class.
public class Divide_raminAmiri
{
    public static void main (String[] args)
    {
        String word;
        int location;

        System.out.println ("Enter a word.");
        word = In.getString ();
        System.out.println ("Enter the location of the letter.");
        location = In.getInt ();

    } // main method


    public static void test (char c)
    {
        System.out.println (word.charAt (location));
    }
} // Divide_raminAmiri class

I'm confused. I think what it wants me to do is use methods to find the letter at the location provided, but I'm getting errors. Any help appreciated!

  • Getting errors — what errors? Please quote any error message/s exactly. “Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers.” (from [What topics can I ask about here?](http://stackoverflow.com/help/on-topic)) – Ole V.V. Feb 10 '17 at 19:14
  • Ask your instructor. –  Feb 10 '17 at 19:15

1 Answers1

1

Okay so I'm not gonna give the full solution since this seems to be some kind of an exercise.

What might help you:

  1. The way you are doing it, you can't access the variable word in the test-method because it is only visible to the main-method, that's why we use parameters so you can pass variables to other methods.
  2. Speaking of parameters, your method is asked to have two parameters, one being the String and one being an int, your test-method only has a char as parameter (?)
  3. Your program starts and ends in the main-method, since you don't call your test-method there, it never gets executed.

Hints for your actual problem: You can get a char at position x from a String with the method

char myChar = myString.charAt(x);

A char can be cast to an int with

int asciiValue = (int) myChar;

My last hint: Big letters have an ASCII-Value starting at 65 (='A'), small letters of 97 (='a').

Hope that helped, if you got any more questions feel free to ask.

matejetz
  • 520
  • 3
  • 7