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");
}
source to share
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
.
source to share
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);
source to share
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
source to share