0

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.

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
dazzaondmic
  • 345
  • 4
  • 14
  • Can you make a more specific question, like a) what did you try b) what did you expect to happen c) how did it fail? – JOM Dec 18 '13 at 22:44
  • You cannot declare several variables and then use user input to pick one of the variables by name. If you want the user to enter a string and have the program find an associated number, use a [`HashMap`](http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html). – ajb Dec 18 '13 at 22:54
  • I will look into hash map as im not familiar with them. – dazzaondmic Dec 18 '13 at 22:58

1 Answers1

0

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); 
  • Using this technique just prints out whatever i enter like A1 or A2. For example if 1st input is A1 and 2nd input is A2. A1 being 100 and A2 being 90, i want my answer to be 190. – dazzaondmic Dec 18 '13 at 23:02
  • Could you use this in a loop until you are finished reading and keep a variable that you add your results to in order to get your final answer? – WeldFire Dec 18 '13 at 23:13
  • @user3117051 I'm kind of confused at what your question is now. This does exactly as you're describing. – But I'm Not A Wrapper Class Dec 19 '13 at 01:56