Hello,
This is my text file:
|RANK| 1950| 1994| 1998|
| 1| JIN| KIM| JOHN|
| 2| JIM| KIM| RON|
| 3| FRED|CRAIG| JIN|
| 4| JOHN| MARK| DON|
I would like the program to output as follows:
Enter name: > John
Year (1998) Ranked 1
Year (1950) Ranked 4
import java.util.Scanner; import java.io.FileNotFoundException; import java.util.ArrayList; public class JavaApplication1 { public static void main(String[] args) { //file instance java.io.File file = new java.io.File("boys-names.txt"); try{ Scanner input = new Scanner(file); //create scanner object Scanner in = new Scanner(System.in); System.out.print("Enter name:> "); String myname = in.nextLine(); //read user input ArrayList<String> yearList = new ArrayList<>(); ArrayList<String> rankList = new ArrayList<>(); //populate both arraylist from file ArrayList<String> rows = new ArrayList<>(); ArrayList<ArrayList<String>> cols = new ArrayList<ArrayList<String>>(); //Populate the matrix of names from file into cols; String[] fullData; while(input.hasNextLine()){ //split data fullData = input.nextLine().split("\\|"); for(int x=0; x<rows.size(); x++) for(int y =0; y<cols.get(x).size(); y++){ String currName = rows.get(x); if(currName.equalsIgnoreCase(myname)) System.out.println("Year (" + yearList.get(y) + ") Ranked " + rankList.get(x)); } } }catch(FileNotFoundException e){ System.out.println("Error the file cannot be found"); } } }
This is what I've done so far but all I'm getting is build successful and nothing about the text file gets printed so something is definitely wrong. Please help!