I have a java code (from another question I had) that reads two files into an ar
ID: 3599295 • Letter: I
Question
I have a java code (from another question I had) that reads two files into an array list; however, I needed to "convert" the code into reading from two files into a String array. What bits of code would I need to change in order to have it programmed for an array instead of an arraylist?
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class NameSearch {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter boy names file name: ");
String fileName = in.next();
List<String> boyNames = loadFile(fileName);
System.out.print("Enter girl names file name: ");
fileName = in.next();
List<String> girlNames = loadFile(fileName);
String name;
int index1, index2;
while (true) {
System.out.print("Enter a name to search(QUIT to exit): ");
name = in.next();
if(name.equalsIgnoreCase("QUIT"))
break;
index1 = findName(boyNames, name);
index2 = findName(girlNames, name);
if(index1 == -1 && index2 == -1) {
System.out.println("The name '" + name + "' was not found in either list.");
} else if(index1 == -1) {
System.out.println("The name '" + name + "' was found in popular girl names list (line " + (index2 + 1) + ").");
} else if(index2 == -1) {
System.out.println("The name '" + name + "' was found in popular boy names list (line " + (index1 + 1) + ").");
} else {
System.out.println("The name '" + name + "' was found in both lists: boy names (line " + (index1 + 1) + ") and girl names (line " + (index2 + 1) + ").");
}
}
}
private static int findName(List<String> names, String name) {
for(int i = 0; i < names.size(); ++i) {
if(names.get(i).equalsIgnoreCase(name)) {
return i;
}
}
return -1;
}
private static List<String> loadFile(String fileName) {
List<String> names = new ArrayList<>();
try {
Scanner in = new Scanner(new FileInputStream(fileName));
while (in.hasNext()) {
names.add(in.next());
}
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return names;
}
}
Explanation / Answer
Hi
I have modified the code from ArrayList to String array.
NameSearch.java
package a13;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Scanner;
public class NameSearch {
public static void main(String[] args) throws FileNotFoundException {
Scanner in = new Scanner(System.in);
System.out.print("Enter boy names file name: ");
String fileName = in.next();
String boyNames[] = loadFile(fileName);
System.out.print("Enter girl names file name: ");
fileName = in.next();
String girlNames[] = loadFile(fileName);
String name;
int index1, index2;
while (true) {
System.out.print("Enter a name to search(QUIT to exit): ");
name = in.next();
if(name.equalsIgnoreCase("QUIT"))
break;
index1 = findName(boyNames, name);
index2 = findName(girlNames, name);
if(index1 == -1 && index2 == -1) {
System.out.println("The name '" + name + "' was not found in either list.");
} else if(index1 == -1) {
System.out.println("The name '" + name + "' was found in popular girl names list (line " + (index2 + 1) + ").");
} else if(index2 == -1) {
System.out.println("The name '" + name + "' was found in popular boy names list (line " + (index1 + 1) + ").");
} else {
System.out.println("The name '" + name + "' was found in both lists: boy names (line " + (index1 + 1) + ") and girl names (line " + (index2 + 1) + ").");
}
}
}
private static int findName(String names[], String name) {
for(int i = 0; i < names.length; ++i) {
if(names[i].equalsIgnoreCase(name)) {
return i;
}
}
return -1;
}
private static String[] loadFile(String fileName) throws FileNotFoundException {
Scanner read = new Scanner(new FileInputStream(fileName));
int count = 0;
while (read.hasNext()) {
count++;
}
String names[] = new String[count];
try {
int i = 0;
Scanner in = new Scanner(new FileInputStream(fileName));
while (in.hasNext()) {
names[i] = in.next();
}
in.close();
read.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return names;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.