-1

I am having some trouble assigning values to my letter array. I need a char array that consists of all letters and have a default value of the position in the alphabet.

Meaning the letter 'A' would have a value of 1 and letter 'B' have a value of 2 and letter 'C' would have a value of 3..etc.

And then the user would pick what letters they want to assign values. Meaning they could input:

C = -13
X = 5
H = 25
D = 4

And only those letters would have values that change. I then need to apply these values to an expression.

if my expression is: A*H+C

then my result is: 12

because: (1)(25)+-13=12

EDIT: The end goal of the program is to evaluate the expression by using the values that the user entered for the variables and if they have not specified a value to use the default values that i assigned in the array to evaluate the expression. The answer must be a type int and i must use arrays or stacks.

The user can input the following:

C = -13
X = 5
H = 25
D = 4
A*H+C

The output should be:

 Result: 12

The default values of the letters must be the following:

A=1

B=2

C=3

. . . Z= 26

Character[] letterArray = new int [26];
//to fill the array initially with values 
for( int i =0; i <letterArray.length; i ++){
       letterArray[i]= 66-(int)'A'
    }

       for(int j = 0 j <letterArray ; j++){
      String input = keyboard.nextLine();//to fill in specific values  

     }
jim
  • 37
  • 6
  • 1
    Question isn't clear, nor does it explicitly state your end goal of the program. What I understand is that you want to have input parsed and the certain character 'assigned' a value where you hard-code an expression? – Richard Kenneth Niescior Apr 15 '15 at 22:55
  • 1
    your question is basically "I have to buy a house, house price is 10 millions dollars, right now I have 1 dollar, what should I do next?" – Iłya Bursov Apr 15 '15 at 22:55
  • 1
    you can use `Map letterMap` to store letter number relationship if user enter C=-13 you can `letterMap.put('C', -13)` which will change the value of `'C'` – almeynman Apr 15 '15 at 22:57
  • @Lashane i have added a clearer explanation in the edit – jim Apr 15 '15 at 23:17
  • @Nomad I am trying to figure out how to do this by only using arrays or stacks – jim Apr 15 '15 at 23:19

1 Answers1

0

Alright, here is some code to get you started. I hope the comments help explain the logic. You still have to figure out how to compute each expression after replacing the characters with assigned values because I use the ScriptEngineManager based on this stackoverflow solution of evaluating string expressions

Good References: this is one solution to help calculate expression using stacks and this is another solution. I believe those solutions work and you can incorporate that into the code as opposed to using the ScriptEngineManager which kind of an overkill for your task.

So here you go:

public static void main(String[] args) throws ScriptException {

    int[] letterArray = new int[26];

    Scanner keyboard = new Scanner(System.in);
    boolean inUse = true;
    int num = 0;

    // initial assigning of characters
    for (int j = 0; j < letterArray.length; j++) {
        letterArray[j] = j + 1;

    }
    System.out.println("Default value of characters: ");
    printArray(letterArray);
    while (inUse) {

        System.out
                .println("enter 1 to change a value of a specific character..");
        System.out.println("enter 2 to compute an expression...");
        System.out.println("enter 3 or any other number to quit");

        System.out.print("Input: ");
        num = keyboard.nextInt();

        if (num == 1) {
            changeValue(letterArray, keyboard);
        } else if (num == 2) {
            System.out.println(computeExpression(keyboard, letterArray));
        } else {
            System.out.println("bye.");
            return;
        }

    }

}

public static void changeValue(int[] array, Scanner keyboard) {
    System.out
            .println("Enter the letter you will like to change: i.e A or B or C...");
    String character = keyboard.next().toUpperCase();
    // changing each input to upper-case to stay consistent
    // with the base value of 65
    int charnum = (int) character.charAt(0);
    if (Character.isLetter(character.charAt(0))) {
        System.out
                .println("Enter the value you are assigning to this character: ");

        array[charnum - 65] = keyboard.nextInt();
    } else {
        System.out
                .println("You entered an invalid character, sorry gotta start the process again");
    }

    System.out.println("Character Values: ");
    printArray(array);

}

// computing expression with ScriptEngineManager
// stole this from
// https://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form
// this is where you will want to implement your stack-based logic to
// analyze the expression
// and compute the result after replacing each character with it's assigned
// value
public static int computeExpression(Scanner keyboard, int[] array)
        throws ScriptException {
    System.out.println("enter expression you will like to compute: ");

    String expr = keyboard.next();

    expr = replaceValues(array, expr);
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("JavaScript");
    // int result = Integer.parseInt((String) (engine.eval(expr)));
    return (Integer.parseInt(engine.eval(expr).toString()));
}

// replacing each character with assigned value
public static String replaceValues(int[] array, String expr) {
    expr = expr.toUpperCase();
    int position = 0;
    for (char c : expr.toCharArray()) {
        if (Character.isLetter(c)) {

            position = (int) c - 65;
            expr = expr.replace(Character.toString(c),
                    Integer.toString(array[position]));
        }
    }
    System.out.println(expr);
    return expr;
}

// printing out each character value
public static void printArray(int[] array) {
    int count = 0;
    for (int eachCh : array) {
        System.out.print((char) (65 + count) + "->" + eachCh + "|||");
        count++;
    }
    System.out.println();
}
Community
  • 1
  • 1
Toni
  • 740
  • 2
  • 8
  • 14