Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

creating arrays from data in a text file

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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));
}
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 74354
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic