How do I select a value from an array?

How do I select a value from an array? For example, this String[] ans = {"+", "-", "/", "*"};

, then I want to choose "+"

.

public static void main(String[] args) {
    String[] ans = {"+","-","/","*"};
    Random random = new Random();
    Scanner calcu = new Scanner(System.in);
    System.out.print("Enter First number: ");
    numOne = calcu.nextInt();
    System.out.print("Enter Second number: ");
    numTwo = calcu.nextInt();
    System.out.print("Choose an Operator to use");

}

      

+3


source to share


8 answers


You can use ans[0]

for "+" etc.

ans[0] = "+";
ans[1] = "-";
ans[2] = "/";
ans[3] ="*";

      

In your case, this code will help you:

     public static void main(String[] a) {

        String[] ans = {"+","-","/","*"};
        double result = 0;
        Scanner calcu = new Scanner(System.in);
        System.out.print("Enter First number: ");
        int numOne = calcu.nextInt();
        System.out.print("Enter Second number: ");
        int numTwo = calcu.nextInt();
        System.out.print("Choose an Operator to use");
        String oparation= calcu.next();

        if(oparation.equals(ans[0])){
           result = numOne + numTwo;
        }
        else if(oparation.equals(ans[1])){
            result = numOne - numTwo;
        }
        else if(oparation.equals(ans[2])){
            result = numOne / numTwo;

        } else if(oparation.equals(ans[3])){
            result = numOne * numTwo;
        }
        System.out.println("result is " + result);

   }

      



If you want to get the same result with the instruction switch

:

public static void main(String[] a) {

        double result = 0;
        Scanner calcu = new Scanner(System.in);
        System.out.print("Enter First number: ");
        int numOne = calcu.nextInt();
        System.out.print("Enter Second number: ");
        int numTwo = calcu.nextInt();
        System.out.print("Choose an Operator to use");
        String oparation= calcu.next();

        switch(oparation){
            case "+" :
            result = numOne + numTwo;
            break;

            case "-" :
            result = numOne - numTwo;
            break;

            case "/" :
            result = numOne / numTwo;
            break;

            case "*" :
            result = numOne * numTwo;
            break;
        }
        System.out.println("result is " + result);

   }

      

However, with an operator switch

, if you want to compare against variables like case ans[0]:

instead case "*"

, you can use enum

.

+6


source


How do you implement it, you will need to display a list like this user:

1: +
2: -
3: /
4: *

      



When they select a number, you can identify the operator with ans[input-1]

.

+3


source


You can access it using the get method :

ans[i]; // for getting first element you should set i to 0

      

+3


source


ans[indexThatYouWantToaccess]

make sure array index starts at 0

ans[0] -> +
ans[1] -> -
ans[2] -> /
ans[3] -> *

      

+3


source


To refer to individual elements in an array, use parentheses with the position of the element you want to refer to, starting at 0.

So

string txt = ans[0];

will give +

and string txt = ans[2];

give/

+3


source


You have an array as String[] ans = {"+","-","/","*"};

, which means the array index from zero

to array.length-1

contains the element that you are inserting into the array, so to get an element from the array, just iterate over the array, or just get the element at the array index

for(String value : ans){
            System.out.println(value);
        }

      

or

for(int i=0;i<ans.length-1;i++){
            System.out.println(ans[i]);
        }

      

or simple

String value = ans[index];//index must be from 0 to arrayLength-1
System.out.println("value "+value);

      

+3


source


ans[0]

will return the first (0, because the first element starts at 0, not 1) of the array element, the ans[1]

second, etc.

+2


source


You select a value from an array by referencing the index of its element. Array elements (things inside your array) are numbered / indexed from 0 to length-1 of your array.

In this case, if you want the first element of your array, you do:

ans[0]

      

If you want the last element of your array:

ans[ans.length-1]

      

Take a look at this tutorial for some great typing on Arrays. http://www.cs.cmu.edu/~adamchik/15-121/lectures/Arrays/arrays.html

+2


source







All Articles