Results 1 to 10 of 10
Thread: Read txt file into arraylist
- 08-04-2010, 09:00 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 5
- Rep Power
- 0
Read txt file into arraylist
Hey
I have a txt file that looks like this:
at:@
too:2
I am triyng to get those words into an arraylist but i cannot get it to work.
I am doing it through an arraylist. In the arraylist the words are supposed to be in this order:
at
@
too
2
and so on.
This is what I have so far:
Scanner slang=new Scanner(new File(slangdict));
slang.useDelimiter(":");
//Tæller til oprettelse af array
while(slang.hasNext()) {
System.out.println(slang.next());
counter++;
}
//System.out.println(counter);
String[] slanglist=new String[counter];
int count=0;
while(slang.hasNext())
{
slanglist[count]=slang.next();
count++;
}
for(int i=0;i<slanglist.length;i++) {
System.out.println(slanglist[i]); }
The problem is, that when I print my arraylist on the scrren it just prints null.
What have I done wrong?
- 08-04-2010, 09:05 AM #2
I'd say the second while loop is never executed.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 08-04-2010, 01:50 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 5
- Rep Power
- 0
I cannot see why the second while loop shouldn't be executed?
By the way - when I say say the arraylist prints null to the screen, it does so numerous times, like this:
null
null
null
null
and so on.
Have you got any other solutions?
They will be highly appreciated?
- 08-04-2010, 02:58 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
You loop through the Scanner using next() and hasNext()...when it exists that loop the Scanner has reached the end of the file, so what do you think the next call to hasNext() (in the second loop) will return?
- 08-04-2010, 03:43 PM #5
Member
- Join Date
- Aug 2010
- Posts
- 5
- Rep Power
- 0
All right, I guess it will return "false" and the while loop wont't be executed. But how do I "restart" the scanner/hasNext\next" so it will read from the start of the file?
I guess thats the way to start the second while loop.
- 08-04-2010, 03:56 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Why not simply store everything in the first loop?
Using an ArrayList rather than a straight array?
In fact, if this is supposed to be mapping the left hand side to the right then you're probably looking at a Map.
Unless this is an exercise, of course.
- 08-04-2010, 04:04 PM #7
Member
- Join Date
- Aug 2010
- Posts
- 5
- Rep Power
- 0
Actually, I am supposed to put it into a hashmap, but i can't use the bufferedreader. So the only thing I could think of was to put it into an array first and the later on store it in a hashmap.
- 08-04-2010, 04:08 PM #8
Don't use a delimiter on the scanner, read a line, split it and add it to a map. Done.
I have no idea what a Reader is needed for.Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 08-04-2010, 04:30 PM #9
Member
- Join Date
- Aug 2010
- Posts
- 5
- Rep Power
- 0
Got it now - Thanks for the help guys!
- 08-04-2010, 04:34 PM #10
Glad you got it sorted. Mark the thread as Solved, please.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
Similar Threads
-
how to read the ArrayList inside HashMap
By koddy in forum New To JavaReplies: 6Last Post: 07-15-2010, 01:41 PM -
Read txt file to arrayList
By koddy in forum New To JavaReplies: 14Last Post: 04-29-2010, 05:15 PM -
how to read openproj(Projity) file i.e. ,POD file(Project Management file)
By mahendra.athneria in forum New To JavaReplies: 0Last Post: 02-11-2009, 09:53 AM -
How can I call my database read method to display its ArrayList?
By matpj in forum New To JavaReplies: 3Last Post: 01-29-2009, 10:20 AM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks