How to give a letter input a number value

1.6k Views Asked by At

So i am trying to make a simple program that uses a user input which will be a letter and number and make the program read it as a number so at the end the numbers get added. Let me give an example. If the user inputs A1 i want the program to read it as 100 and A2 as 90 for example. At the end I want the program to add these values and give the user and answer. This is what i have:

public class LCCalcMain {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        String input1, input2, answer;
        System.out.println("1st result");
        input1 = input.nextLine();
        input1 = System.out.println("2nd result");
        input2 = input.nextLine();
        int A1, A2, B1;
        A1 = 100;
        A2 = 90;
        B1 = 85;
        answer = input1 + input2;
        System.out.println(answer); 
    }
}

So i want them to enter like A1 and my program will read it as 100. So i can easily add everything at the end and give them an answer. Thanks for your help.

1

There are 1 best solutions below

3
On

You can just do an if/else case per input:

int currentValue1;//new variable
input1 = input.nextLine();
if("A1".equals(input1)){
    currentValue = 100;
}else if("A2".equals(input1)){
    //do another case
}else if("B1".equals(input1)){
    //do another case
}

int currentValue2;//new variable
input2 = input.nextLine();
if("A1".equals(input2)){
    currentValue2 = 100;
}else if("A2".equals(input2)){
    //do another case
}else if("B1".equals(input2)){
    //do another case
}

answer = input1 + input2;
System.out.println(answer);