Write a program that accepts two arrays, an array of fruit names and an array of price of fruits, and a fruit name and returns the price of the fruit. (Assume that a price in the second array corresponds to the price of the fruit at the same position in the first list) i. e. if inputs are {â??Grapesâ??, â??Orangeâ??, â??Appleâ??â?¦} and {23.50, 35.00, 12.40â?¦} then price of Grapes is 23.50
import java.util.*; class ArrayExample4 { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("Enter fruit names: "); String fruit[]=new String[3]; double price[]=new double[3]; for(int i=0;i<fruit.length;i++){ fruit[i]=input.next(); } System.out.println("Enter fruit prices: "); for(int i=0;i<price.length;i++){ price[i]=input.nextDouble(); } System.out.println("Enter fruit name: "); String f=input.next(); int index=0; for(int i=0;i<fruit.length;i++){ if(fruit[i].equals(f)){ index=i; } } System.out.println("price of "+f+" is "+price[index]); } }
Ads