I need to create 2 arrays from a list of data in a text file. I've managed to read the information from the file and display it in a command window but I can only display every line in the tezt file, not every other line.
This is an example of the text file:
mary 34 ...etc...
I need one array for the names, and one for the numbers
If you would be able to help me with the numbers part, as i'm sure i will be able to work the other one out for myself.
This is the code I have written so far, but as I said, it will read every line, not every other line, and I have no idea how to fix it.
ArrayList cities = new ArrayList();
Scanner file = new Scanner(new File("NamesAge.txt"));
for(int i = 0; i < names.size(); i++)
I know I need two arrays, but I dont know how to do it.
Do I need a new if loop within the while i've already got? Saying something along the lines of, if it's an even line put it in this array, else, put it in the second array?
If I'm on the right track, I don't know what I need to put in the if statement.
Edit: just re-read your post, and i think i understand what you meant now. i know how to create 2 arrays but how would i alternate which array list to place them in?
You can write a regular expression which will match numbers. Read about regular expressions here in the Java Tutorials.
Cheat: Search for a tool called JFlex and find its handbook/manual. In there, you will probably find a regular expression for numbers in Java. You can probably google for "regular expression number" and find one ready-made elsewhere too.