+ 1

String arr[ ];

In java how to finde a String array(index number) by equaling with int ? int a=6; if(String arr[i]==a){}

26th Jan 2022, 5:06 PM
A Rahman Mamnoon
A Rahman Mamnoon - avatar
6 Réponses
+ 1
I am trying to catch array indexs value by entering a number int choice =sc.nexInt(); String arr[]={"java","c","c++"}; for(int i=0;i<arr.length;i++){ If(choice==arr[i]){ System.out.print(arr[i]); } }
26th Jan 2022, 5:49 PM
A Rahman Mamnoon
A Rahman Mamnoon - avatar
+ 2
AB Rahman "Mamnoon" choice is integer And arr[i] is string then how you can compare? How number will be equal to string? If you want index value then compare with index like (choice == i) If you want index then you should enter string to get index by comparing with equals method. like (choice.equals(arr[i]))
26th Jan 2022, 5:56 PM
A͢J
A͢J - avatar
+ 2
AB Rahman "Mamnoon" If you want a value on a particular index then why don't pass index value directly like this: System.out.print(arr[choice]);
26th Jan 2022, 5:58 PM
A͢J
A͢J - avatar
+ 2
Scanner sc = new Scanner(System.in); int choice = sc.nextInt(); //debug String arr[]={"java","c","c++"}; for(int i=0;i<arr.length;i++){ if(choice==i){ //debug 🤔 System.out.print(arr[i]); } }
26th Jan 2022, 6:06 PM
Solo
Solo - avatar
+ 1
Scanner sc = new Scanner(System.in); String arr[]={"java","c","c++"}; int choice =sc.nexInt(); System.out.println(arr[choice]);
27th Jan 2022, 1:54 AM
Shen Bapiro
Shen Bapiro - avatar
0
What you want to trying to do?
26th Jan 2022, 5:31 PM
A͢J
A͢J - avatar