1. Write a complete Java program called Finder that declares an array of Strings
ID: 3681195 • Letter: 1
Question
1. Write a complete Java program called Finder that declares an array of Strings of length 5 (call it names) and then populates the array with user input from the command line. Next, prompt the user for another String (call it searchTerm) and search the names array for a match. If the searchTerm is found in the names array, output: "Search term found.". Otherwise, output "Search term not found.".
2. Write a complete Java program called Parser that gets a comma-delimited String of integers (e.g. "4, 8, 16, 32…") from the user at the command line and then converts the String to an ArrayList of integers (using the wrapper class) with each element containing one of the input integers in sequence. Finally, use a for loop to output the integers to the command line, each on a separate line.
3. Write a complete Java program called Scorer that declares a two-dimensional array of doubles (call it scores) with three rows and three columns. Use a nested while loop to get the nine (3 x 3) doubles from the user at the command line. Finally, use a nested for loop to compute the average of the doubles in each row and output these three averages to the command line.
Explanation / Answer
Program 1
public class Finder {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String names[] =new String[5];
java.util.Scanner in = new java.util.Scanner(System.in);
for(int i=0; i<names.length; i++){
System.out.println("Please Enter "+(i+1)+" String Value ...");
names[i] = in.next();
}
System.out.println("Enter your Search String... ");
String searchTerm = in.next();
boolean found = false;
for(int i=0; i<names.length; i++){
if(searchTerm.equalsIgnoreCase(names[i])){
found = true;
break;
}
}
if(found)
System.out.println("Search term Found");
else
System.out.println("Search term not Found");
}
}
Output:
Please Enter 1 String Value ...
aaa
Please Enter 2 String Value ...
bbb
Please Enter 3 String Value ...
cc
Please Enter 4 String Value ...
ddd
Please Enter 5 String Value ...
eeee
Enter your Search String...
tttt
Search term not Found
Program 2:
Parser.java
import java.util.ArrayList;
public class Parser {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
java.util.Scanner in = new java.util.Scanner(System.in);
System.out.println("Please enter input string with comma delimiter...");
String s = in.nextLine();
String values[] = s.split(",");
ArrayList list = new ArrayList();
for(int i=0; i<values.length; i++){
list.add(values[i].trim());
}
System.out.println("Output is:");
for(int i=0; i<list.size(); i++)
System.out.println(list.get(i));
}
}
Output:
Please enter input string with comma delimiter...
4,8,16,32
Output is:
4
8
16
32
Program 3:
Scorer.java
public class Scorer {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int n = 3;
double scores[][] = new double[n][n];
java.util.Scanner in = new java.util.Scanner(System.in);
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
System.out.println("Please Enter row ["+(i)+"] col ["+(j)+"] Value ...");
scores[i][j] = in.nextDouble();
}
}
System.out.println("Two Dimensional Input : ");
String str = "";
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
str = str + scores[i][j] +" ";
}
str = str + " ";
}
System.out.println(str);
double sum = 0.0;
for(int i=0; i<n; i++){
sum = 0.0;
for(int j=0; j<n; j++){
sum = sum + scores[i][j];
}
System.out.printf("The "+i+" Row Average is : %.2f ",((double)sum/(double)n));
}
}
}
Output:
Please Enter row [0] col [0] Value ...
1.1
Please Enter row [0] col [1] Value ...
2.2
Please Enter row [0] col [2] Value ...
3.3
Please Enter row [1] col [0] Value ...
4.4
Please Enter row [1] col [1] Value ...
5.5
Please Enter row [1] col [2] Value ...
6.6
Please Enter row [2] col [0] Value ...
7.7
Please Enter row [2] col [1] Value ...
8.8
Please Enter row [2] col [2] Value ...
9.9
Two Dimensional Input :
1.1 2.2 3.3
4.4 5.5 6.6
7.7 8.8 9.9
The 0 Row Average is : 2.20
The 1 Row Average is : 5.50
The 2 Row Average is : 8.80
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.