Reputation: 45
I'm new to Java, and I'm dealing with arrays. I have two arrays and would like to link them so that the elements in the second array correspond to those in the first. That way, I can search an element in the first array and display the corresponding value in the second array.
short[] Years = {2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012};
String[] Months = {"January", "February", "June", "January", "March", "June", "July", "August", "September", "March", "November", "March", "June"};
I'm trying to link it so that when I search March, for example, it displays 2004, 2009, 2011
List<String> results = new ArrayList<String>();
for (String s : months)
{
if(s.equals(term))
{
results.add(s);
}
}
if (results.size() > 0)
{
System.out.println("The month " + term + "appears " + results.size() + " times");
}
else
{
System.out.println("Your search for " + term + " did not return any results");
}
I have this code to show how many times a month appears, I just need it to print out the years after this.
Upvotes: 2
Views: 9626
Reputation: 4525
That's an "associative array" or a "map". Here is a Java example of the Map
interface.
Map<String, String> userEmails = new HashMap<String, String>();
userEmails.put("Tony", "[email protected]");
userEmails.put("Ozzy", "[email protected]");
Let's find Ozzy and print his email address:
System.out.println(userEmails.get("Ozzy"));
Map is the interface. It has operations "put" and "get". HashMap is a popular implementation of "Map" in which the map keys (the usernames) have unique hashcodes.
Upvotes: 4
Reputation: 713
Take a look at my code, I explained all of what I did.
import java.util.Scanner;
public class GetPrice {
public static void main(String args[]) {
// You can add any number of elements in both the arrays. The lengths
// should, of course, be the same for both the arrays.
String items[] = { "pizza", "cheesebread", "stromboli" };
double prices[] = { 1.1, 2.2, 3.3 };
// What we need to do is, once the user inputs the item, we need to
// search the string and find the index. As the prices are in the
// corresponding indices on the other array, we can just use the index
// number to get the price from the other array. So we just use the same
// index but on a different array.
System.out.println("Choose from the following, to get the price: ");
for (int index = 0; index < items.length; index++)
System.out.println(items[index]);
System.out.println("\nEnter the item: ");
Scanner input = new Scanner(System.in);
String inputItem = input.next();
for (int index = 0; index < items.length; index++) {
if (items[index].equals(inputItem.toLowerCa… {
System.out.println("Price for '" + items[index] + "' is: " + prices[index]);
}
}
}
}
Upvotes: 1
Reputation: 388446
Try
short[] Years = { 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
2009, 2010, 2011, 2012 };
String[] Months = { "January", "February", "June", "January", "March",
"June", "July", "August", "September", "March", "November",
"March", "June" };
String term = "March";
List<Short> indexes = new ArrayList<Short>();
for (int i = 0; i < Months.length; i++) {
String string = Months[i];
if (term.equals(string)) {
indexes.add(Years[i]);
}
}
for (Short short1 : indexes) {
System.out.print(short1);
}
Update:
Scanner keyboard = new Scanner(System.in);
short[] Years = { 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
2009, 2010, 2011, 2012 };
String[] Months = { "January", "February", "June", "January", "March",
"June", "July", "August", "September", "March", "November",
"March", "June" };
String term = keyboard.next();
List<String> results = new ArrayList<String>();
for (String s : Months) {
if (s.equals(term)) {
results.add(s);
}
}
if (results.size() > 0) {
System.out.println("The month " + term + "appears "
+ results.size() + " times");
} else {
System.out.println("Your search for " + term
+ " did not return any results");
}
List<Short> indexes = new ArrayList<Short>();
for (int i = 0; i < Months.length; i++) {
String string = Months[i];
if (term.equals(string)) {
indexes.add(Years[i]);
}
}
for (Short short1 : indexes) {
System.out.print(short1);
}
}
It gives me the following output for input March
The month Marchappears 3 times
200420092011
Upvotes: 0