I wanted help in regards to accessing the index then printing it. Below is the example and my desired output:
Input Format:
-Array Size
-string
-index to be printed
Input:
3 Joana Kim Nina 2
Desired Output:
Your friends are: Joana Kim Nina Your best friend is: Nina
This is the code I made. The third input format is not satisfied as I am yet to figure out the code to determine the bestfriend. How should I code to get the index from the given value?
int words=input.nextInt(); String[] names=new String[words]; for(int counter=0;counter<words; counter++){ names[counter]=input.next(); } System.out.println("Your friends are"); for(int counter=0;counter<words;counter++){ System.out.println(names[counter]);
Advertisement
Answer
After getting all the friends, you have to take best friend index and print that.
int words=input.nextInt(); String[] names=new String[words]; for(int counter=0;counter<words; counter++){ names[counter]=input.next(); } int bestFriendIndex =input.nextInt(); System.out.println("Your friends are"); for(int counter=0;counter<words;counter++){ System.out.println(names[counter]); } System.out.println("Your best friends is"); System.out.println(names[bestFriendIndex]);