I am reading from a text file a specific line defined by a user input with if(nameLine.contains(inputFileName)){
I am trying to print an array of names and array of numbers from that line. Could anyone help me out with how to do so? Do I have to create 2 different methods or can I do it all in main? I only know how to work with one Array
in one code with while loop
, but I am not sure how to have it all combined. Thank you for your help!
My code is not finished, but this is how it looks so far
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
System.out.print("Enter the State name: ");
String inputFileName = in.next();
String names = "pplnames17.txt";
List<String> namearr = new ArrayList<String>();
File namesFile = new File(names);
Scanner stateSearch = new Scanner (namesFile);
//print title
String line0 = Files.readAllLines(Paths.get("pplnames17.txt")).get(0);
System.out.printf("%s in %s", line0, inputFileName);
while(stateSearch.hasNextLine()){
String nameLine = stateSearch.nextLine();
if(nameLine.contains(inputFileName)){
System.out.printf("\n%s",nameLine);
}
}
This is a partial of my
Array
code forString
, which I had it after awhile loop
while(stateSearch.hasNextLine()){
namearr.add(stateSearch.nextLine());
}
String[] arr = namearr.toArray(new String [0]);
System.out.println(arr);