Explore BrainMass
Share

Java Searching an Array for Exact Match Using Loops

This content was STOLEN from BrainMass.com - View the original, and get the already-completed solution here!

The program uses an array that contains valid names for 10 cities in the Illinois. You ask the user of the program to enter a city name; your program then searches the array for that city name. If it is not found, the program should print a message that informs the user that the city name is not found in the list of valid cities in Illinois.
The data file provided for this lab includes the input statements and the necessary variable declarations. You need to use a loop to examine all the items in the array and test for a match. You also need to set a flag if there is a match, and then test the flag variable to determine if you should print the "Not a city in Illinois" message. Comments in the code tell you where to write your statements. You can use the Mail Order Program in this chapter guide.

- Write a loop statement that examines the names of cities stored in the array.
- Write a code that tests for a match.
- Write code that, when appropriate, prints the message: Not a city in Illinois.
- Execute the program using the following as input:
o Peoria
o Kankakee
o Austin
o Boise

© BrainMass Inc. brainmass.com October 25, 2018, 7:05 am ad1c9bdddf
https://brainmass.com/computer-science/arrays/java-searching-array-exact-match-loops-490186

Attachments

Solution Summary

The expert examines Java searching an array for exact match using loops.

$2.19
See Also This Related BrainMass Solution

Basic Arrays: Computer Science - Java Assistance

Exercise 1 Array Basics
1.) Write array declarations for each of the following:
a. Eight grade point averages
a. Int gradePointAverage [] = new int [8];

b. Five first names
a. String firstName [] = new String [5];

c. 21 ages
a. Int ages [] = new int[21];

2.) Declare and initialize arrays that store the following:
a. The whole numbers 9, 36, 36, 54, and 65
i. Int numbers [] = {9, 36, 36, 54, 65};

b. The last names Smith, Johnson, and Hanson:
i. String lastNames [] = {"smith", "Johnson", "Hanson"};

c. The price 12.00, 101.00, and 3.50
i. Double price [] = {12.00, 101.00, 3.50};

3.) Write an assignment statement that assigns the value 999 to the first element of an array of integers named idNumbers.
Int idNumbers[];
idNumbers [0] = 999;

Exercise 2 Searching An Array for an Exact Match

String apples[] = {"Garla", "Rome", "Fuji", "Delicious"};
Int foundIt, i;
Final int MAX_APPLES = 4;
String inApple;
inApple = JOptionPane.showInputDialog("Enter apple type:");
for(I = 0; i <= MAX_APPLEs; i++)
{
If(inApple == apples[i])
{
foundIt = true;
}
}

1 . Is the for loop written correctly? If not, how can you fix it?

No it should be for(I = 0; i <=MAX_APPLEs; i++)

2. Which variable is the flag?

foundIt

3. Is the flag variable declared correctly? If not, what should you do to fix it?

No Boolean foundIt = false;

4. Is the comparison in the if statement done correctly? If not, how can you fix it?

inApple.equals(apples[i])

View Full Posting Details