wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes creating arrays from data in a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "creating arrays from data in a text file" Watch "creating arrays from data in a text file" New topic
Author

creating arrays from data in a text file

charlie mills
Ranch Hand

Joined: Nov 01, 2008
Posts: 49
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
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
charlie mills
Ranch Hand

Joined: Nov 01, 2008
Posts: 49
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

Joined: Oct 13, 2005
Posts: 36514
    
  16
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

Joined: Feb 06, 2009
Posts: 36
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
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

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
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: creating arrays from data in a text file
 
Similar Threads
help compiling
switch statement
Passing Jave object arrays
Returning A List Of Variables From A Folder Of Documents And Returning Them Into A New Document
Global Array not working