• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

creating arrays from data in a text file

 
charlie mills
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

charlie
12
bob
34
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"));

while (file.hasNext())
{
names.add(file.nextLine());
}

for(int i = 0; i < names.size(); i++)
{
System.out.println(names.get(i));
}
 
Henry Wong
author
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Well, one option is to have two array lists -- one for names and one for numbers. And have the while loop alternate which list to place it in, instead of putting it all into the names array list.


You probably need to add type checking to prevent names going into numbers and vice versa, but that's later...

Henry
 
charlie mills
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Chamsay
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a very simple code, not sure if it is something that you would want.

Assuming that the text has the same format all throughout, a loop like this would work, you can fill in the other stuff:



You can then print all the names or all the numbers using a for each loop:

 
Henry Wong
author
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


Quite frankly, you have a ton of options available....

1... You can keep a flag of the last type, and toggle it, as you are processing the file.

2... You can keep a count of the number of lines processed, and process based upon this line being even or odd.

3... You can examine the number of items in the array lists, and process depending on whether the names list is equal in size to the number list, or one size larger.

4... As Campbell mentioned, you can examine the line read to determine whether it is a valid number or not, and process accordingly.

5... As Paul mentioned, you can process the file in pairs -- two lines at a time.

etc. etc. etc.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a very simple code, not sure if it is something that you would want....


Paul, for future reference, please try to provide hints instead of just giving out the answer. Generally, people learn better when they do it for themselves.

Thanks,
Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic